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

Android 中 Bitmap 與 Drawable 詳解

編輯:關於 Android 程式設計

一、相關概念

  • Drawable:一個「可畫的對象」,它可能是一張位圖(BitmapDrawable)、一個圖形(ShapeDrawable),也可能是一個圖層(LayerDrawable)。我們根據需求創建相應的可畫對象。
  • Canvas:畫布,繪圖的目標區域,用於承載繪製操作。
  • Bitmap:位圖,主要用於圖像的處理。
  • Matrix:矩陣,常用於圖片的平移、旋轉、縮放等變換。

二、Bitmap 常用操作

1. 從資源中獲取 Bitmap

Resources res = getResources();
Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.icon);

2. Bitmap 轉 byte[]

public byte[] Bitmap2Bytes(Bitmap bm) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
    return baos.toByteArray();
}

3. byte[] 轉 Bitmap

public Bitmap Bytes2Bimap(byte[] b) {
    if (b.length != 0) {
        return BitmapFactory.decodeByteArray(b, 0, b.length);
    } else {
        return null;
    }
}

4. Bitmap 縮放

public static Bitmap zoomBitmap(Bitmap bitmap, int width, int height) {
    int w = bitmap.getWidth();
    int h = bitmap.getHeight();
    Matrix matrix = new Matrix();
    float scaleWidth = ((float) width / w);
    float scaleHeight = ((float) height / h);
    matrix.postScale(scaleWidth, scaleHeight);
    Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);
    return newbmp;
}

5. Drawable 轉 Bitmap

public static Bitmap drawableToBitmap(Drawable drawable) {
    // 取 drawable 的長寬
    int w = drawable.getIntrinsicWidth();
    int h = drawable.getIntrinsicHeight();
    // 取 drawable 的顏色格式
    Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE
            ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
    // 建立對應 bitmap
    Bitmap bitmap = Bitmap.createBitmap(w, h, config);
    // 建立對應 bitmap 的畫布
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, w, h);
    // 把 drawable 內容畫到畫布中
    drawable.draw(canvas);
    return bitmap;
}

6. 獲得圓角圖片

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {
    int w = bitmap.getWidth();
    int h = bitmap.getHeight();
    Bitmap output = Bitmap.createBitmap(w, h, Config.ARGB_8888);
    Canvas canvas = new Canvas(output);
    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, w, h);
    final RectF rectF = new RectF(rect);
    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);
    return output;
}

7. 獲得帶倒影的圖片

public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap) {
    final int reflectionGap = 4;
    int w = bitmap.getWidth();
    int h = bitmap.getHeight();
    Matrix matrix = new Matrix();
    matrix.preScale(1, -1);
    Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, h / 2, w, h / 2, matrix, false);
    Bitmap bitmapWithReflection = Bitmap.createBitmap(w, (h + h / 2), Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmapWithReflection);
    canvas.drawBitmap(bitmap, 0, 0, null);
    Paint deafalutPaint = new Paint();
    canvas.drawRect(0, h, w, h + reflectionGap, deafalutPaint);
    canvas.drawBitmap(reflectionImage, 0, h + reflectionGap, null);
    Paint paint = new Paint();
    LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0,
            bitmapWithReflection.getHeight() + reflectionGap,
            0x70ffffff, 0x00ffffff, TileMode.CLAMP);
    paint.setShader(shader);
    // Set the Transfer mode to be porter duff and destination in
    paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
    // Draw a rectangle using the paint with our linear gradient
    canvas.drawRect(0, h, w, bitmapWithReflection.getHeight() + reflectionGap, paint);
    return bitmapWithReflection;
}

三、Drawable 常用操作

1. Bitmap 轉 Drawable

Bitmap bm = xxx; // xxx 根據你的情況獲取
BitmapDrawable bd = new BitmapDrawable(getResources(), bm);
// 因為 BitmapDrawable 是 Drawable 的子類,最終直接使用 bd 對象即可。

2. Drawable 縮放

public static Drawable zoomDrawable(Drawable drawable, int w, int h) {
    int width = drawable.getIntrinsicWidth();
    int height = drawable.getIntrinsicHeight();
    // drawable 轉換成 bitmap
    Bitmap oldbmp = drawableToBitmap(drawable);
    // 創建操作圖片用的 Matrix 對象
    Matrix matrix = new Matrix();
    // 計算縮放比例
    float sx = ((float) w / width);
    float sy = ((float) h / height);
    // 設置縮放比例
    matrix.postScale(sx, sy);
    // 建立新的 bitmap,其內容是對原 bitmap 的縮放後的圖
    Bitmap newbmp = Bitmap.createBitmap(oldbmp, 0, 0, width, height, matrix, true);
    return new BitmapDrawable(newbmp);
}
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved