Android中為圖標加上數字--用于未讀短信數提醒,待更新應用數提醒等
|
admin
2014年11月24日 21:56
本文熱度 5785
|
在我們開發一些如短消息、應用商店等應用時,會考慮在短消息的圖標上加上未讀短信的數量,在應用商店上加上可以升級的應用數量,這樣不占太大空間還能達到提示的目的。 本節就以一個展示手機內聯系人數量的例子來演示怎么在一個圖標的上面加上數字的例子,也就是一個數字overlay..做過google map開發的知道overlay這個東西。。
一:你可學到
- 手機內依據Uri查看聯系人
- 權限的添加
- 獲取手機內任意一款應用的應用圖標
- 對圖形的處理,如復制,為圖片加上覆蓋層--數字。
- 使用RemoteView自定義Notification
二:開始行動
- 新建名為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代表我手機中有一個聯系人
三:采用狀態欄通知的辦法展示聯系人的數量
- 如果我們監聽未讀短信的數量,展示在狀態欄通知了,就可以用這個方法,我這里還是以聯系人的數量為例
- 由于Notification默認的icon字段使用的是一個一個資源的引用類型int,所以我們這里采用RemoteView的方式自定義Notification。定義RemoteView,需要一個layout,用于定義展示的Notification樣式,我們新建一個notification.xml布局文件如下:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:padding="3dp"
- >
- <ImageView android:id="@+id/image"
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:layout_marginRight="10dp"
- />
- <TextView android:id="@+id/text"
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:textColor="#000"
- />
- </LinearLayout>
很簡單,一個ImageView和一個TextView,用于顯示提示的圖標和文字。
- 要想使用Notification,我們必須獲取一個Service--NotificationManager,Android中由它來管理我們的Notification,獲取NotificationManager的代碼如下,卸載onCreate方法里
- nm=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
- 然后緊接著我們就調用我們定義好的方法來顯示通知:
- showNotifacation(contactCountIcon);
- 下面我們看一下方法showNotifacation的具體實現:
-
-
-
-
- private void showNotifacation(Bitmap icon){
- Notification notification=new Notification(R.drawable.icon,"聯系人數量",System.currentTimeMillis());
-
- RemoteViews contentView=new RemoteViews(getPackageName(), R.layout.notification);
- contentView.setImageViewBitmap(R.id.image, icon);
- contentView.setTextViewText(R.id.text, "圖標上的紅色數字表示手機中聯系人的數量");
- notification.contentView=contentView;
- Intent notificationIntent=new Intent(this, NotificationIconActivity.class);
- PendingIntent contentIntent=PendingIntent.getActivity(this, 0, notificationIntent, 0);
- notification.contentIntent=contentIntent;
- nm.notify(NOTIFICATION_CONTACT_ID, notification);
- }
注釋的都很詳細,就不一一解釋了
- 最后的就是重寫onDestroy,在Android銷毀該Activity的時候清除我們的通知。
- @Override
- protected void onDestroy() {
- super.onDestroy();
- nm.cancel(NOTIFICATION_CONTACT_ID);
- }
- 運行,可以看到如下效果
 可以看到,效果已經出來了。。
四:小結和一點引申
這結的主要關鍵還是對圖片的處理,比如這里的加上數字,當時你也可以加上其他的東西,Canvas里有很多相應的函數可以使用。。
這里說的都是應用內、狀態通知等一些顯示處理后圖片的辦法,那么有沒有方法處理手機主屏幕上的圖標的辦法,為他們加上數字呢。比如在短信息的應用圖標的右上角加上未讀短信數目等,答案是有的,不過是迂回實現的,給個思路就是使用AppWidget,這個可以實現,還能動態更新,具體怎么實現,大家可以自己摸索下,這里只提供個思路,整體和這節的例子差不多,只不過呢,要做成AppWidget。。。
該文章在 2014/11/24 21:56:52 編輯過
|
|