Android入門第十四篇之畫圖

來源:互聯網
上載者:User

 常用控制項說了不少,現在說說手機開發中也常用到的畫圖。要掌握Android的畫圖,首先就要瞭解一下,基本用到的圖形介面:

1.Bitmap,可以來自資源/檔案,也可以在程式中建立,實際上的功能相當於圖片的儲存空間;

2.Canvas,緊密與Bitmap聯絡,把Bitmap比喻內容的話,那麼Canvas就是提供了眾多方法操作Bitamp的平台;

3.Paint,與Canvas緊密聯絡,是"畫板"上的筆刷工具,也用於設定View控制項上的樣式;

4.Drawable,如果說前三者是看不見地在記憶體中畫圖,那麼Drawable就是把前三者繪圖結果表現出來的介面。Drawable多個子類,例如:位元影像(BitmapDrawable)、圖形(ShapeDrawable)、圖層(LayerDrawable)等。

 

本文主要講解如何在ImageView畫圖,以及如何直接在Button(繼承View的控制項)上面繪製自訂映像。

直接把資源圖片畫出來

 

 

在ImageView上畫圖以及繪字

 

 

 

直接在控制項背景上畫圖

 

main.xml的源碼:

view plaincopy to clipboardprint?
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://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> 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://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>
 

 

程式的源碼:

 view plaincopy to clipboardprint?
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,btn4;  
    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)//顯示資源圖片  
            {//功能等效  
                //iv.setBackgroundResource(R.drawable.icon);//開啟資源圖片  
                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 newb = Bitmap.createBitmap( 300, 300, Config.ARGB_8888 );  
                  
                Canvas

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.