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

Android 入門第十四篇:圖形繪製四大核心元件

編輯:Android 開發實例

常用元件談得差不多了,這一篇來聊聊手機開發中同樣頻繁的繪圖。想真正掌握 Android 的畫圖能力,得先認識四個基本介面,它們分工明確、彼此緊密咬合。

先搞懂這四個角色

1. Bitmap——可以來自資源或檔案,也可以在程式中即時建立,本質上就是圖片的儲存空間。

2. Canvas——與 Bitmap 關係最為緊密。若把 Bitmap 比喻成畫布上的內容,Canvas 就是那個提供各式各樣操作方法的平台,讓你能在上面畫點、畫線、畫字、貼圖。

3. Paint——同樣與 Canvas 搭配使用,是畫板上的筆刷與顏料,除了決定顏色、粗細、字型,也能用來設定 View 元件的樣式。

4. Drawable——前面三者都在記憶體裡默默作畫,看不見摸不著;Drawable 則是把繪圖結果呈現到畫面上的介面。它有多個子類別,例如位圖(BitmapDrawable)、圖形(ShapeDrawable)、圖層(LayerDrawable)等。

本文的三個示範

接下來會依序示範三種常見場景:把資源圖片原封不動畫出來、在 ImageView 上同時繪圖與寫字、以及直接在繼承自 View 的 Button 背景上作畫。

直接把資源圖片畫出來

在 ImageView 上畫圖以及繪字

直接在控件背景上畫圖

main.xml 的源碼:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" 
  3.     android:orientation="vertical" 
  4.     android:layout_width="fill_parent" 
  5.     android:layout_height="fill_parent" 
  6.     > 
  7. <Button android:id="@+id/Button01" android:layout_width="fill_parent" android:layout_height="44px" android:text="顯示資源圖片"></Button> 
  8. <Button android:id="@+id/Button02" android:layout_width="fill_parent" android:layout_height="44px" android:text="顯示並繪畫資源圖片"></Button> 
  9. <Button android:id="@+id/Button03" android:layout_height="44px" android:layout_width="fill_parent" android:text="在控件上繪圖"></Button> 
  10. <ImageView android:id="@+id/ImageView01" android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView> 
  11.  
  12. </LinearLayout> 

程式碼的兩個重點

第一個關鍵在於「唯讀」這件事。用 BitmapFactory.decodeResource 取回的 Bitmap 是不能直接在上面作畫的,必須另外用 Bitmap.createBitmap 開一塊可寫的新記憶體,再包成 Canvas 才有辦法動筆。示範中先 drawColor(Color.TRANSPARENT) 鋪底,接著用指定字型與紅色畫筆寫字,最後把原圖貼到 (50, 50) 的位置。

第二個關鍵是「畫完之後怎麼掛上去」。畫在 Bitmap 上的結果要顯示到 ImageView,靠的是 setImageBitmap;若要當成 Button 的背景,則得先包成 Drawable,再交給 setBackgroundDrawable。此處刻意用 btn3.getWidth() 與 getHeight() 建立同尺寸畫布,讓自繪的內容剛好鋪滿按鈕。

完整程式碼:

  1. package com.testDraw;     
  2.     
  3. import android.app.Activity;     
  4. import android.content.res.Resources;     
  5. import android.graphics.Bitmap;     
  6. import android.graphics.Bitmap.Config;     
  7. import android.graphics.BitmapFactory;     
  8. import android.graphics.Canvas;     
  9. import android.graphics.Color;     
  10. import android.graphics.Paint;     
  11. import android.graphics.Typeface;     
  12. import android.graphics.drawable.BitmapDrawable;     
  13. import android.graphics.drawable.Drawable;     
  14. import android.os.Bundle;     
  15. import android.view.View;     
  16. import android.widget.Button;     
  17. import android.widget.ImageView;     
  18.     
  19. public class testDraw extends Activity {     
  20.           
  21.     ImageView iv;     
  22.     Button btn1,btn2,btn3,btn4;     
  23.     Resources r;     
  24.     @Override    
  25.     public void onCreate(Bundle savedInstanceState) {     
  26.         super.onCreate(savedInstanceState);     
  27.         setContentView(R.layout.main);     
  28.         iv=(ImageView)this.findViewById(R.id.ImageView01);     
  29.         btn1=(Button)this.findViewById(R.id.Button01);     
  30.         btn2=(Button)this.findViewById(R.id.Button02);     
  31.         btn3=(Button)this.findViewById(R.id.Button03);     
  32.     
  33.         btn1.setOnClickListener(new ClickEvent());     
  34.         btn2.setOnClickListener(new ClickEvent());     
  35.         btn3.setOnClickListener(new ClickEvent());     
  36.              
  37.         r = this.getResources();     
  38.     
  39.           
  40.     }     
  41.     class ClickEvent implements View.OnClickListener {     
  42.     
  43.         public void onClick(View v) {     
  44.             if(v==btn1)//顯示資源圖片     
  45.             {//功能等效     
  46.                 //iv.setBackgroundResource(R.drawable.icon);//打開資源圖片     
  47.                 Bitmap bmp=BitmapFactory.decodeResource(r, R.drawable.icon);//打開資源圖片     
  48.                 iv.setImageBitmap(bmp);     
  49.             }     
  50.             else if(v==btn2)//顯示並繪畫資源圖片     
  51.             {     
  52.                 Bitmap bmp=BitmapFactory.decodeResource(r, R.drawable.icon);//只讀,不能直接在bmp上畫     
  53.                 Bitmap newb = Bitmap.createBitmap( 300, 300, Config.ARGB_8888 );     
  54.                      
  55.                 Canvas canvasTemp = new Canvas( newb );     
  56.                 canvasTemp.drawColor(Color.TRANSPARENT);     
  57.                      
  58.                 Paint p = new Paint();     
  59.                 String familyName ="宋體";     
  60.                 Typeface font = Typeface.create(familyName,Typeface.BOLD);     
  61.                 p.setColor(Color.RED);     
  62.                 p.setTypeface(font);     
  63.                 p.setTextSize(22);     
  64.                 canvasTemp.drawText("寫字。。。",50,50,p);     
  65.                 canvasTemp.drawBitmap(bmp, 50, 50, p);//畫圖     
  66.                 iv.setImageBitmap(newb);     
  67.             }     
  68.             else if(v==btn3)//直接在Button上繪圖     
  69.             {     
  70.                 Bitmap newb = Bitmap.createBitmap( btn3.getWidth(), btn3.getHeight(), Config.ARGB_8888 );     
  71.                 Canvas canvasTemp = new Canvas( newb );     
  72.                 canvasTemp.drawColor(Color.WHITE);     
  73.                 Paint p = new Paint();     
  74.                 String familyName = "宋體";     
  75.                 Typeface font = Typeface.create(familyName, Typeface.BOLD);     
  76.                 p.setColor(Color.RED);     
  77.                 p.setTypeface(font);     
  78.                 p.setTextSize(20);     
  79.                 canvasTemp.drawText("寫字。。。", 30, 30, p);     
  80.                 Drawable drawable = new BitmapDrawable(newb);     
  81.                 btn3.setBackgroundDrawable(drawable);     
  82.             }     
  83.         }     
  84.              
  85.     }     
  86.     
  87. } 
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved