仿腾讯手机管家桌面快捷方式极速清理效果

很多手机管家之类的软件都会在桌面生成内存清理的快捷方式,下图中是腾讯手机管家的桌面快捷方式,效果比较酷炫,点击极速清理,会在快捷方式处产生一系列动画。

思考一下它的实现原理,其实很简单。当我们点击快捷方式时,启动一个背景透明的Activity,找到快捷方式在launcher的位置,在Activity处同样位置进行动画,就可以实现这个效果了。我们只需要能确定快捷方式图标在桌面的位置就可以了。

创建快捷方式

创建快捷方式很简单,通过发送系统广播的方式来实现,直接上代码:

private void createShortCut() {
        Intent shortCutIntent = new Intent();
        shortCutIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
        shortCutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "极速清理");
        shortCutIntent.putExtra("duplicate", false);//避免重复创建,有时无作用
        shortCutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON, BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
        Intent i = new Intent();//指定启动的Activity
        i.setAction("com.luyao.shortcut");
        i.addCategory("android.intent.category.DEFAULT");
        shortCutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, i);
        sendBroadcast(shortCutIntent);
    }

清单文件中注册点击要启动的Activity:

 
            
                
                
            
 

最后不要忘记添加权限:

这不是一个危险权限,不需要做处理。这样就可以在桌面生成快捷方式了,点击进入一个透明的Activity。

确定ShortCut坐标

阅读下面一段Launcher.java的源码:

         /**
          * Launches the intent referred by the clicked shortcut.
          *
          * @param v The view representing the clicked shortcut.
          */
        public void onClick(View v) {
                // Make sure that rogue clicks don't get through while allapps is launching, or after the
                // view has detached (it's possible for this to happen if the view is removed mid touch).
                if (v.getWindowToken() == null) {
                        return;
                    }

                if (!mWorkspace.isFinishedSwitchingState()) {
                        return;
                    }

                Object tag = v.getTag();
                if (tag instanceof ShortcutInfo) {
                        // Open shortcut
                        final Intent intent = ((ShortcutInfo) tag).intent;
                        int[] pos = new int[2];
                        v.getLocationOnScreen(pos);
                        intent.setSourceBounds(new Rect(pos[0], pos[1],
                                        pos[0] + v.getWidth(), pos[1] + v.getHeight()));

                        boolean success = startActivitySafely(v, intent, tag);

                        if (success && v instanceof BubbleTextView) {
                                mWaitingForResume = (BubbleTextView) v;
                                mWaitingForResume.setStayPressed(true);
                            }
                    } else if (tag instanceof FolderInfo) {
                        if (v instanceof FolderIcon) {
                                FolderIcon fi = (FolderIcon) v;
                                handleFolderClick(fi);
                            }
                    } else if (v == mAllAppsButton) {
                        if (isAllAppsVisible()) {
                                showWorkspace(true);
                            } else {
                                onClickAllAppsButton(v);
                            }
                    }
            }

这段代码处理了ShortCut的点击事件。着重看一下这几行代码:

 final Intent intent = ((ShortcutInfo) tag).intent;
 int[] pos = new int[2];
 v.getLocationOnScreen(pos);
 intent.setSourceBounds(new Rect(pos[0], pos[1],pos[0] + v.getWidth(), pos[1] + v.getHeight()));
 boolean success = startActivitySafely(v, intent, tag);

这里利用IntentsetSourceBounds()方法将shortcut的位置信息保存到了用来启动Activity的intent中。既然有setSourceBounds(),必然有getSourceBounds(),看一下Intent源码:

    /**
     * Set the bounds of the sender of this intent, in screen coordinates.  This can be
     * used as a hint to the receiver for animations and the like.  Null means that there
     * is no source bounds.
     */
    public void setSourceBounds(Rect r) {
        if (r != null) {
            mSourceBounds = new Rect(r);
        } else {
            mSourceBounds = null;
        }
    }

    /**
     * Get the bounds of the sender of this intent, in screen coordinates.  This can be
     * used as a hint to the receiver for animations and the like.  Null means that there
     * is no source bounds.
     */
    public Rect getSourceBounds() {
        return mSourceBounds;
    }

就是简单的setter/getter方法,通过getSourceBounds()方法我们就可以得到存储着shortcut位置信息的Rect对象。然后就很简单了,只需要根据得到的坐标在指定位置进行布局就可以了。

在启动的ShortCutActivity的onCreate()方法中,获取到坐标值,进行布局:

        rect = getIntent().getSourceBounds();
        if (rect == null) {
            finish();
        } else {
            requestLayout();
        }

       private void requestLayout() {

        int statusBarHeight=0;
        try {
            Class clazz=Class.forName("com.android.internal.R$dimen");
            Object object=clazz.newInstance();
            int height=Integer.parseInt(clazz.getField("status_bar_height").get(object).toString());
            statusBarHeight=getResources().getDimensionPixelOffset(height);
        } catch (Exception e) {
            e.printStackTrace();
        }

        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) icon.getLayoutParams();
        lp.width = rect.width();
        lp.height = rect.height();
        lp.leftMargin = rect.left;
        lp.topMargin = rect.top - statusBarHeight - 20;

        parentView.updateViewLayout(icon, lp);
        parentView.updateViewLayout(rotate, lp);
    }

这里需要注意的是设置高度的时候,不要忽略状态栏的高度,通过反射可以获取状态栏的高度。这里就演示一个简单的旋转动画:

        ObjectAnimator animator = ObjectAnimator.ofFloat(rotate, "rotation", 0f, 360f);
        animator.setRepeatCount(3);
        animator.setInterpolator(new LinearInterpolator());
        animator.setDuration(500);
        animator.start();

        animator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                Toast.makeText(getApplicationContext(),"已为您释放1GB空间!",Toast.LENGTH_SHORT).show();
                finish();
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });

最后运行效果为:

有任何疑问,欢迎加群讨论:261386924

IT文库 » 仿腾讯手机管家桌面快捷方式极速清理效果
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址