Android桌面悬浮窗进阶,QQ手机管家小火箭效果实现

来源:互联网 发布:windows xp平板 编辑:程序博客网 时间:2024/05/01 07:12

转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/16919859

今天是2013年的最后一天了,这里首先提前祝大家新年快乐!同时,本篇文章也是我今年的最后一篇文章了,因此我想要让它尽量有点特殊性,比起平时的文章要多一些特色。记得在今年年初的时候,我写的第一篇文章是模仿360手机卫士的桌面悬浮窗效果,那么为了能够首尾呼应,今年的最后一篇文章就同样还是来实现桌面悬浮窗的效果吧,当然效果将会更加高级。

相信用过QQ手机管家的朋友们都会知道它有一个小火箭加速的功能,将小火箭拖动到火箭发射台上发射就会出现一个火箭升空的动画,那么今天我们就来模仿着实现一下这个效果吧。

这次我们将代码的重点放在火箭升空的效果上,因此简单起见,就直接在模仿360手机卫士悬浮窗的那份代码的基础上继续开发了,如果你还没有看过那篇文章的话,建议先去阅读 Android桌面悬浮窗效果实现,仿360手机卫士悬浮窗效果 。

比起普通的桌面悬浮窗,现在我们需要在拖动悬浮窗的时候将悬浮窗变成一个小火箭,并且在屏幕的底部添加一个火箭发射台。那么我们就从火箭发射台开始编写吧,首先创建launcher.xml作为火箭发射台的布局文件,如下所示:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <LinearLayout  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:layout_width="wrap_content"  
  5.     android:layout_height="wrap_content"  
  6.     android:orientation="vertical"  
  7.     >  
  8.       
  9.     <ImageView   
  10.         android:id="@+id/launcher_img"  
  11.         android:layout_width="200dp"  
  12.         android:layout_height="88dp"  
  13.         android:src="@drawable/launcher_bg_hold"  
  14.         />  
  15.       
  16. </LinearLayout>  
可以看到,这里的ImageView是用于显示当前火箭发射台状态的。我事先准备好了两张图片,一张是当小火箭未拖动到火箭发射台时显示的,一张是当小火箭拖动到火箭发射台上时显示的。

接下来创建RocketLauncher类,作为火箭发射台的View,代码如下所示:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public class RocketLauncher extends LinearLayout {  
  2.   
  3.     /** 
  4.      * 记录火箭发射台的宽度 
  5.      */  
  6.     public static int width;  
  7.   
  8.     /** 
  9.      * 记录火箭发射台的高度 
  10.      */  
  11.     public static int height;  
  12.   
  13.     /** 
  14.      * 火箭发射台的背景图片 
  15.      */  
  16.     private ImageView launcherImg;  
  17.   
  18.     public RocketLauncher(Context context) {  
  19.         super(context);  
  20.         LayoutInflater.from(context).inflate(R.layout.launcher, this);  
  21.         launcherImg = (ImageView) findViewById(R.id.launcher_img);  
  22.         width = launcherImg.getLayoutParams().width;  
  23.         height = launcherImg.getLayoutParams().height;  
  24.     }  
  25.   
  26.     /** 
  27.      * 更新火箭发射台的显示状态。如果小火箭被拖到火箭发射台上,就显示发射。 
  28.      */  
  29.     public void updateLauncherStatus(boolean isReadyToLaunch) {  
  30.         if (isReadyToLaunch) {  
  31.             launcherImg.setImageResource(R.drawable.launcher_bg_fire);  
  32.         } else {  
  33.             launcherImg.setImageResource(R.drawable.launcher_bg_hold);  
  34.         }  
  35.     }  
  36.   
  37. }  
RocketLauncher中的代码还是非常简单的,在构建方法中调用了LayoutInflater的inflate()方法来将launcher.xml这个布局文件加载进来,并获取到了当前View的宽度和高度。在updateLauncherStatus()方法中会进行判断,如果传入的参数是true,就显示小火箭即将发射的图片,如果传入的是false,就显示将小火箭拖动到发射台的图片。

