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

Android 畫圖入門:Bitmap、Canvas 與 Paint 的三角關係

編輯:Android 開發實例

前面介紹了不少常用控件,這次換個方向,談談自繪圖形。要在 Android 上畫圖,先搞懂四個基礎角色:

  1. Bitmap——可以來自資源或檔案,也能在程式中建立,本質上就是記憶體裡一塊像素儲存空間。
  2. Canvas——與 Bitmap 緊密搭配。若把 Bitmap 比喻成畫布上的內容,Canvas 就是提供各種繪製方法的操作平台。
  3. Paint——Canvas 上的筆刷,負責設定顏色、字型、線寬、樣式等繪圖屬性。
  4. Drawable——前三者在記憶體中完成繪製,Drawable 則是把結果呈現在畫面上的接口。它有多個子類:BitmapDrawable(位圖)、ShapeDrawable(圖形)、LayerDrawable(圖層)等。

本文示範三件事:把資源圖片直接顯示出來、在 ImageView 上畫圖並寫字、以及在按鈕背景上直接作畫。

直接把資源圖片畫出來:

在 ImageView 上畫圖以及寫字:

版面檔 main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <Button android:id="@+id/Button01"
        android:layout_width="fill_parent" android:layout_height="44px"
        android:text="顯示資源圖片"></Button>
    <Button android:id="@+id/Button02"
        android:layout_width="fill_parent" android:layout_height="44px"
        android:text="顯示並繪畫資源圖片"></Button>
    <Button android:id="@+id/Button03"
        android:layout_height="44px" android:layout_width="fill_parent"
        android:text="在控件上繪圖"></Button>
    <ImageView android:id="@+id/ImageView01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"></ImageView>
</LinearLayout>

Java 程式碼

package com.testDraw;

import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class testDraw extends Activity {
    ImageView iv;
    Button btn1, btn2, btn3;
    Resources r;

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

        iv = (ImageView) this.findViewById(R.id.ImageView01);
        btn1 = (Button) this.findViewById(R.id.Button01);
        btn2 = (Button) this.findViewById(R.id.Button02);
        btn3 = (Button) this.findViewById(R.id.Button03);

        btn1.setOnClickListener(new ClickEvent());
        btn2.setOnClickListener(new ClickEvent());
        btn3.setOnClickListener(new ClickEvent());

        r = this.getResources();
    }

    class ClickEvent implements View.OnClickListener {
        public void onClick(View v) {
            if (v == btn1) {
                // 顯示資源圖片
                Bitmap bmp = BitmapFactory.decodeResource(r, R.drawable.icon);
                iv.setImageBitmap(bmp);
            } else if (v == btn2) {
                // 顯示並在圖片上作畫
                Bitmap bmp = BitmapFactory.decodeResource(r, R.drawable.icon);
                // 原 bmp 是唯讀的,必須另建一張可寫的 Bitmap
                Bitmap newb = Bitmap.createBitmap(300, 300, Config.ARGB_8888);
                Canvas canvasTemp = new Canvas(newb);
                canvasTemp.drawColor(Color.TRANSPARENT);

                Paint p = new Paint();
                Typeface font = Typeface.create("宋體", Typeface.BOLD);
                p.setColor(Color.RED);
                p.setTypeface(font);
                p.setTextSize(22);
                canvasTemp.drawText("寫字。。。", 50, 50, p);
                canvasTemp.drawBitmap(bmp, 50, 50, p);

                iv.setImageBitmap(newb);
            } else if (v == btn3) {
                // 直接在 Button 背景上繪圖
                Bitmap newb = Bitmap.createBitmap(
                        btn3.getWidth(), btn3.getHeight(), Config.ARGB_8888);
                Canvas canvasTemp = new Canvas(newb);
                canvasTemp.drawColor(Color.WHITE);

                Paint p = new Paint();
                Typeface font = Typeface.create("宋體", Typeface.BOLD);
                p.setColor(Color.RED);
                p.setTypeface(font);
                p.setTextSize(20);
                canvasTemp.drawText("寫字。。。", 30, 30, p);

                Drawable drawable = new BitmapDrawable(newb);
                btn3.setBackgroundDrawable(drawable);
            }
        }
    }
}

三個容易忽略的重點

decodeResource 回傳的 Bitmap 是不可變的。程式碼中 btn2 那段的註解寫得很清楚:不能直接在 decode 出來的 bmp 上畫,必須用 Bitmap.createBitmap() 另開一張 ARGB_8888 的可變 Bitmap,再把原圖 drawBitmap 進去。少了這步會直接拋 IllegalStateException: Immutable bitmap。

Canvas 必須綁定一張 Bitmap。new Canvas(bitmap) 之後,所有繪製指令的結果都會寫進那張 Bitmap;不指定 Bitmap 的 Canvas 只能用來當作 View 的 drawing surface,兩者用途不同。

btn3 那段有個時機問題。在 onCreate 階段直接抓 btn3.getWidth() 會得到 0,因為版面尚未完成 measure/layout。範例中之所以能跑,是因為它寫在點擊事件裡,此時按鈕早已完成配置。若要在開場就作畫,得改用 View.post() 或 ViewTreeObserver 等版面完成後再執行。

最後提一下 API 演進:setBackgroundDrawable() 在 API 16 之後已被 setBackground() 取代,兩者效果相同,新專案用後者即可。

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