112.s1-模拟杀毒软件小火箭

小火箭是利用服务悬浮的,背景的烟雾效果是通过启动一个actiivty启动的,因此,当烟雾起来的时候是不能点击其他的按钮的,结束以后就关闭activity

WindowManager.LayoutParams.TYPE_PHONE;需要权限

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

创建一个布局文件,放置火箭rocket.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/iv_rocket"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/desktop_rocket_launch_1" />

</LinearLayout>

启动service的住MainActivity.java

package com.ldw.rocket;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void startRocket(View v){
    	//开启火箭的服务
    	startService(new Intent(this, RocketService.class));
    	finish();
    }
    
    public void endRocket(View v){
    	stopService(new Intent(this, RocketService.class));
    	finish();
    }
    
}

service实现火箭的升起RocketService.java

package com.ldw.rocket;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.graphics.drawable.AnimationDrawable;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.widget.ImageView;

public class RocketService extends Service {

	private WindowManager.LayoutParams params;
	private int winWidth;
	private int winHeight;
	private WindowManager mWM;
	private View view;

	private int startX;
	private int startY;

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();

		mWM = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);

		// 获取屏幕宽高
		winWidth = mWM.getDefaultDisplay().getWidth();
		winHeight = mWM.getDefaultDisplay().getHeight();

		params = new WindowManager.LayoutParams();
		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.gravity = Gravity.LEFT + Gravity.TOP;// 将重心位置设置为左上方,
													// 也就是(0,0)从左上方开始,而不是默认的重心位置
		params.setTitle("Toast");

		view = View.inflate(this, R.layout.rocket, null);// 初始化火箭布局

		// 初始化火箭帧动画,,火箭有冒火的效果
		ImageView ivRocket = (ImageView) view.findViewById(R.id.iv_rocket);
		ivRocket.setBackgroundResource(R.drawable.anim_rocket);
		AnimationDrawable anim = (AnimationDrawable) ivRocket.getBackground();
		anim.start();

		mWM.addView(view, params);

		view.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					// 初始化起点坐标
					startX = (int) event.getRawX();
					startY = (int) event.getRawY();
					break;
				case MotionEvent.ACTION_MOVE:
					int endX = (int) event.getRawX();
					int endY = (int) event.getRawY();

					// 计算移动偏移量
					int dx = endX - startX;
					int dy = endY - startY;

					// 更新浮窗位置
					params.x += dx;
					params.y += dy;

					// 防止坐标偏离屏幕
					if (params.x < 0) {
						params.x = 0;
					}

					if (params.y < 0) {
						params.y = 0;
					}

					// 防止坐标偏离屏幕
					if (params.x > winWidth - view.getWidth()) {
						params.x = winWidth - view.getWidth();
					}

					if (params.y > winHeight - view.getHeight()) {
						params.y = winHeight - view.getHeight();
					}

					// System.out.println("x:" + params.x + ";y:" + params.y);

					mWM.updateViewLayout(view, params);

					// 重新初始化起点坐标
					startX = (int) event.getRawX();
					startY = (int) event.getRawY();
					break;
				case MotionEvent.ACTION_UP:
					if (params.x > 100 && params.x < 250
							&& params.y > winHeight - 120) {
						System.out.println("发射火箭!!!");
						sendRocket();

						// 火箭发射以后,启动烟雾效果,开启一个新的activity
      						//火箭发射完成以后关闭activity
						Intent intent = new Intent(RocketService.this,
								BackgroundActivity.class);
						intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);// 启动一个栈来存放activity
						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;
			mWM.updateViewLayout(view, params);
		};
	};

	/**
	 * 发射火箭
	 */
	protected void sendRocket() {
		// 设置火箭居中
		params.x = winWidth / 2 - view.getWidth() / 2;
		mWM.updateViewLayout(view, params);

		new Thread() {
			@Override
			public void run() {
				int pos = 380;// 移动总距离
				for (int i = 0; i <= 10; 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();
		if (mWM != null && view != null) {
			mWM.removeView(view);
		}
	}
}

火箭的动画文件/res/drawable/anim_rocket.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>

火箭烟雾的背景布局activity_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<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_bottom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:src="@drawable/desktop_smoke_m" />

    <ImageView
        android:id="@+id/iv_top"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/iv_bottom"
        android:layout_alignParentLeft="true"
        android:src="@drawable/desktop_smoke_t" />

</RelativeLayout>

火箭烟雾的背景逻辑BackgroundActivity.java

package com.ldw.rocket;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.animation.AlphaAnimation;
import android.widget.ImageView;
/**
 * 烟雾背景
 */
public class BackgroundActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_bg);

		ImageView ivTop = (ImageView) findViewById(R.id.iv_top);
		ImageView ivBottom = (ImageView) findViewById(R.id.iv_bottom);

		// 渐变动画
		AlphaAnimation anim = new AlphaAnimation(0, 1);
		anim.setDuration(1000);
		anim.setFillAfter(true);// 动画结束后保持状态

		// 运行动画
		ivTop.startAnimation(anim);
		ivBottom.startAnimation(anim);

		new Handler().postDelayed(new Runnable() {

			@Override
			public void run() {
				finish();
			}
		}, 1000);// 延时1秒后结束activity
	}

}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值