編輯:關於 Android 程式設計
延續上一篇用 BadgeView 的做法,這次要談另一條路:完全不依賴第三方元件,直接把數字畫進 Bitmap 裡。原理是建立一張與原圖同尺寸的新 Bitmap,取得它的 Canvas,先畫上原始圖示,再疊上數字,最後把新圖塞進 ImageView。
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 搭配圓心座標,就不用自己推算文字寬度了。
Android 讀取手機通話記錄的實作方式
在 Android 中,通話記錄屬於系統內容提供者管理的資料。若應用程式需要顯示通話紀錄,通常會透過 ContentResolver 查詢 CallLog,再將 Curs
使用 Android Studio 匯入 Android 源碼
背景 Android 原始碼內有 development/tools/idegen,可產生 IntelliJ 與 Eclipse 專案設定。Android Studio
自定義 ListView 實作側滑刪除:SlideDeleteListView 與 ScrollView 手勢衝突的完整解法
側滑露出刪除按鈕,幾乎成了清單類應用的標準互動。看著簡單,真正動手卻常卡在兩處:手勢判定不夠乾淨,以及 ListView 被放進 ScrollView 之後各種「打架」。
Android TabLayout 結合 ViewPager 實現分頁切換教學
在 Android 中,TabLayout 與 ViewPager 是非常常見的組合,適合用來建立頂部頁籤與可滑動頁面的結構。只要把兩者綁定在一起,就能讓使用者透過點擊頁