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

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



思考一下它的实现原理,其实很简单。当我们点击快捷方式时,启动一个背景透明的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
            android:name=".ShortCutActivity"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.Translucent.NoTitleBar">
            <intent-filter>
                <action android:name="com.luyao.shortcut" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
 </activity>

最后不要忘记添加权限:

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,847评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,208评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,587评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,942评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,332评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,587评论 1 218
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,853评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,568评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,273评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,542评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,033评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,373评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,031评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,073评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,830评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,628评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,537评论 2 269

推荐阅读更多精彩内容