Android入門之畫圖詳解
前文常用的控件介紹了不少,現(xiàn)在就來討論一下手機開發(fā)中常用到的畫圖。要掌握Android的畫圖,首先就要了解一下,基本用到的如下一些圖形接口:
1.Bitmap,可以來自資源/文件,也可以在程序中創(chuàng)建,實際上的功能相當于圖片的存儲空間;
2.Canvas,緊密與Bitmap聯(lián)系,把Bitmap比喻內容的話,那么Canvas就是提供了眾多方法操作Bitamp的平臺;
3.Paint,與Canvas緊密聯(lián)系,是"畫板"上的筆刷工具,也用于設置View控件上的樣式;
4.Drawable,如果說前三者是看不見地在內存中畫圖,那么Drawable就是把前三者繪圖結果表現(xiàn)出來的接口。Drawable多個子類,例如:位圖(BitmapDrawable)、圖形(ShapeDrawable)、圖層(LayerDrawable)等。
本文主要講解如何在ImageView畫圖,以及如何直接在Button(繼承View的控件)上面繪制自定義圖像。如下圖所示:
直接把資源圖片畫出來:
在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 NestedScrolling嵌套滾動的示例代碼
這篇文章主要介紹了Android NestedScrolling嵌套滾動的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05android ListView和ProgressBar(進度條控件)的使用方法
這篇文章主要介紹了android ListView控件的使用方法和ProgressBar(進度條控件)的使用方法,代碼大家可以參考使用2013-11-11Android studio 引用aar 進行java開發(fā)的操作步驟
這篇文章主要介紹了Android studio 引用aar 進行java開發(fā)的操作步驟,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-09-09解決Android應用冷啟動時出現(xiàn)的白屏問題的方法
本篇文章主要介紹了解決Android應用冷啟動時出現(xiàn)的白屏問題的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08