package cn.zengfansheng.launchRocket;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
protected static final String TAG = "MainActivity";
private ImageView iv_rocket_image;
private ImageView iv_smoke_bottom;
private ImageView iv_smoke_top;
private WindowManager wm;
private int window_width;// 手机窗体宽度
private int window_height;// 手机窗体高度
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
int bottom_location = msg.what;
// 1、如果bottom_location达到了iv_smoke_top的底部,那么将iv_smoke_top显示动画
if (bottom_location < iv_smoke_top.getBottom() + 20) {
iv_smoke_top.setVisibility(ImageView.VISIBLE);
// 上面烟雾开启动画效果
AlphaAnimation aa = new AlphaAnimation(0.0f, 1.0f);
aa.setRepeatCount(1);
aa.setRepeatMode(AlphaAnimation.REVERSE);
aa.setFillAfter(true);
aa.setDuration(800);
iv_smoke_top.startAnimation(aa);
}
if (bottom_location < iv_smoke_top.getTop()) {
iv_smoke_top.setVisibility(ImageView.INVISIBLE);
}
int l = iv_rocket_image.getLeft();// 火箭左边
int t = iv_rocket_image.getTop();// 火箭上边
int r = iv_rocket_image.getRight();// 火箭右边
int b = iv_rocket_image.getBottom();// 获取下边
iv_rocket_image.layout(l, bottom_location - (b - t), r, bottom_location);
}
};
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
window_width = wm.getDefaultDisplay().getWidth();
window_height = wm.getDefaultDisplay().getHeight();
iv_rocket_image = (ImageView) findViewById(R.id.iv_rocket_image);
iv_smoke_bottom = (ImageView) findViewById(R.id.iv_smoke_bottom);
iv_smoke_top = (ImageView) findViewById(R.id.iv_smoke_top);
// 1、小火箭开启frame动画
iv_rocket_image.setBackgroundResource(R.anim.rocket);
AnimationDrawable rocketAnimation = (AnimationDrawable) iv_rocket_image.getBackground();
rocketAnimation.start();
// 2、注册触摸事件
iv_rocket_image.setOnTouchListener(new OnTouchListener() {
private int startX;
private int startY;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:// 1、触摸到火箭
startX = (int) event.getRawX();
startY = (int) event.getRawY();
Log.i(TAG, "触摸到了:" + startX + ":" + startY);
break;
case MotionEvent.ACTION_MOVE:// 2、移动火箭
Log.i(TAG, "移动~~");
int nowX = (int) event.getRawX();
int nowY = (int) event.getRawY();
int dX = nowX - startX;
int dY = nowY - startY;
int l = iv_rocket_image.getLeft() + dX;//火箭左边
int t = iv_rocket_image.getTop() + dY;//火箭上边
int r = iv_rocket_image.getRight() + dX;//火箭右边
int b = iv_rocket_image.getBottom() + dY;//获取下边
if (l < 0 || t < 0 || r > window_width || b > window_height-40) {//不让小火箭出边界
break;
}
iv_rocket_image.layout(l, t, r, b);
startX = nowX;
startY = nowY;
break;
case MotionEvent.ACTION_UP:// 3、手松开
Log.i(TAG, "松手~~~");
l = iv_rocket_image.getLeft();//火箭左边
t = iv_rocket_image.getTop();//火箭上边
r = iv_rocket_image.getRight();//火箭右边
b = iv_rocket_image.getBottom();//获取下边
final int iv_smoke_bottom_height = iv_smoke_bottom.getHeight();// 底部云层高度
// 如果在窗体的宽度1/3~2/3,底部窗体,烟雾的一般范围类,可以发送火箭
if (l > window_width / 3 && r<window_width*2/3 &&
b>window_height-40-iv_smoke_bottom_height/2 && b<window_height-40) {
// 发射
Toast.makeText(getApplicationContext(), "发射火箭了~~~",
Toast.LENGTH_SHORT).show();
iv_smoke_bottom.setVisibility(ImageView.VISIBLE);
// 下面的烟雾团开启动画效果
AlphaAnimation aa = new AlphaAnimation(0.0f, 1.0f);
aa.setRepeatCount(1);
aa.setRepeatMode(AlphaAnimation.REVERSE);
aa.setFillAfter(true);
aa.setDuration(1000);
iv_smoke_bottom.startAnimation(aa);
// 开启子线程发送消息给消息处理器,更新小火箭的位置
new Thread() {
@Override
public void run() {
int total_height = iv_smoke_bottom.getHeight()+iv_smoke_top.getHeight()+50;//总高度
int speed = total_height / 10;// 每次前进的距离
for (int i = 0; i < 10; i++) {
int what = iv_rocket_image.getBottom() - speed;
handler.sendEmptyMessage(what);
try {
Thread.sleep(70);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
break;
}
return true;
}
});
}
}