android添加捷徑並且表徵圖上添加數字

來源:互聯網
上載者:User

前兩個日誌的合并

我們可以給案頭添加捷徑的時候 制定一個帶數位圖片

 


1、添加一個方法

Java代碼 
private Bitmap generatorContactCountIcon(Bitmap icon){ 
        //初始化畫布 
        int iconSize=(int)getResources().getDimension(android.R.dimen.app_icon_size); 
        Bitmap contactIcon=Bitmap.createBitmap(iconSize, iconSize, Config.ARGB_8888); 
        Canvas canvas=new Canvas(contactIcon); 
         
        //拷貝圖片 
        Paint iconPaint=new Paint(); 
        iconPaint.setDither(true);//防震 
        iconPaint.setFilterBitmap(true);//用來對Bitmap進行濾波處理,這樣,當你選擇Drawable時,會有消除鋸齒的效果 
        Rect src=new Rect(0, 0, icon.getWidth(), icon.getHeight()); 
        Rect dst=new Rect(0, 0, iconSize, iconSize); 
        canvas.drawBitmap(icon, src, dst, iconPaint); 
         
        //在圖片上建立一個覆蓋的連絡人個數 
        int contacyCount=11; 
        //啟用消除鋸齒和使用裝置的文本字距 
        Paint countPaint=new Paint(Paint.ANTI_ALIAS_FLAG|Paint.DEV_KERN_TEXT_FLAG); 
        countPaint.setColor(Color.RED); 
        countPaint.setTextSize(20f); 
        countPaint.setTypeface(Typeface.DEFAULT_BOLD); 
        canvas.drawText(String.valueOf(contacyCount), iconSize-18, 25, countPaint); 
        return contactIcon; 
    } 


上面
Java代碼 
int contacyCount=11; 

這個是寫死的 你可以在程式裡面更具業務來控制

2、修改方法:

Java代碼 
private void installShortCut(){ 
        Intent shortcutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT"); 
        shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name)); 
        // 是否可以有多個捷徑的副本,參數如果是true就可以產生多個捷徑,如果是false就不會重複添加 
        shortcutIntent.putExtra("duplicate", false); 
        Intent mainIntent = new Intent(Intent.ACTION_MAIN); 
        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); 
 
        // 要刪除的應用程式的ComponentName,即應用程式套件組合名+activity的名字 
        //intent2.setComponent(new ComponentName(this.getPackageName(), this.getPackageName() + ".MainActivity")); 
        mainIntent.setClass(this, this.getClass()); 
 
        shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, mainIntent); 
        //shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this, R.drawable.icon)); 
        shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON, generatorContactCountIcon(((BitmapDrawable)(getResources().getDrawable(R.drawable.icon))).getBitmap())); 
        sendBroadcast(shortcutIntent); 
    } 

 

以前是根據id來找到圖片
Java代碼 
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this, R.drawable.icon)); 


現在改為根據圖片:
Java代碼 
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON, generatorContactCountIcon(((BitmapDrawable)(getResources().getDrawable(R.drawable.icon))).getBitmap())); 

作者“TryLoveCatch”
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.