国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android 給應用程序的icon添加未讀消息個數提示(紅圈內數字)

2019-10-23 18:31:52
字體:
來源:轉載
供稿:網友

最近在做一個可以查看未讀消息的功能,需要在界面中的Tab頁的標簽icon的右上角添加一個未讀消息提示的功能。

先上個效果圖出來,比較直觀明白需求:

android,icon未讀數量,未讀消息數字,android圖標未讀消息

思路上似乎有兩種:

 1. 直接把底圖和紅圓圈的圖片用相對布局進行排列,在代碼中動態更改紅圓中的TextView的數字,并且識別一下各種情況下紅圓的顯示或者隱藏。這種方法比較直觀。

 2. 采用canvas畫出圓和數字。

由于項目采用的是Tab頁的形式,其中的RadioButton不適合采用相對布局。故我采用了第二種方式。將繪制的過程寫成了工具方法。在需要的時候調用。便于以后的技術遷移。

直接上代碼:

/**   * 繪制圖標右上角的未讀消息數量顯示   *   * @param context   *   上下文   * @param icon   *   需要被添加的icon的資源ID   * @param news   *   未讀的消息數量   * @return drawable   */  @SuppressWarnings("unused")  public static Drawable displayNewsNumber(Context context, int icon, int news) {   // 初始化畫布   int iconSize = (int) context.getResources().getDimension(     android/138438.html">android.R.dimen.app_icon_size);   // Bitmap contactIcon = Bitmap.createBitmap(iconSize, iconSize,   // Config.ARGB_8888);   Bitmap iconBitmap = BitmapFactory.decodeResource(     context.getResources(), icon);   Canvas canvas = new Canvas(iconBitmap);   // 拷貝圖片   Paint iconPaint = new Paint();   iconPaint.setDither(true);// 防抖動   iconPaint.setFilterBitmap(true);// 用來對Bitmap進行濾波處理   Rect src = new Rect(0, 0, iconBitmap.getWidth(), iconBitmap.getHeight());   Rect dst = new Rect(0, 0, iconBitmap.getWidth(), iconBitmap.getHeight());   canvas.drawBitmap(iconBitmap, src, dst, iconPaint);   // 啟用抗鋸齒和使用設備的文本字距   Paint countPaint = new Paint(Paint.ANTI_ALIAS_FLAG     | Paint.DEV_KERN_TEXT_FLAG);   countPaint.setColor(Color.RED);   canvas.drawCircle(iconSize - 13, 20, 10, countPaint);   Paint textPaint = new Paint();   textPaint.setColor(Color.WHITE);   // textPaint.setTypeface(Typeface.DEFAULT_BOLD);   textPaint.setTextSize(19f);   canvas.drawText(String.valueOf(news), iconSize - 18, 27, textPaint);   return new BitmapDrawable(iconBitmap);  } 

調用語句:

if (count > 0) {//顯示右上角未讀消息提示    Drawable mineDrawable = BitmapUtil.displayNewsNumber(this,      R.drawable.icon_mine, count);    mineButton.setCompoundDrawablesWithIntrinsicBounds(null, null,      null, mineDrawable);//這里就是設置背景,自行發揮。   } else {//隱藏    Drawable defaultDrawable = getResources().getDrawable(      R.drawable.icon_mine);    mineButton.setCompoundDrawablesWithIntrinsicBounds(null, null,      null, defaultDrawable);   } 

注:

1.count是未讀消息的數量。

2.setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom)可以在上、下、左、右設置圖標,如果不想在某個地方顯示,則設置為null。圖標的寬高將會設置為固有寬高,既自動通過getIntrinsicWidth和getIntrinsicHeight獲取。該方法是和在xml中設置 Android:drawableTop="@drawable/icon_mine"的效果一樣。

以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持VEVB武林網!


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 大方县| 洛南县| 汤阴县| 林周县| 金华市| 贵溪市| 宜宾县| 渑池县| 万年县| 平泉县| 会昌县| 班玛县| 怀来县| 高邮市| 青岛市| 北流市| 碌曲县| 新源县| 台北市| 长丰县| 天祝| 汉源县| 蒲江县| 青海省| 祁门县| 鄯善县| 辉县市| 洪雅县| 乌鲁木齐县| 山丹县| 馆陶县| 兴国县| 余江县| 胶南市| 茶陵县| 海盐县| 彭州市| 兴业县| 枞阳县| 金堂县| 汉源县|