主页面
package com.example.firework;
import com.example.firework.service.fireService;
import android.app.Activity;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
public class MainActivity extends Activity {
private Button bt_add;
private Button bt_remove;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt_add = (Button) findViewById(R.id.bt_add);
bt_remove = (Button) findViewById(R.id.bt_remove);
bt_add.setOnClickListener(new OnClickListener() {
/* (non-Javadoc)
* 点击按钮,创建小火箭
* @see android.view.View.OnClickListener#onClick(android.view.View)
*/
@Override
public void onClick(View v) {
startService(new Intent(getApplicationContext(),
fireService.class));
finish();
}
});
bt_remove.setOnClickListener(new OnClickListener() {
/* (non-Javadoc)
* 点击按钮,销毁小火箭
* @see android.view.View.OnClickListener#onClick(android.view.View)
*/
@Override
public void onClick(View v) {
stopService(new Intent(getApplicationContext(),
fireService.class));
}
});
}
}
小火箭服务
package com.example.firework.service;
import com.example.firework.R;
import com.example.firework.ShowActivity;
import android.app.Service;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.IBinder;
import android.telephony.TelephonyManager;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.ImageView;
public class fireService extends Service {
private LayoutParams mParams = new WindowManager.LayoutParams();
private WindowManager mWM;
private TelephonyManager tm;
private int width;
private int height;
private View fire;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
mWM = (WindowManager) getSystemService(WINDOW_SERVICE);
//得到屏幕宽高
width = mWM.getDefaultDisplay().getWidth();
height = mWM.getDefaultDisplay().getHeight();
//展示小火箭
Show();
super.onCreate();
}
private void Show() {
//设置显示参数
final WindowManager.LayoutParams params = mParams;
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;
params.setTitle("Toast");
params.gravity = Gravity.TOP + Gravity.LEFT;
// 得到显示的土司控件
fire = View.inflate(getApplicationContext(), R.layout.fire, null);
ImageView iv_fire = (ImageView) fire.findViewById(R.id.iv_fire);
AnimationDrawable background = (AnimationDrawable) iv_fire
.getBackground();
background.start();
// 设置触摸事件,用于拖拽功能
fire.setOnTouchListener(new OnTouchListener() {
private float startX;
private float startY;
//接受消息,更新火箭UI
private Handler mHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
mWM.updateViewLayout(fire, params);
};
};
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// 记录起始位置
startX = event.getRawX();
startY = event.getRawY();
break;
case MotionEvent.ACTION_MOVE:
float moveX = event.getRawX();
float moveY = event.getRawY();
float changeX = moveX - startX;
float changeY = moveY - startY;
params.x = (int) (params.x + changeX);
params.y = (int) (params.y + changeY);
// 容错处理
if (params.x < 0) {
params.x = 0;
}
// 容错处理
if (params.y < 0) {
params.y = 0;
}
// 容错处理
if (params.x > width - fire.getWidth()) {
params.x = width - fire.getWidth();
}
// 容错处理
if (params.y > height - fire.getHeight()) {
params.y = height - fire.getHeight();
}
// 窗口更新显示土司位置
mWM.updateViewLayout(fire, params);
startX = moveX;
startY = moveY;
break;
case MotionEvent.ACTION_UP:
//抬起发射
float move2X = event.getRawX();
float move2Y = event.getRawY();
if (move2X > width * 0.3 && move2X < width * 0.7
&& move2Y > height * 0.7) {
//打开烟雾页面
Intent intent = new Intent(getApplicationContext(),
ShowActivity.class);
//设置栈
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
//位移火箭
moveFire(move2X, move2Y);
return true;
}
break;
}
return true;
}
/**
* 移动小火箭
*
* @param moveY
* @param moveX
*/
private void moveFire(final float moveX, final float moveY) {
new Thread(new Runnable() {
@Override
public void run() {
int startY = (int) moveY;
for (int i = startY; i > 20; i -= 50) {
params.x = width/2-fire.getWidth()/2;
params.y = i;
mHandler.sendEmptyMessage(0);
try {
new Thread().sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}).start();
}
});
mWM.addView(fire, params);
}
@Override
public void onDestroy() {
mWM.removeView(fire);
super.onDestroy();
}
}
烟雾页面
package com.example.firework;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.ImageView;
public class ShowActivity extends Activity {
private ImageView iv_bottom;
private ImageView iv_way;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show);
initUI();
initAnim();
}
private void initAnim() {
//设置烟雾动画
AlphaAnimation aa = new AlphaAnimation(1.0f, 0.0f);
aa.setDuration(1000);
aa.setRepeatCount(1);
aa.setRepeatMode(Animation.REVERSE);
iv_bottom.startAnimation(aa);
iv_way.startAnimation(aa);
new Thread(new Runnable() {
@Override
public void run() {
//定时关闭此页面
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finish();
}
}).start();
}
/**
* 初始化控件
*/
private void initUI() {
iv_bottom = (ImageView) findViewById(R.id.iv_bottom);
iv_way = (ImageView) findViewById(R.id.iv_way);
}
}
小火箭服务里有个背景自动切换的功能,代码如下
这是在服务里开启背景切换功能
fire = View.inflate(getApplicationContext(), R.layout.fire, null);
ImageView iv_fire = (ImageView) fire.findViewById(R.id.iv_fire);
AnimationDrawable background = (AnimationDrawable) iv_fire
.getBackground();
background.start();
这是这个控件的布局文件,这里引用了一个负责背景切换的文件
<ImageView
android:id="@+id/iv_fire"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/fire_bg" />
这是引用的负责背景切换的文件,文件名为“fire_bg.xml”
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/desktop_rocket_launch_1"
android:duration="200"/>
<item android:drawable="@drawable/desktop_rocket_launch_2"
android:duration="200"/>
</animation-list>