Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發 >> 初階開發

用 Canvas 與 ImageView 顯示圖片的兩種做法

編輯:初階開發

Android 中顯示圖片的方式很多,最常見的兩條路是「自己畫在 Canvas 上」與「直接塞給 ImageView」。以下用同一張 icon.png 示範兩種寫法。

做法一:用 Canvas 繪製

這種方式完全掌握繪製流程,適合需要自訂排版、疊圖或動畫效果的场景。

package com.ray.draw;

import java.io.InputStream;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;

public class TestDrawBitmap extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new MyView(this));
    }
}

class MyView extends View {

    public MyView(Context context) {
        super(context);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // 把 icon.png 讀進串流
        InputStream is = getResources().openRawResource(R.drawable.icon);
        // 解碼成 Bitmap
        Bitmap mBitmap = BitmapFactory.decodeStream(is);
        Paint mPaint = new Paint();
        // 畫在 (40, 40) 的位置
        canvas.drawBitmap(mBitmap, 40, 40, mPaint);
    }
}

關鍵是覆寫 onDraw(),取得 Canvas 後呼叫 drawBitmap(),座標與 Paint 都由自己決定。

做法二:用 ImageView 顯示

如果只是想單純呈現一張圖,ImageView 省事得多。實務上更常見的場景是從網路抓圖,這時最好先限制大小,避免 OOM:

/**
 * 從 URL 取得 Bitmap,並限制在指定大小以內才載入(預設 200K)
 */
public static Bitmap getURLBitmap(String strurl) {
    int defaultLargestSize = 200 * 1024;   // 預設上限 200K
    URL imageUrl = null;
    Bitmap bitmap = null;
    try {
        imageUrl = new URL(strurl);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
    try {
        HttpGet httpRequest = new HttpGet(imageUrl.toURI());
        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
        HttpEntity entity = response.getEntity();
        BufferedHttpEntity buffHttpEntity = new BufferedHttpEntity(entity);
        if (buffHttpEntity.getContentLength() < defaultLargestSize) {
            InputStream is = buffHttpEntity.getContent();
            bitmap = BitmapFactory.decodeStream(is);
            is.close();
        } else {
            bitmap = null;    // 超過上限就放棄
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return bitmap;
}

實際呼叫與降採樣

抓不到圖時用本地資源當預設值,並透過 inSampleSize 縮小解碼尺寸:

private Bitmap bmp = null;
ImageView mImageView1;

bmp = Global.getURLBitmap(uriPic + photo_path);
if (bmp == null) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 2;
    bmp = BitmapFactory.decodeResource(this.getResources(), R.drawable.notice);
}

mImageView1.setImageBitmap(bmp);

怎麼選?

單純顯示用 ImageView 就夠了,它自帶縮放與資源管理。需要自訂繪製邏輯——例如多層圖層、手勢變換、動畫——才動用 Canvas 自己畫。另外,無論哪種方式,從網路載圖時都記得檢查大小並設定採樣率,否則在記憶體有限的行動裝置上很容易出問題。

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