新增的文件只有这两个,剩下的就是要修改之前的代码了。首先修改MyWindowManager中的代码,如下所示:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public class MyWindowManager {  
  2.   
  3.     /** 
  4.      * 小悬浮窗View的实例 
  5.      */  
  6.     private static FloatWindowSmallView smallWindow;  
  7.   
  8.     /** 
  9.      * 大悬浮窗View的实例 
  10.      */  
  11.     private static FloatWindowBigView bigWindow;  
  12.   
  13.     /** 
  14.      * 火箭发射台的实例 
  15.      */  
  16.     private static RocketLauncher rocketLauncher;  
  17.   
  18.     /** 
  19.      * 小悬浮窗View的参数 
  20.      */  
  21.     private static LayoutParams smallWindowParams;  
  22.   
  23.     /** 
  24.      * 大悬浮窗View的参数 
  25.      */  
  26.     private static LayoutParams bigWindowParams;  
  27.   
  28.     /** 
  29.      * 火箭发射台的参数 
  30.      */  
  31.     private static LayoutParams launcherParams;  
  32.   
  33.     /** 
  34.      * 用于控制在屏幕上添加或移除悬浮窗 
  35.      */  
  36.     private static WindowManager mWindowManager;  
  37.   
  38.     /** 
  39.      * 用于获取手机可用内存 
  40.      */  
  41.     private static ActivityManager mActivityManager;  
  42.   
  43.     /** 
  44.      * 创建一个小悬浮窗。初始位置为屏幕的右部中间位置。 
  45.      */  
  46.     public static void createSmallWindow(Context context) {  
  47.         WindowManager windowManager = getWindowManager(context);  
  48.         int screenWidth = windowManager.getDefaultDisplay().getWidth();  
  49.         int screenHeight = windowManager.getDefaultDisplay().getHeight();  
  50.         if (smallWindow == null) {  
  51.             smallWindow = new FloatWindowSmallView(context);  
  52.             if (smallWindowParams == null) {  
  53.                 smallWindowParams = new LayoutParams();  
  54.                 smallWindowParams.type = LayoutParams.TYPE_SYSTEM_ALERT;  
  55.                 smallWindowParams.format = PixelFormat.RGBA_8888;  
  56.                 smallWindowParams.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL  
  57.                         | LayoutParams.FLAG_NOT_FOCUSABLE;  
  58.                 smallWindowParams.gravity = Gravity.LEFT | Gravity.TOP;  
  59.                 smallWindowParams.width = FloatWindowSmallView.windowViewWidth;  
  60.                 smallWindowParams.height = FloatWindowSmallView.windowViewHeight;  
  61.                 smallWindowParams.x = screenWidth;  
  62.                 smallWindowParams.y = screenHeight / 2;  
  63.             }  
  64.             smallWindow.setParams(smallWindowParams);  
  65.             windowManager.addView(smallWindow, smallWindowParams);  
  66.         }  
  67.     }  
  68.   
  69.     /** 
  70.      * 将小悬浮窗从屏幕上移除。 
  71.      */  
  72.     public static void removeSmallWindow(Context context) {  
  73.         if (smallWindow != null) {  
  74.             WindowManager windowManager = getWindowManager(context);  
  75.             windowManager.removeView(smallWindow);  
  76.             smallWindow = null;  
  77.         }  
  78.     }  
  79.   
  80.     /** 
  81.      * 创建一个大悬浮窗。位置为屏幕正中间。 
  82.      */  
  83.     public static void createBigWindow(Context context) {  
  84.         WindowManager windowManager = getWindowManager(context);  
  85.         int screenWidth = windowManager.getDefaultDisplay().getWidth();  
  86.         int screenHeight = windowManager.getDefaultDisplay().getHeight();  
  87.         if (bigWindow == null) {  
  88.             bigWindow = new FloatWindowBigView(context);  
  89.             if (bigWindowParams == null) {  
  90.                 bigWindowParams = new LayoutParams();  
  91.                 bigWindowParams.x = screenWidth / 2  
  92.                         - FloatWindowBigView.viewWidth / 2;  
  93.                 bigWindowParams.y = screenHeight / 2  
  94.                         - FloatWindowBigView.viewHeight / 2;  
  95.                 bigWindowParams.type = LayoutParams.TYPE_PHONE;  
  96.                 bigWindowParams.format = PixelFormat.RGBA_8888;  
  97.                 bigWindowParams.gravity = Gravity.LEFT | Gravity.TOP;  
  98.                 bigWindowParams.width = FloatWindowBigView.viewWidth;  
  99.                 bigWindowParams.height = FloatWindowBigView.viewHeight;  
  100.             }  
  101.             windowManager.addView(bigWindow, bigWindowParams);  
  102.         }  
  103.     }  
  104.   
  105.     /** 
  106.      * 将大悬浮窗从屏幕上移除。 
  107.      */  
  108.     public static void removeBigWindow(Context context) {  
  109.         if (bigWindow != null) {  
  110.             WindowManager windowManager = getWindowManager(context);  
  111.             windowManager.removeView(bigWindow);  
  112.             bigWindow = null;  
  113.         }  
  114.     }  
  115.   
  116.     /** 
  117.      * 创建一个火箭发射台,位置为屏幕底部。 
  118.      */  
  119.     public static void createLauncher(Context context) {  
  120.         WindowManager windowManager = getWindowManager(context);  
  121.         int screenWidth = windowManager.getDefaultDisplay().getWidth();  
  122.         int screenHeight = windowManager.getDefaultDisplay().getHeight();  
  123.         if (rocketLauncher == null) {  
  124.             rocketLauncher = new RocketLauncher(context);  
  125.             if (launcherParams == null) {  
  126.                 launcherParams = new LayoutParams();  
  127.                 launcherParams.x = screenWidth / 2 - RocketLauncher.width / 2;  
  128.                 launcherParams.y = screenHeight - RocketLauncher.height;  
  129.                 launcherParams.type = LayoutParams.TYPE_PHONE;  
  130.                 launcherParams.format = PixelFormat.RGBA_8888;  
  131.                 launcherParams.gravity = Gravity.LEFT | Gravity.TOP;  
  132.                 launcherParams.width = RocketLauncher.width;  
  133.                 launcherParams.height = RocketLauncher.height;  
  134.             }  
  135.             windowManager.addView(rocketLauncher, launcherParams);  
  136.         }  
  137.     }  
  138.   
  139.     /** 
  140.      * 将火箭发射台从屏幕上移除。 
  141.      */  
  142.     public static void removeLauncher(Context context) {  
  143.         if (rocketLauncher != null) {  
  144.             WindowManager windowManager = getWindowManager(context);  
  145.             windowManager.removeView(rocketLauncher);  
  146.             rocketLauncher = null;  
  147.         }  
  148.     }  
  149.   
  150.     /** 
  151.      * 更新火箭发射台的显示状态。 
  152.      */  
  153.     public static void updateLauncher() {  
  154.         if (rocketLauncher != null) {  
  155.             rocketLauncher.updateLauncherStatus(isReadyToLaunch());  
  156.         }  
  157.     }  
  158.   
  159.     /** 
  160.      * 更新小悬浮窗的TextView上的数据,显示内存使用的百分比。 
  161.      *  
  162.      * @param context 
  163.      *            可传入应用程序上下文。 
  164.      */  
  165.     public static void updateUsedPercent(Context context) {  
  166.         if (smallWindow != null) {  
  167.             TextView percentView = (TextView) smallWindow  
  168.                     .findViewById(R.id.percent);  
  169.             percentView.setText(getUsedPercentValue(context));  
  170.         }  
  171.     }  
  172.   
  173.     /** 
  174.      * 是否有悬浮窗(包括小悬浮窗和大悬浮窗)显示在屏幕上。 
  175.      *  
  176.      * @return 有悬浮窗显示在桌面上返回true,没有的话返回false。 
  177.      */  
  178.     public static boolean isWindowShowing() {  
  179.         return smallWindow != null || bigWindow != null;  
  180.     }  
  181.   
  182.     /** 
  183.      * 判断小火箭是否准备好发射了。 
  184.      *  
  185.      * @return 当火箭被发到发射台上返回true,否则返回false。 
  186.      */  
  187.     public static boolean isReadyToLaunch() {  
  188.         if ((smallWindowParams.x > launcherParams.x && smallWindowParams.x  
  189.                 + smallWindowParams.width < launcherParams.x  
  190.                 + launcherParams.width)  
  191.                 && (smallWindowParams.y + smallWindowParams.height > launcherParams.y)) {  
  192.             return true;  
  193.         }  
  194.         return false;  
  195.     }  
  196.   
  197.     /** 
  198.      * 如果WindowManager还未创建,则创建一个新的WindowManager返回。否则返回当前已创建的WindowManager。 
  199.      *  
  200.      * @param context 
  201.      *            必须为应用程序的Context. 
  202.      * @return WindowManager的实例,用于控制在屏幕上添加或移除悬浮窗。 
  203.      */  
  204.     private static WindowManager getWindowManager(Context context) {  
  205.         if (mWindowManager == null) {  
  206.             mWindowManager = (WindowManager) context  
  207.                     .getSystemService(Context.WINDOW_SERVICE);  
  208.         }  
  209.         return mWindowManager;  
  210.     }  
  211.   
  212.     /** 
  213.      * 如果ActivityManager还未创建,则创建一个新的ActivityManager返回。否则返回当前已创建的ActivityManager。 
  214.      *  
  215.      * @param context 
  216.      *            可传入应用程序上下文。 
  217.      * @return ActivityManager的实例,用于获取手机可用内存。 
  218.      */  
  219.     private static ActivityManager getActivityManager(Context context) {  
  220.         if (mActivityManager == null) {  
  221.             mActivityManager = (ActivityManager) context  
  222.                     .getSystemService(Context.ACTIVITY_SERVICE);  
  223.         }  
  224.         return mActivityManager;  
  225.     }  
  226.   
  227.     /** 
  228.      * 计算已使用内存的百分比,并返回。 
  229.      *  
  230.      * @param context 
  231.      *            可传入应用程序上下文。 
  232.      * @return 已使用内存的百分比,以字符串形式返回。 
  233.      */  
  234.     public static String getUsedPercentValue(Context context) {  
  235.         String dir = "/proc/meminfo";  
  236.         try {  
  237.             FileReader fr = new FileReader(dir);  
  238.             BufferedReader br = new BufferedReader(fr, 2048);  
  239.             String memoryLine = br.readLine();  
  240.             String subMemoryLine = memoryLine.substring(memoryLine  
  241.                     .indexOf("MemTotal:"));  
  242.             br.close();  
  243.             long totalMemorySize = Integer.parseInt(subMemoryLine.replaceAll(  
  244.                     "\\D+"""));  
  245.             long availableSize = getAvailableMemory(context) / 1024;  
  246.             int percent = (int) ((totalMemorySize - availableSize)  
  247.                     / (float) totalMemorySize * 100);  
  248.             return percent + "%";  
  249.         } catch (IOException e) {  
  250.             e.printStackTrace();  
  251.         }  
  252.         return "悬浮窗";  
  253.     }  
  254.   
  255.     /** 
  256.      * 获取当前可用内存,返回数据以字节为单位。 
  257.      *  
  258.      * @param context 
  259.      *            可传入应用程序上下文。 
  260.      * @return 当前可用内存。 
  261.      */  
  262.     private static long getAvailableMemory(Context context) {  
  263.         ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();  
  264.         getActivityManager(context).getMemoryInfo(mi);  
  265.         return mi.availMem;  
  266.     }  
  267.   
  268. }  
MyWindowManager是所有桌面悬浮窗的管理器,这里我们主要添加了createLauncher()、removeLauncher()和updateLauncher()这几个方法,分别用于创建、移除、以及更新火箭发射台悬浮窗。另外还添加了isReadyToLaunch()这个方法,它是用于判断小火箭是否已经拖动到火箭发射台上了。判断的方式当然也很简单,只需要对小火箭的边界和火箭发射台的边界进行检测,判断它们是否相交就行了。

接下来还需要修改FloatWindowSmallView中的代码,当手指拖动悬浮窗的时候要将它变成小火箭,如下所示:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public class FloatWindowSmallView extends LinearLayout {  
  2.   
  3.     /** 
  4.      * 记录小悬浮窗的宽度 
  5.      */  
  6.     public static int windowViewWidth;  
  7.   
  8.     /** 
  9.      * 记录小悬浮窗的高度 
  10.      */  
  11.     public static int windowViewHeight;  
  12.   
  13.     /** 
  14.      * 记录系统状态栏的高度 
  15.      */  
  16.     private static int statusBarHeight;  
  17.   
  18.     /** 
  19.      * 用于更新小悬浮窗的位置 
  20.      */  
  21.     private WindowManager windowManager;  
  22.   
  23.     /** 
  24.      * 小悬浮窗的布局 
  25.      */  
  26.     private LinearLayout smallWindowLayout;  
  27.   
  28.     /** 
  29.      * 小火箭控件 
  30.      */  
  31.     private ImageView rocketImg;  
  32.   
  33.     /** 
  34.      * 小悬浮窗的参数 
  35.      */  
  36.     private WindowManager.LayoutParams mParams;  
  37.   
  38.     /** 
  39.      * 记录当前手指位置在屏幕上的横坐标值 
  40.      */  
  41.     private float xInScreen;  
  42.   
  43.     /** 
  44.      * 记录当前手指位置在屏幕上的纵坐标值 
  45.      */  
  46.     private float yInScreen;  
  47.   
  48.     /** 
  49.      * 记录手指按下时在屏幕上的横坐标的值 
  50.      */  
  51.     private float xDownInScreen;  
  52.   
  53.     /** 
  54.      * 记录手指按下时在屏幕上的纵坐标的值 
  55.      */  
  56.     private float yDownInScreen;  
  57.   
  58.     /** 
  59.      * 记录手指按下时在小悬浮窗的View上的横坐标的值 
  60.      */  
  61.     private float xInView;  
  62.   
  63.     /** 
  64.      * 记录手指按下时在小悬浮窗的View上的纵坐标的值 
  65.      */  
  66.     private float yInView;  
  67.   
  68.     /** 
  69.      * 记录小火箭的宽度 
  70.      */  
  71.     private int rocketWidth;  
  72.   
  73.     /** 
  74.      * 记录小火箭的高度 
  75.      */  
  76.     private int rocketHeight;  
  77.   
  78.     /** 
  79.      * 记录当前手指是否按下 
  80.      */  
  81.     private boolean isPressed;  
  82.   
  83.     public FloatWindowSmallView(Context context) {  
  84.         super(context);  
  85.         windowManager = (WindowManager) context  
  86.                 .getSystemService(Context.WINDOW_SERVICE);  
  87.         LayoutInflater.from(context).inflate(R.layout.float_window_small, this);  
  88.         smallWindowLayout = (LinearLayout) findViewById(R.id.small_window_layout);  
  89.         windowViewWidth = smallWindowLayout.getLayoutParams().width;  
  90.         windowViewHeight = smallWindowLayout.getLayoutParams().height;  
  91.         rocketImg = (ImageView) findViewById(R.id.rocket_img);  
  92.         rocketWidth = rocketImg.getLayoutParams().width;  
  93.         rocketHeight = rocketImg.getLayoutParams().height;  
  94.         TextView percentView = (TextView) findViewById(R.id.percent);  
  95.         percentView.setText(MyWindowManager.getUsedPercentValue(context));  
  96.     }  
  97.   
  98.     @Override  
  99.     public boolean onTouchEvent(MotionEvent event) {  
  100.         switch (event.getAction()) {  
  101.         case MotionEvent.ACTION_DOWN:  
  102.             isPressed = true;  
  103.             // 手指按下时记录必要数据,纵坐标的值都需要减去状态栏高度  
  104.             xInView = event.getX();  
  105.             yInView = event.getY();  
  106.             xDownInScreen = event.getRawX();  
  107.             yDownInScreen = event.getRawY() - getStatusBarHeight();  
  108.             xInScreen = event.getRawX();  
  109.             yInScreen = event.getRawY() - getStatusBarHeight();  
  110.             break;  
  111.         case MotionEvent.ACTION_MOVE:  
  112.             xInScreen = event.getRawX();  
  113.             yInScreen = event.getRawY() - getStatusBarHeight();  
  114.             // 手指移动的时候更新小悬浮窗的状态和位置  
  115.             updateViewStatus();  
  116.             updateViewPosition();  
  117.             break;  
  118.         case MotionEvent.ACTION_UP:  
  119.             isPressed = false;  
  120.             if (MyWindowManager.isReadyToLaunch()) {  
  121.                 launchRocket();  
  122.             } else {  
  123.                 updateViewStatus();  
  124.                 // 如果手指离开屏幕时,xDownInScreen和xInScreen相等,且yDownInScreen和yInScreen相等,则视为触发了单击事件。  
  125.                 if (xDownInScreen == xInScreen && yDownInScreen == yInScreen) {  
  126.                     openBigWindow();  
  127.                 }  
  128.             }  
  129.             break;  
  130.         default:  
  131.             break;  
  132.         }  
  133.         return true;  
  134.     }  
  135.   
  136.     /** 
  137.      * 将小悬浮窗的参数传入,用于更新小悬浮窗的位置。 
  138.      *  
  139.      * @param params 
  140.      *            小悬浮窗的参数 
  141.      */  
  142.     public void setParams(WindowManager.LayoutParams params) {  
  143.         mParams = params;  
  144.     }  
  145.   
  146.     /** 
  147.      * 用于发射小火箭。 
  148.      */  
  149.     private void launchRocket() {  
  150.         MyWindowManager.removeLauncher(getContext());  
  151.         new LaunchTask().execute();  
  152.     }  
  153.   
  154.     /** 
  155.      * 更新小悬浮窗在屏幕中的位置。 
  156.      */  
  157.     private void updateViewPosition() {  
  158.         mParams.x = (int) (xInScreen - xInView);  
  159.         mParams.y = (int) (yInScreen - yInView);  
  160.         windowManager.updateViewLayout(this, mParams);  
  161.         MyWindowManager.updateLauncher();  
  162.     }  
  163.   
  164.     /** 
  165.      * 更新View的显示状态,判断是显示悬浮窗还是小火箭。 
  166.      */  
  167.     private void updateViewStatus() {  
  168.         if (isPressed && rocketImg.getVisibility() != View.VISIBLE) {  
  169.             mParams.width = rocketWidth;  
  170.             mParams.height = rocketHeight;  
  171.             windowManager.updateViewLayout(this, mParams);  
  172.             smallWindowLayout.setVisibility(View.GONE);  
  173.             rocketImg.setVisibility(View.VISIBLE);  
  174.             MyWindowManager.createLauncher(getContext());  
  175.         } else if (!isPressed) {  
  176.             mParams.width = windowViewWidth;  
  177.             mParams.height = windowViewHeight;  
  178.             windowManager.updateViewLayout(this, mParams);  
  179.             smallWindowLayout.setVisibility(View.VISIBLE);  
  180.             rocketImg.setVisibility(View.GONE);  
  181.             MyWindowManager.removeLauncher(getContext());  
  182.         }  
  183.     }  
  184.   
  185.     /** 
  186.      * 打开大悬浮窗,同时关闭小悬浮窗。 
  187.      */  
  188.     private void openBigWindow() {  
  189.         MyWindowManager.createBigWindow(getContext());  
  190.         MyWindowManager.removeSmallWindow(getContext());  
  191.     }  
  192.   
  193.     /** 
  194.      * 用于获取状态栏的高度。 
  195.      *  
  196.      * @return 返回状态栏高度的像素值。 
  197.      */  
  198.     private int getStatusBarHeight() {  
  199.         if (statusBarHeight == 0) {  
  200.             try {  
  201.                 Class<?> c = Class.forName("com.android.internal.R$dimen");  
  202.                 Object o = c.newInstance();  
  203.                 Field field = c.getField("status_bar_height");  
  204.                 int x = (Integer) field.get(o);  
  205.                 statusBarHeight = getResources().getDimensionPixelSize(x);  
  206.             } catch (Exception e) {  
  207.                 e.printStackTrace();  
  208.             }  
  209.         }  
  210.         return statusBarHeight;  
  211.     }  
  212.   
  213.     /** 
  214.      * 开始执行发射小火箭的任务。 
  215.      *  
  216.      * @author guolin 
  217.      */  
  218.     class LaunchTask extends AsyncTask<Void, Void, Void> {  
  219.   
  220.         @Override  
  221.         protected Void doInBackground(Void... params) {  
  222.             // 在这里对小火箭的位置进行改变,从而产生火箭升空的效果  
  223.             while (mParams.y > 0) {  
  224.                 mParams.y = mParams.y - 10;  
  225.                 publishProgress();  
  226.                 try {  
  227.                     Thread.sleep(8);  
  228.                 } catch (InterruptedException e) {  
  229.                     e.printStackTrace();  
  230.                 }  
  231.             }  
  232.             return null;  
  233.         }  
  234.   
  235.         @Override  
  236.         protected void onProgressUpdate(Void... values) {  
  237.             windowManager.updateViewLayout(FloatWindowSmallView.this, mParams);  
  238.         }  
  239.   
  240.         @Override  
  241.         protected void onPostExecute(Void result) {  
  242.             // 火箭升空结束后,回归到悬浮窗状态  
  243.             updateViewStatus();  
  244.             mParams.x = (int) (xDownInScreen - xInView);  
  245.             mParams.y = (int) (yDownInScreen - yInView);  
  246.             windowManager.updateViewLayout(FloatWindowSmallView.this, mParams);  
  247.         }  
  248.   
  249.     }  
  250.   
  251. }  
这里在代码中添加了一个isPressed标识位,用于判断用户是否正在拖动悬浮窗。当拖动的时候就调用updateViewStatus()方法来更新悬浮窗的显示状态,这时悬浮窗就会变成一个小火箭。然后当手指离开屏幕的时候,也会调用updateViewStatus()方法,这时发现isPressed为false,就会将悬浮窗重新显示出来。

同时,当手指离开屏幕的时候,还会调用MyWindowManager的isReadyToLaunch()方法来判断小火箭是否被拖动到火箭发射台上了,如果为true,就会触发火箭升空的动画效果。火箭升空的动画实现是写在LaunchTask这个任务里的,可以看到,这里会在doInBackground()方法中执行耗时逻辑,将小火箭的纵坐标不断减小,以让它实现上升的效果。当纵坐标减小到0的时候,火箭升空的动画就结束了,然后在onPostExecute()方法中重新将悬浮窗显示出来。

另外,在AndroidManifest.xml文件中记得要声明两个权限,如下所示:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />  
  2. <uses-permission android:name="android.permission.GET_TASKS" />  
代码就只有这么多,接下来我们运行一下看看效果吧。在主界面点击Start Float Window按钮可以开启悬浮窗并回到桌面,然后拖动悬浮窗后就会变成小火箭的状态,将它拖动到屏幕底部火箭发射台上,然后放手,小火箭就会腾空而起了,如下图所示:

好了,今天的讲解就到这里,伴随着小火箭的起飞,我今年的最后一篇文章也结束了。

新的一年即将来临,祝愿大家在未来的一年里,无论是工作还是学习,都能像这个小火箭一样,腾飞起来,达到一个新的高度!2014年,我们继续共同努力!

源码下载,请点击这里
0 0