android一个简单的线程实例

TestHandlerActivity.java

package src.com;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;

public class TestHandlerActivity extends Activity implements Runnable {

private static final int my_key = 0x123;
private int a = 0;
private int[] myImage = {R.drawable.aa, R.drawable.bb,
R.drawable.cc, R.drawable.dd};
private Handler myHandler;
private Thread myThread;
private ImageView myImageView;
private Animation myAnimationAlpha;
private Animation myAnimationScale;
private Animation myAnimationTranslate;
private Animation myAnimationRotate;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

myImageView = (ImageView) findViewById(R.id.ImageView01);

myAnimationAlpha = new AlphaAnimation(0.1f, 0.1f);
myAnimationAlpha.setDuration(3000);

myAnimationScale = new ScaleAnimation(0.0f, 1.4f, 0.0f, 1.4f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
myAnimationScale.setDuration(3000);

myAnimationTranslate = new TranslateAnimation(30.0f, -80.0f, 30.0f, 300.0f);
myAnimationTranslate.setDuration(3000);

myAnimationRotate = new RotateAnimation(0.0f, +350.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
myAnimationRotate.setDuration(3000);

// myLinearLayout.setBackgroundColor(Color.WHITE);
myHandler = new Handler(){

@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);

switch(msg.what){
case TestHandlerActivity.my_key:
switch(a){
case 0:
myImageView.startAnimation(myAnimationAlpha);
break;
case 1:
myImageView.startAnimation(myAnimationScale);
break;
case 2:
myImageView.startAnimation(myAnimationTranslate);
break;
case 3:
myImageView.startAnimation(myAnimationRotate);
break;
default:
break;
}
myImageView.setImageResource(myImage[a-1]);
myImageView.setScaleType(ImageView.ScaleType.FIT_XY);
//myImageView.setLayoutParams(new LayoutParams(300, 200));

if( a == myImage.length ){
a = 0;
}

break;
default:
break;
}

//super.handleMessage(msg);
}

};

myThread = new Thread(this);
myThread.start();
}

@Override
public void run() {
// TODO Auto-generated method stub
try {
do{
a++;
Thread.sleep(4000);
Message msg = new Message();
msg.what = TestHandlerActivity.my_key;
myHandler.sendMessage(msg);

}while(Thread.interrupted() == false);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

 

xml布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<ImageView
android:id="@+id/ImageView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ImageView>

</LinearLayout>

posted @ 2012-05-10 09:00  幻星宇  阅读(821)  评论(0编辑  收藏  举报