仿騰訊手機(jī)管家桌面快捷方式極速清理效果

很多手機(jī)管家之類的軟件都會在桌面生成內(nèi)存清理的快捷方式,下圖中是騰訊手機(jī)管家的桌面快捷方式,效果比較酷炫,點(diǎn)擊極速清理,會在快捷方式處產(chǎn)生一系列動畫。



思考一下它的實現(xiàn)原理,其實很簡單。當(dāng)我們點(diǎn)擊快捷方式時,啟動一個背景透明的Activity,找到快捷方式在launcher的位置,在Activity處同樣位置進(jìn)行動畫,就可以實現(xiàn)這個效果了。我們只需要能確定快捷方式圖標(biāo)在桌面的位置就可以了。

創(chuàng)建快捷方式

創(chuàng)建快捷方式很簡單,通過發(fā)送系統(tǒng)廣播的方式來實現(xiàn),直接上代碼:

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);//避免重復(fù)創(chuàng)建,有時無作用
        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);
    }

清單文件中注冊點(diǎn)擊要啟動的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>

最后不要忘記添加權(quán)限:

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

這不是一個危險權(quán)限,不需要做處理。這樣就可以在桌面生成快捷方式了,點(diǎn)擊進(jìn)入一個透明的Activity。

確定ShortCut坐標(biāo)

閱讀下面一段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的點(diǎn)擊事件。著重看一下這幾行代碼:

 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對象。然后就很簡單了,只需要根據(jù)得到的坐標(biāo)在指定位置進(jìn)行布局就可以了。

在啟動的ShortCutActivity的onCreate()方法中,獲取到坐標(biāo)值,進(jìn)行布局:

        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);
    }

這里需要注意的是設(shè)置高度的時候,不要忽略狀態(tài)欄的高度,通過反射可以獲取狀態(tài)欄的高度。這里就演示一個簡單的旋轉(zhuǎn)動畫:

        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) {

            }
        });

最后運(yùn)行效果為:


有任何疑問,歡迎加群討論:261386924

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容