Loading Image

时间:2023-03-08 18:41:02

Android doesn’t handle animated gifs, but here’s one way to display an animated loading image that is similar to the Spinner style of ProgressDialog.

Image Files:

Loading Images

drawable/loading.xml

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/loading_1" android:duration="100" />
<item android:drawable="@drawable/loading_2" android:duration="100" />
<item android:drawable="@drawable/loading_3" android:duration="100" />
<item android:drawable="@drawable/loading_4" android:duration="100" />
<item android:drawable="@drawable/loading_5" android:duration="100" />
<item android:drawable="@drawable/loading_6" android:duration="100" />
<item android:drawable="@drawable/loading_7" android:duration="100" />
<item android:drawable="@drawable/loading_8" android:duration="100" />
<item android:drawable="@drawable/loading_9" android:duration="100" />
<item android:drawable="@drawable/loading_10" android:duration="100" />
<item android:drawable="@drawable/loading_11" android:duration="100" />
<item android:drawable="@drawable/loading_12" android:duration="100" />
</animation-list>

MyActivity.java

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class MyActivity extends Activity {

ImageView loading;
AnimationDrawable loadAnimation;

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
LinearLayout main = new LinearLayout(this);
main.setOrientation(LinearLayout.VERTICAL);
setContentView(main);

loading = new ImageView(this);
loading.setImageResource(R.drawable.loading);
loadAnimation = (AnimationDrawable)loading.getDrawable();
main.addView(loading);

}

//can't start animating in OnCreate, so start when window becomes in focus
@Override
public void onWindowFocusChanged(boolean hasFocus){

loadAnimation.start();

}

}