//设置动画效果:
<?xml version="1.0" encoding= "utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable ="@drawable/desktop_rocket_launch_1" android:duration="200" />
<item android:drawable ="@drawable/desktop_rocket_launch_2" android:duration="200" />
</animation-list>
-------------------------------------------------------------------------------------
public class MainActivity extends Activity
{
AnimationDrawable rocketAnimation;
ImageView rocketImage;
ImageView iv_bottom;
ImageView iv_somke_top;
//跟新ui
Handler handler = new Handler(){
public void handleMessage(android.os.Message
msg) {
int height
= msg. what;
int iv_height
= rocketImage.getBottom() - rocketImage.getTop(); //得到imageview 的高度
rocketImage.layout( rocketImage.getLeft(),
height, rocketImage.getRight(), height+iv_height );
if(msg. what ==0){
AlphaAnimation aa = new AlphaAnimation(1.0f,
0.0f);
aa.setFillAfter( true);
aa.setDuration(1000);
iv_somke_top.startAnimation(aa);
iv_bottom.setVisibility(View. INVISIBLE);
}
if(msg. what ==200){
iv_somke_top.setVisibility(View. VISIBLE);
}
};
};
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout. activity_main);
//找到关心的控件
rocketImage =
(ImageView) findViewById(R.id. iv_rocket);
rocketImage.setBackgroundResource(R.drawable. rocket);
rocketAnimation =
(AnimationDrawable) rocketImage.getBackground();
iv_bottom =
(ImageView) findViewById(R.id. iv_bottom);
iv_somke_top =
(ImageView) findViewById(R.id. iv_somke_top);
//给imageView设置触摸事件
rocketImage.setOnTouchListener( new OnTouchListener()
{
int startX =0;
int startY =
0;
@Override
public boolean onTouch(View
v, MotionEvent event) {
if (! rocketAnimation.isRunning())
{
rocketAnimation.start();
}
switch (event.getAction())
{
case MotionEvent. ACTION_DOWN: //摸到时
startX =
( int) event.getRawX();
startY =
( int) event.getRawY();
break;
case MotionEvent. ACTION_MOVE: //移动时
int newX
= ( int) event.getRawX();
int newY
= ( int) event.getRawY();
int dx
= newX - startX;
int dy
= newY - startY;
int l
= rocketImage.getLeft();
int r
= rocketImage.getRight();
int t
= rocketImage.getTop();
int b
= rocketImage.getBottom();
int newl
= l+ dx;
int newr
= r+ dx;
int newt
= t + dy;
int newb
= b + dy;
rocketImage.layout(newl,
newt, newr, newb); //确定 imageview的位置
startX =
( int) event.getRawX();
startY =
( int) event.getRawY();
break;
case MotionEvent. ACTION_UP: //离开时
int bottom
= rocketImage.getBottom();
int left
= rocketImage.getLeft();
if(
bottom > 400 && left>100){
Toast. makeText(getApplicationContext(), "发射火箭",
0).show();
iv_bottom .setVisibility(View.VISIBLE);
AlphaAnimation aa = new AlphaAnimation(0.0f,
1.0f);
aa.setDuration(1000);
iv_bottom.startAnimation(aa);
//动态定时更新 view对象的位置.
new Thread(){
public void run()
{
for( int i
=1 ;i<=8;i++){
try {
Thread. sleep(10);
} catch (InterruptedException
e) {
e.printStackTrace();
}
int height
= 50*i;
handler.sendEmptyMessage(400-height);
}
};
}.start();
}
break;
}
return true;
}
});
}
}