1. 程式人生 > >Android入門之畫圖詳解

Android入門之畫圖詳解

www.666fo.com這篇文章主要介紹了Android入門之畫圖,對Android初學者有很好的學習借鑑價值,需要的朋友可以參考下
前文常用的控制元件介紹了不少,現在就來討論一下手機開發中常用到的畫圖。要掌握Android的畫圖,首先就要了解一下,基本用到的如下一些圖形介面:


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


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


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


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


本文主要講解如何在ImageView畫圖,以及如何直接在Button(繼承View的控制元件)上面繪製自定義影象。如下圖所示:






直接把資源圖片畫出來:
www.123uj.com

 


在ImageView上畫圖以及繪字:






直接在控制元件背景上畫圖:


main.xml的原始碼如下:


<?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>


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,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 canvasTemp = new Canvas( newb );
     canvasTemp.drawColor(Color.TRANSPARENT);
     
     Paint p = new Paint();
     String familyName ="宋體";
     Typeface font = Typeface.create(familyName,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();
  String familyName = "宋體";
  Typeface font = Typeface.create(familyName, 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);
  }
 }
 }
}您可能感興趣的文章:
android影象繪製(四)自定義一個SurfaceView控制元件Android提高之MediaPlayer音視訊播放Android入門之Style與Theme用法例項解析Android入門之ActivityGroup+GridView實現Tab分頁標籤的方法Android入門之Gallery+ImageSwitcher用法例項解析Android入門之Gallery用法例項解析Android入門之TabHost與TabWidget例項解析Android入門之PopupWindow用法例項解析Android入門簡單例項Android提高之SurfaceView與多執行緒的混搭例項
QQ空間 新浪微博 騰訊微博 搜狐微博 人人網 開心網 百度搜藏 更多 0Tags:Android 畫圖 複製連結收藏本文列印本文關閉本文返回首頁 上一篇:Android入門之Gallery+ImageSwitcher用法例項解析下一篇:Android提高之SurfaceView與多執行緒的混搭例項相關文章2013-01-01Android中父View和子view的點選事件處理問題探討2013-02-02Android文字框搜尋和清空效果實現程式碼及簡要概述2013-06-06如何使用Matrix對bitmap的旋轉與映象水平垂直翻轉2012-12-12Android的webview支援HTML5的離線應用功能詳細配置2013-06-06Android圖片處理:識別影象方向並顯示例項教程2012-11-11android 字型顏色選擇器(ColorPicker)介紹2012-11-11Android之PreferenceActivity應用詳解(2)2013-01-01Android開發之圖形影象與動畫(二)Animation實現影象的漸變/縮放/2013-06-06android通過bitmap生成新圖片關鍵性程式碼2013-10-10Handler與Android多執行緒詳解