这个程序本来是看看 别人写的,后来发现他实现的太麻烦了,先晒出他写的:(这里没有批评任何人的意思,仅仅作为交流)
效果如下
第一个辅助类:
package wht.android.loading;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.widget.ImageView;
public class LoadingView extends ImageView implements Runnable
{
private boolean isStop = false;
private int[] imageIds;
private int index = 0;
private int length = 1;
public LoadingView(Context context)
{
this(context, null);
}
public LoadingView(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public void setImageIds(int[] imageId)
{
this.imageIds = imageId;
if(imageIds != null && imageIds.length > 0)
{
length = imageIds.length;
}
}
@Override
protected void onDetachedFromWindow()
{
// TODO Auto-generated method stub
super.onDetachedFromWindow();
isStop = true;
}
@Override
protected void onDraw(Canvas canvas)
{
// TODO Auto-generated method stub
super.onDraw(canvas);
if(imageIds != null && imageIds.length > 0)
{
this.setImageResource(imageIds[index]);
}
}
@Override
public void run()
{
while(!isStop)
{
index = ++index % length;
postInvalidate();
try
{
Thread.sleep(400);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
public void startAnim()
{
new Thread(this).start();
}
}
第二个类:
package wht.android.loading;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity
{
private LoadingView main_imageview;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
main_imageview = (LoadingView)findViewById(R.id.main_imageview);
initLoadingImages();
new Thread()
{
@Override
public void run()
{
main_imageview.startAnim();
}
}.start();
}
private void initLoadingImages()
{
int[] imageIds = new int[6];
imageIds[0] = R.drawable.loader_frame_1;
imageIds[1] = R.drawable.loader_frame_2;
imageIds[2] = R.drawable.loader_frame_3;
imageIds[3] = R.drawable.loader_frame_4;
imageIds[4] = R.drawable.loader_frame_5;
imageIds[5] = R.drawable.loader_frame_6;
main_imageview.setImageIds(imageIds);
}
}
后来我想通过handler和runnable 来实现,效果一样,而且简单很多,和大家分享一下:
晒一下我写的:
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ImageView;
public class EnterUIActivity extends Activity {
private ImageView image;
private int[] images = new int[]{
R.drawable.loader_frame_1,
R.drawable.loader_frame_2,
R.drawable.loader_frame_3,
R.drawable.loader_frame_4,
R.drawable.loader_frame_5,
R.drawable.loader_frame_6,};
/** Called when the activity is first created. */
private Handler handler = new Handler();
private static int num = 0;
Runnable runnable = new Runnable() {
@Override
public void run() {
image.setBackgroundResource(images[num++]);
if (num%6==0) {
num = 0;
}
handler.postDelayed(runnable, 700);
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
private void init(){
image = (ImageView)this.findViewById(R.id.imageView1);
handler.post(runnable);
}
@Override
protected void onDestroy() {
handler.removeCallbacks(runnable);
super.onDestroy();
}
}
布局文件很简单:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="179dp"/>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView1"
android:layout_centerHorizontal="true"
android:text="启动中......."
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>