2011.09.13(2)——— android 图标上面添加数字
2011.09.13(2)——— android 图标上面添加数字
参考:http://flysnow.iteye.com/blog/906770
?
?
?
- 新建名为NotificationIconCount的Android Project。
- 首先修改AndroidManifest.xml,加入权限<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>,因为我们要读取联系人。
- 修改main.xml如下,这里定义一个ImageView,勇于预览我们处理好的加上数字的图标。
- <?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>??
? - 先来获取手机内通讯录的图标,如果没有获取到,则使用该应用的图标.
- super.onCreate(savedInstanceState);??
- ????????setContentView(R.layout.main);??
- ????????mImageView=(ImageView)findViewById(R.id.icon);??
- ??????????
- ????????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);??
- ????????}??
?这里用到一个自定义的根据资源图标id获取图片的函数,很简单,代码如下:- ?
- ?
- ?
- ?
- ?
- ??
- ???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;??
- ????}??
- ???}??
? - 获取到图标之后就要对这个图标进行处理了,要为该图标加上联系人数量的覆盖,首先我们看获取联系人个数的函数。
- ?
- ?
- ?
- ??
- ????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();??
- ????????}??
- ????}??
?这里采用Uri的方式获取联系人的cursor,然后获取个数。 - 有了图标和联系人个数就可以生成带联系人个数的图标了,我们看下生成的这个函数。
- ?
- ?
- ?
- ?
- ??
- ????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);??
- ????????iconPaint.setFilterBitmap(true);??
- ????????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=getContactCount();??
- ??????????
- ????????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;??
- ????}??
?注释的很详细,就不解释了,无非就是定义一个画布(Canvas),然后在上面画图标,画数字文本。 - 然后我们把得到的这个处理过的Bitmap放在我们在main.xml里定义的ImageView里展示就可以看到效果了.
- mImageView.setImageBitmap(contactCountIcon);??
? - 我们启动应用看看效果.
?我们看到了,右上角红色的1代表我手机中有一个联系人