2011.09.13(2)——— android 圖標(biāo)上面添加數(shù)字
參考:
http://flysnow.iteye.com/blog/906770
?
?
?
- 新建名為NotificationIconCount的Android Project。
- 首先修改AndroidManifest.xml,加入權(quán)限<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>,因?yàn)槲覀円x取聯(lián)系人。
-
修改main.xml如下,這里定義一個(gè)ImageView,勇于預(yù)覽我們處理好的加上數(shù)字的圖標(biāo)。
?- <? xml ? version = "1.0" ? encoding = "utf-8" ?> ??
- < LinearLayout ? xmlns:android = "http://schemas.android.com/apk/res/android" ??
- ???? android:orientation = "vertical" ??
- ???? android:layout_width = "fill_parent" ??
- ???? android:layout_height = "fill_parent" ??
- ???? > ??
- ???? < ImageView ???
- ???????? android:id = "@+id/icon" ??
- ???????? android:layout_width = "wrap_content" ??
- ???????? android:layout_height = "wrap_content" ??
- ???????? android:scaleType = "center" ??
- ???????? android:adjustViewBounds = "true" /> ??
- </ LinearLayout > ??
-
先來(lái)獲取手機(jī)內(nèi)通訊錄的圖標(biāo),如果沒有獲取到,則使用該應(yīng)用的圖標(biāo).
?這里用到一個(gè)自定義的根據(jù)資源圖標(biāo)id獲取圖片的函數(shù),很簡(jiǎn)單,代碼如下:- super .onCreate(savedInstanceState);??
- ????????setContentView(R.layout.main);??
- ????????mImageView=(ImageView)findViewById(R.id.icon);??
- ???????? //優(yōu)先采用聯(lián)系人的圖標(biāo),如果不存在則采用該應(yīng)用的圖標(biāo) ??
- ????????Drawable?contactIcon;??
- ???????? try ?{??
- ????????????contactIcon?=?getPackageManager().getApplicationIcon( "com.android.contacts" );??
- ????????}? catch ?(NameNotFoundException?e)?{??
- ????????????contactIcon= null ;??
- ????????}??
- ????????Bitmap?icon;??
- ???????? if (contactIcon? instanceof ?BitmapDrawable){??
- ????????????icon=((BitmapDrawable)contactIcon).getBitmap();??
- ????????} else {??
- ????????????icon=getResIcon(getResources(),?R.id.icon);??
- ????????}??
?- /** ?
- ????*?根據(jù)id獲取一個(gè)圖片 ?
- ????*?@param?res ?
- ????*?@param?resId ?
- ????*?@return ?
- ????*/ ??
- ??? private ?Bitmap?getResIcon(Resources?res, int ?resId){??
- ????Drawable?icon=res.getDrawable(resId);??
- ???? if (icon? instanceof ?BitmapDrawable){??
- ????????BitmapDrawable?bd=(BitmapDrawable)icon;??
- ???????? return ?bd.getBitmap();??
- ????} else {??
- ???????? return ? null ;??
- ????}??
- ???}??
-
獲取到圖標(biāo)之后就要對(duì)這個(gè)圖標(biāo)進(jìn)行處理了,要為該圖標(biāo)加上聯(lián)系人數(shù)量的覆蓋,首先我們看獲取聯(lián)系人個(gè)數(shù)的函數(shù)。
?這里采用Uri的方式獲取聯(lián)系人的cursor,然后獲取個(gè)數(shù)。- /** ?
- ?????*?獲取聯(lián)系人的個(gè)數(shù) ?
- ?????*?@return?手里通訊錄中聯(lián)系人的個(gè)數(shù) ?
- ?????*/ ??
- ???? private ? int ?getContactCount(){??
- ????????Cursor?c=getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,? new ?String[]{ContactsContract.Contacts._COUNT},? null ,? null ,? null );??
- ???????? try {??
- ????????????c.moveToFirst();??
- ???????????? return ?c.getInt( 0 );??
- ????????} catch (Exception?e){??
- ???????????? return ? 0 ;??
- ????????} finally {??
- ????????????c.close();??
- ????????}??
- ????}??
-
有了圖標(biāo)和聯(lián)系人個(gè)數(shù)就可以生成帶聯(lián)系人個(gè)數(shù)的圖標(biāo)了,我們看下生成的這個(gè)函數(shù)。
?注釋的很詳細(xì),就不解釋了,無(wú)非就是定義一個(gè)畫布(Canvas),然后在上面畫圖標(biāo),畫數(shù)字文本。- /** ?
- ?????*?在給定的圖片的右上角加上聯(lián)系人數(shù)量。數(shù)量用紅色表示 ?
- ?????*?@param?icon?給定的圖片 ?
- ?????*?@return?帶聯(lián)系人數(shù)量的圖片 ?
- ?????*/ ??
- ???? private ?Bitmap?generatorContactCountIcon(Bitmap?icon){??
- ???????? //初始化畫布 ??
- ???????? int ?iconSize=( int )getResources().getDimension(android.R.dimen.app_icon_size);??
- ????????Log.d(TAG,? "the?icon?size?is?" +iconSize);??
- ????????Bitmap?contactIcon=Bitmap.createBitmap(iconSize,?iconSize,?Config.ARGB_8888);??
- ????????Canvas?canvas= new ?Canvas(contactIcon);??
- ??????????
- ???????? //拷貝圖片 ??
- ????????Paint?iconPaint= new ?Paint();??
- ????????iconPaint.setDither( true ); //防抖動(dòng) ??
- ????????iconPaint.setFilterBitmap( true ); //用來(lái)對(duì)Bitmap進(jìn)行濾波處理,這樣,當(dāng)你選擇Drawable時(shí),會(huì)有抗鋸齒的效果 ??
- ????????Rect?src= new ?Rect( 0 ,? 0 ,?icon.getWidth(),?icon.getHeight());??
- ????????Rect?dst= new ?Rect( 0 ,? 0 ,?iconSize,?iconSize);??
- ????????canvas.drawBitmap(icon,?src,?dst,?iconPaint);??
- ??????????
- ???????? //在圖片上創(chuàng)建一個(gè)覆蓋的聯(lián)系人個(gè)數(shù) ??
- ???????? int ?contacyCount=getContactCount();??
- ???????? //啟用抗鋸齒和使用設(shè)備的文本字距 ??
- ????????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;??
- ????}??
-
然后我們把得到的這個(gè)處理過的Bitmap放在我們?cè)趍ain.xml里定義的ImageView里展示就可以看到效果了.
?- mImageView.setImageBitmap(contactCountIcon);??
-
我們啟動(dòng)應(yīng)用看看效果.
?我們看到了,右上角紅色的1代表我手機(jī)中有一個(gè)聯(lián)系人
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元

