- 双击居中
//图片设置为屏幕居中
ivDrag.layout(windowWidth / 2 - ivDrag.getWidth() / 2,
ivDrag.getTop(),
windowWidth / 2 + ivDrag.getWidth() / 2,
ivDrag.getBottom());
//在sp中记录位置
Editor edit = sp.edit();
edit.putInt("lastX", ivDrag.getLeft());
edit.putInt("lastY", ivDrag.getTop());
edit.commit();
注意: 为了能响应点击事件,需要在onTouch中返回false,将事件传递给onClick
- 窗体触摸移动
1. 为了获取触摸事件,首先需要去掉WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
2. 其次设置params.type = WindowManager.LayoutParams.TYPE_Phone;
3. 增加权限
4. 移动逻辑处理
case MotionEvent.ACTION_MOVE:
int dx = (int) (event.getRawX() - startX);
int dy = (int) (event.getRawY() - startY);
params.x += dx;
params.y += dy;
//控制图片不要超出屏幕边界
if (params.x < 0) {
params.x = 0;
}
//控制图片不要超出屏幕边界
if (params.y < 0) {
params.y = 0;
}
//控制图片不要超出屏幕边界
if (params.x > wm.getDefaultDisplay().getWidth()
- view.getWidth()) {
params.x = wm.getDefaultDisplay().getWidth()
- view.getWidth();
}
//控制图片不要超出屏幕边界
if (params.y > wm.getDefaultDisplay().getHeight()
- view.getHeight()) {
params.y = wm.getDefaultDisplay().getHeight()
- view.getHeight();
}
System.out.println("当前位置:" + params.x + ";" + params.y);
wm.updateViewLayout(view, params);//更新图片的显示位置
startX = (int) event.getRawX();
startY = (int) event.getRawY();
break;
- 火箭发射系统
1. 写主页面, 控制service的启动和停止
public void startRocket(View view) {
startService(new Intent(this, RocketService.class));
finish();
}
public void endRocket(View view) {
stopService(new Intent(this, RocketService.class));
finish();
}
2. 写RocketService, 专用于火箭显示和移动
public class RocketService extends Service {
private WindowManager wm;
private ImageView ivRocket;
private View view;
private int startX;
private int startY;
private WindowManager.LayoutParams params;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
wm = (WindowManager) getSystemService(WINDOW_SERVICE);
view = View.inflate(this, R.layout.view_rocket, null);//初始化界面
ivRocket = (ImageView) view.findViewById(R.id.iv_rocket);//获取火箭对象
ivRocket.setBackgroundResource(R.drawable.rocket);
AnimationDrawable anim = (AnimationDrawable) ivRocket.getBackground();
anim.start();//启动火箭的帧动画
params = new WindowManager.LayoutParams();
params.gravity = Gravity.TOP + Gravity.LEFT;
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
params.width = WindowManager.LayoutParams.WRAP_CONTENT;
params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
params.format = PixelFormat.TRANSLUCENT;
params.type = WindowManager.LayoutParams.TYPE_PHONE;
wm.addView(view, params);
ivRocket.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// System.out.println("被触摸了");
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
startX = (int) event.getRawX();
startY = (int) event.getRawY();
break;
case MotionEvent.ACTION_MOVE:
int dx = (int) (event.getRawX() - startX);
int dy = (int) (event.getRawY() - startY);
params.x += dx;
params.y += dy;
wm.updateViewLayout(view, params);// 更新图片的显示位置
startX = (int) event.getRawX();
startY = (int) event.getRawY();
break;
case MotionEvent.ACTION_UP:
// 发射火箭
if (params.x > 100 && params.x < 300 && params.y > 320) {
sendRocket();
//启动BackgroundActivity,展示烟雾
Intent intent = new Intent(RocketService.this,
BackgroundActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
break;
default:
break;
}
return true;
}
});
}
private Handler mHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
int y = msg.arg1;
params.y = y;
wm.updateViewLayout(view, params);// 更新图片的显示位置
};
};
protected void sendRocket() {
Toast.makeText(RocketService.this, "发射火箭", Toast.LENGTH_SHORT).show();
//将火箭位置调整为屏幕居中
params.x = wm.getDefaultDisplay().getWidth() / 2 - ivRocket.getWidth()
/ 2;
wm.updateViewLayout(view, params);
//执行循环, 每隔一段时间发送当前y坐标, 更新界面
new Thread() {
@Override
public void run() {
int pos = 380;
for (int i = 0; i < 11; i++) {
try {
Thread.sleep(50);//调整改时间,可以控制火箭速度
} catch (InterruptedException e) {
e.printStackTrace();
}
int y = pos - 38 * i;
Message msg = Message.obtain();
msg.arg1 = y;
mHandler.sendMessage(msg);
}
}
}.start();
}
@Override
public void onDestroy() {
super.onDestroy();
wm.removeView(view);
view = null;
}
}
3. 写BackgroundActivity, 用于烟雾的展示
public class BackgroundActivity extends Activity {
private ImageView ivSmokeBottom;
private ImageView ivSmokeTop;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_background);
ivSmokeBottom = (ImageView) findViewById(R.id.iv_smoke_m);
ivSmokeTop = (ImageView) findViewById(R.id.iv_smoke_t);
// 初始化烟雾
ivSmokeTop.setVisibility(View.VISIBLE);
ivSmokeBottom.setVisibility(View.VISIBLE);
//初始化渐变动画
AlphaAnimation animAlpha = new AlphaAnimation(0, 1);
animAlpha.setFillAfter(true);
animAlpha.setDuration(1000);
ivSmokeTop.startAnimation(animAlpha);
ivSmokeBottom.startAnimation(animAlpha);
//1秒之后,销毁activity
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
finish();
}
}, 1000);
}
}
android:name=".BackgroundActivity"
android:theme="@android:style/Theme.Translucent.NoTitleBar" >