1,将窗体设为透明:
在清单文件中:
<activity
android:name="com.itheima.rocket.MainActivity"
android:label="@string/app_name"
android:theme="@android:style/Theme.Translucent.NoTitleBar"---------------加上这句话
>
2,新建一个drawable目录或者(anim目录)。
------> 新建一个rocket.xml文件
然后XML文件中的代码如下:
<?xml version="1.0" encoding="utf-8"?>
- <!--动画列表 oneshot是播放的次数,false说明可以播放多次-->
<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>
图片和XML文件放在一个目录下,图片资源如下:
desktop_rocket_launch_1.png
desktop_rocket_launch_2.png
3,activity_main.xml的代码如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/iv_rocket"
android:layout_width="40dip"
android:layout_height="73dip" />
<ImageView
android:id="@+id/iv_bottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:src="@drawable/desktop_smoke_m"
android:visibility="invisible" />
<ImageView
android:id="@+id/iv_top"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/iv_bottom"
android:src="@drawable/desktop_smoke_t"
android:visibility="invisible" />
</RelativeLayout>
4,所涉及的图片资源以及存放目录如图:
两张火焰的图片:
desktop_smoke_m.png
desktop_smoke_t.png
补充小知识:
l 和 t 是控件左边缘和上边缘相对于父类控件左边缘和上边缘的距离
r 和 b是空间右边缘和下边缘相对于父类控件左边缘和上边缘的距离
r 和 b是空间右边缘和下边缘相对于父类控件左边缘和上边缘的距离
4,MainActivity.java中的代码如下
:
package com.itheima.rocket;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.animation.AlphaAnimation;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
private ImageView rocketImage;
private AnimationDrawable rocketAnimation;
private ImageView iv_top,iv_bottom;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取ImageView对象
rocketImage = (ImageView) findViewById(R.id.iv_rocket);
//获取rocket.xml对象,用于填充ImageView对象
rocketImage.setBackgroundResource(R.drawable.rocket);
//火箭火焰的两个控件
iv_top = (ImageView) findViewById(R.id.iv_top);
iv_bottom = (ImageView) findViewById(R.id.iv_bottom);
//用于让图片的背景不停的切换
rocketAnimation = (AnimationDrawable) rocketImage.getBackground();
rocketAnimation.start();
//设置小火箭的触摸事件
rocketImage.setOnTouchListener(new OnTouchListener() {
int startX = 0;
int startY = 0;
//五个步骤一定要记住
@Override
public boolean onTouch(View v, MotionEvent event) {
//获取事件的类型
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN://按下
//1.第一次坐标
startX = (int) event.getRawX();
startY = (int) event.getRawY();
break;
case MotionEvent.ACTION_MOVE://移动
//2.新坐标
int newX = (int) event.getRawX();
int newY = (int) event.getRawY();
//3.偏移量
int dX = newX - startX;
int dY = newY - startY;
//4.更新小火箭的位置
rocketImage.layout(rocketImage.getLeft()+dX, rocketImage.getTop()+dY, rocketImage.getRight()+dX, rocketImage.getBottom()+dY);
//5.重新记录坐标
startX = (int) event.getRawX();
startY = (int) event.getRawY();
break;
case MotionEvent.ACTION_UP://离开
//获取
int newl = rocketImage.getLeft();
int newt = rocketImage.getTop();
int newr = rocketImage.getRight();
//火箭发射架系统
if(newl > 100&& newt > 320&&newr<320){
// Toast.makeText(MainActivity.this, "火箭发射架系统", 1).show();
sendRocket();
iv_bottom.setVisibility(View.VISIBLE);
iv_top.setVisibility(View.VISIBLE);
//渐变动画--从看不见到看的见
AlphaAnimation aa = new AlphaAnimation(0.0f, 1.0f);
//持续时间为0.5.秒
aa.setDuration(500);
//设置播放次数为1
aa.setRepeatCount(1);
//播放后的状态
aa.setFillAfter(true);
//停留在取反状态--从看得见到看不见
aa.setRepeatMode(AlphaAnimation.REVERSE);
iv_top.startAnimation(aa);
iv_bottom.startAnimation(aa);
}
break;
}
return true;
}
});
}
private Handler handler = new Handler(){
public void handleMessage(Message msg) {
int y = (Integer) msg.obj;
rocketImage.layout(rocketImage.getLeft(), y, rocketImage.getRight(), y+rocketImage.getHeight());
//让小火箭到一定的高度后才让火焰显示
// if(y <320){
// iv_top.setVisibility(View.VISIBLE);
// AlphaAnimation aa = new AlphaAnimation(0.0f, 1.0f);
// aa.setDuration(300);
// iv_top.startAnimation(aa);
// }
//
// if(y <20){
//从看的见到看不见
// AlphaAnimation aa = new AlphaAnimation(1.0f, 0.0f);
// aa.setDuration(300);
// aa.setFillAfter(true);
// iv_top.startAnimation(aa);
// }
};
};
/**
* 火箭发射系统
*/
protected void sendRocket() {
//不能主线程中休眠,在子线程中休眠
new Thread(){
public void run() {
for(int i=0;i<12;i++){
//休眠0.1秒
SystemClock.sleep(10);
int y = 380 - i* 38;
Message msg = Message.obtain();
msg.obj = y;
handler.sendMessage(msg);
}
};
}.start();
}
}
效果和一些软件的效果一样,如360带的那种小火箭。
放一张效果图:看见白色的火焰和小火箭的尾巴了吗~成功