Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 仿 iPhone 通知角標實作(二):用 Canvas 直接在圖片上畫數字

編輯:關於 Android 程式設計

延續上一篇用 BadgeView 的做法,這次要談另一條路:完全不依賴第三方元件,直接把數字畫進 Bitmap 裡。原理是建立一張與原圖同尺寸的新 Bitmap,取得它的 Canvas,先畫上原始圖示,再疊上數字,最後把新圖塞進 ImageView。

Java 程式碼

package cc.testtipmessage1;

import android.os.Bundle;
import android.widget.ImageView;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;

/**
 * Demo 描述:
 * 仿 iPhone 通知角標的實現,借以提示用戶有幾條新的資訊
 *
 * 參考資料:
 * https://blog.csdn.net/t12x3456/article/details/9337561
 * Thank you very much
 */
public class MainActivity extends Activity {

    private ImageView mImageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        init();
    }

    private void init() {
        mImageView = (ImageView) findViewById(R.id.imageView);
        Bitmap rawBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
        Bitmap newBitmap = createNewBitmap(rawBitmap);
        mImageView.setImageBitmap(newBitmap);
    }

    private Bitmap createNewBitmap(Bitmap rawBitmap) {
        // 新建畫布
        int width = rawBitmap.getWidth();
        int height = rawBitmap.getHeight();
        Bitmap newBitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
        Canvas canvas = new Canvas(newBitmap);

        // ---> 先畫原來的圖片
        Paint bitmapPaint = new Paint();
        bitmapPaint.setDither(true);        // 防止抖動
        bitmapPaint.setFilterBitmap(true);  // 對 Bitmap 進行濾波處理
        Rect src = new Rect(0, 0, rawBitmap.getWidth(), rawBitmap.getHeight());
        Rect dst = new Rect(0, 0, newBitmap.getWidth(), newBitmap.getHeight());
        canvas.drawBitmap(rawBitmap, src, dst, bitmapPaint);

        // ---> 再畫新加的數字
        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("5", 18, 25, countPaint);

        return newBitmap;
    }
}

版面配置

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="100dip"
        android:layout_height="100dip"
        android:layout_centerInParent="true" />

</RelativeLayout>

關鍵重點說明

為什麼要開新 Bitmap 而不是直接在原圖上畫。原圖是從資源檔 decode 出來的,若直接改它會污染記憶體中的共用資源物件,導致其他地方用到同一張圖時也一併被改掉。建立新 Bitmap 才是安全做法。

setDither 與 setFilterBitmap。Dither 是抖動處理,在 16 位色深的裝置上能讓漸層看起來更平滑,避免出現色帶。FilterBitmap 則是開啟雙線性濾波,縮放時邊緣比較不會鋸齒。兩者在這裡主要是畫面品質的保險。

drawText 的座標意義。canvas.drawText("5", 18, 25, paint) 中的 (18, 25) 指的是文字基線(baseline)的左上角,不是字的外框左上角。因此實際視覺位置會比數字看起來更偏右上,需要依字級微調。若要精準定位,可先用 Paint.getTextBounds() 量出字寬字高再計算。

這種做法的限制。數字是直接燒進像素的,一旦數量變動就得重新產生一張 Bitmap,無法像 BadgeView 那樣只更新文字。若角標需要頻繁更新,效能與記憶體都會吃 machi。因此這套做法比較適合「數字固定」或「更新頻率很低」的情境,例如常駐的等級標記。

延伸:加上紅色圓形底襯

真正的 iOS 角標是紅底白字的圓形徽章。想更接近原貌,可以在畫數字之前先畫一個圓:

countPaint.setStyle(Paint.Style.FILL);
countPaint.setColor(Color.RED);
canvas.drawCircle(width - 20, 20, 18, countPaint);

countPaint.setColor(Color.WHITE);
countPaint.setTextAlign(Paint.Align.CENTER);
canvas.drawText("5", width - 20, 27, countPaint);

用 Paint.Align.CENTER 搭配圓心座標,就不用自己推算文字寬度了。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved