一、小火箭框架搭建
MainActivity.javapackage com.example.smallrocket;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}public void startRocket(View view){startService(new Intent(this, RocketService.class));}public void stopRocket(View view){stopService(new Intent(this, RocketService.class));}}
RocketService.java
package com.example.smallrocket;import android.app.Service;import android.content.Context;import android.content.Intent;import android.graphics.PixelFormat;import android.os.IBinder;import android.view.View;import android.view.WindowManager;public class RocketService extends Service {private WindowManager.LayoutParams params;private int winWidth;private int winHeight;private WindowManager mWM;private View view;@Overridepublic IBinder onBind(Intent intent) {return null;}@Overridepublic void onCreate() {super.onCreate();mWM = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);winWidth = mWM.getDefaultDisplay().getWidth();winHeight = mWM.getDefaultDisplay().getHeight();params = new WindowManager.LayoutParams();params.width = WindowManager.LayoutParams.WRAP_CONTENT;params.height = 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");view = View.inflate(this, R.layout.rocket, null);//初始化火箭布局mWM.addView(view, params);}@Overridepublic void onDestroy() {if(mWM!=null && view!=null){mWM.removeView(view);view = null;}super.onDestroy();}}
rocket.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="/apk/res/android"android:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="vertical" ><ImageViewandroid:id="@+id/iv_rocket"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/desktop_rocket_launch_1" /></LinearLayout>
在清单文件中加入权限:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
运行效果:
二、小火箭完成
RocketService.javapackage com.example.smallrocket;import android.app.Service;import android.content.Context;import android.content.Intent;import android.graphics.PixelFormat;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;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;private int endX;private int endY;@Overridepublic IBinder onBind(Intent intent) {return null;}@Overridepublic void onCreate() {super.onCreate();mWM = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);winWidth = mWM.getDefaultDisplay().getWidth();winHeight = mWM.getDefaultDisplay().getHeight();params = new WindowManager.LayoutParams();params.width = WindowManager.LayoutParams.WRAP_CONTENT;params.height = 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);// 初始化火箭布局mWM.addView(view, params);view.setOnTouchListener(new OnTouchListener() {@Overridepublic 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:endX = (int) event.getRawX();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 - 100) {System.out.println("发射火箭");sendRocket();}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() {new Thread(){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();}@Overridepublic void onDestroy() {if (mWM != null && view != null) {mWM.removeView(view);view = null;}super.onDestroy();}}
运行效果:
动态火箭:
动画效果参考文档:/guide/topics/graphics/drawable-animation.html参考代码:
res/drawable/anim_rocket.xml
<?xml version="1.0" encoding="utf-8"?><animation-list xmlns:android="/apk/res/android"android:oneshot="true" ><itemandroid:drawable="@drawable/desktop_rocket_launch_1"android:duration="200"/><itemandroid:drawable="@drawable/desktop_rocket_launch_2"android:duration="200"/></animation-list>
RocketService.java
package com.example.smallrocket;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;private int endX;private int endY;@Overridepublic IBinder onBind(Intent intent) {return null;}@Overridepublic void onCreate() {super.onCreate();mWM = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);winWidth = mWM.getDefaultDisplay().getWidth();winHeight = mWM.getDefaultDisplay().getHeight();params = new WindowManager.LayoutParams();params.width = WindowManager.LayoutParams.WRAP_CONTENT;params.height = 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();//....}}
完成火箭的烟雾效果:
BackgroundActivity.javapackage com.example.smallrocket;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.view.animation.AlphaAnimation;import android.widget.ImageView;/*** 烟雾背景* @author Administrator**/public class BackgroundActivity extends Activity {@Overrideprotected 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(0f, 1f);anim.setDuration(1000);anim.setFillAfter(true);//动画结束后保持状态//运行动画ivTop.startAnimation(anim);ivBottom.startAnimation(anim);new Handler().postDelayed(new Runnable() {@Overridepublic void run() {finish();}}, 1000);//延迟1秒后结束activity}}
activity_bg.xml
<RelativeLayout xmlns:android="/apk/res/android"xmlns:tools="/tools"android:layout_width="match_parent"android:layout_height="match_parent"><ImageViewandroid: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" /><ImageViewandroid: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>
在清单文件中:
<activityandroid:name="com.example.smallrocket.BackgroundActivity"android:theme="@android:style/Theme.Translucent.NoTitleBar" />
RocketService.java
package com.example.smallrocket;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;private int endX;private int endY;@Overridepublic IBinder onBind(Intent intent) {return null;}@Overridepublic void onCreate() {super.onCreate();mWM = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);winWidth = mWM.getDefaultDisplay().getWidth();winHeight = mWM.getDefaultDisplay().getHeight();params = new WindowManager.LayoutParams();params.width = WindowManager.LayoutParams.WRAP_CONTENT;params.height = 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() {@Overridepublic 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:endX = (int) event.getRawX();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 - 100) {System.out.println("发射火箭");sendRocket();//启动烟雾效果Intent intent = new Intent(RocketService.this, BackgroundActivity.class);intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//启动一个栈来存放activitystartActivity(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(){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();}@Overridepublic void onDestroy() {if (mWM != null && view != null) {mWM.removeView(view);view = null;}super.onDestroy();}}
运行效果:
所有代码: /detail/btt/9407505
如果觉得《腾讯管家 小火箭效果的实现》对你有帮助,请点赞、收藏,并留下你的观点哦!