Android入門教程(二十五)——之畫圖)

來源:互聯網
上載者:User

本文來自http://blog.csdn.net/hellogv/ ,引用必須註明出處!

        常用控制項說了不少,現在說說手機開發中也常用到的畫圖。要掌握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 plain copy to clipboard print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://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>  

<?xml version="1.0" encoding="utf-8"?><br /><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:orientation="vertical"<br /> android:layout_width="fill_parent"<br /> android:layout_height="fill_parent"<br /> ><br /><Button android:id="@+id/Button01" android:layout_width="fill_parent" android:layout_height="44px" android:text="顯示資源圖片"></Button><br /><Button android:id="@+id/Button02" android:layout_width="fill_parent" android:layout_height="44px" android:text="顯示並繪畫資源圖片"></Button><br /><Button android:id="@+id/Button03" android:layout_height="44px" android:layout_width="fill_parent" android:text="在控制項上繪圖"></Button><br /><ImageView android:id="@+id/ImageView01" android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView></p><p></LinearLayout><br />

 

 

程式的源碼:

 

view plain copy to clipboard print ?
  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. }  
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.