常用控件说了不少,现在说说手机开发中也常用到的画图。要掌握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的源码:
01.
<? xml version = "1.0" encoding = "utf-8" ?>
02.
< LinearLayout xmlns:android = "[url=http://schemas.android.com/apk/res/android ]http://schemas.android.com/apk/res/android [/url]"
03.
android:orientation = "vertical"
04.
android:layout_width = "fill_parent"
05.
android:layout_height = "fill_parent"
06.
>
07.
< Button android:id = "@+id/Button01" android:layout_width = "fill_parent" android:layout_height = "44px" android:text = "显示资源图片" ></ Button >
08.
< Button android:id = "@+id/Button02" android:layout_width = "fill_parent" android:layout_height = "44px" android:text = "显示并绘画资源图片" ></ Button >
09.
< 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.
</ LinearLayout >
01.
package
com.testDraw;
02.
import
android.app.Activity;
03.
import
android.content.res.Resources;
04.
import
android.graphics.Bitmap;
05.
import
android.graphics.Bitmap.Config;
06.
import
android.graphics.BitmapFactory;
07.
import
android.graphics.Canvas;
08.
import
android.graphics.Color;
09.
import
android.graphics.Paint;
10.
import
android.graphics.Typeface;
11.
import
android.graphics.drawable.BitmapDrawable;
12.
import
android.graphics.drawable.Drawable;
13.
import
android.os.Bundle;
14.
import
android.view.View;
15.
import
android.widget.Button;
16.
import
android.widget.ImageView;
17.
public
class testDraw extends Activity {
18.
19.
ImageView iv;
20.
Button btn1,btn2,btn3,btn4;
21.
Resources r;
22.
@Override
23.
public void onCreate(Bundle savedInstanceState) {
24.
super .onCreate (savedInstanceState);
25.
setContentView(R.layout.main);
26.
iv=(ImageView) this .findViewById (R.id.ImageView01);
27.
btn1=(Button) this .findViewById (R.id.Button01);
28.
btn2=(Button) this .findViewById (R.id.Button02);
29.
btn3=(Button) this .findViewById (R.id.Button03);
30.
btn1.setOnClickListener( new ClickEvent());
31.
btn2.setOnClickListener( new ClickEvent());
32.
btn3.setOnClickListener( new ClickEvent());
33.
34.
r = this .getResources();
35.
36.
}
37.
class ClickEvent implements View.OnClickListener {
38.
public void onClick(View v) {
39.
if (v==btn1) //显示资源图片
40.
{ //功能等效
41.
//iv.setBackgroundResource(R.drawable.icon);//打开资源图片
42.
Bitmap bmp=BitmapFactory.decodeResource(r, R.drawable.icon); //打开资源图片
43.
iv.setImageBitmap(bmp);
44.
}
45.
else if (v==btn2) //显示并绘画资源图片
46.
{
47.
Bitmap bmp=BitmapFactory.decodeResource(r, R.drawable.icon); //只读,不能直接在bmp上画
48.
Bitmap newb = Bitmap.createBitmap( 300 , 300 , Config.ARGB_8888 );
49.
50.
Canvas canvasTemp = new Canvas( newb );
51.
canvasTemp.drawColor(Color.TRANSPARENT);
52.
53.
Paint p = new Paint();
54.
String familyName = "宋体" ;
55.
Typeface font = Typeface.create(familyName,Typeface.BOLD);
56.
p.setColor(Color.RED);
57.
p.setTypeface(font);
58.
p.setTextSize( 22 );
59.
canvasTemp.drawText( "写字。。。" , 50 , 50 ,p);
60.
canvasTemp.drawBitmap(bmp, 50 , 50 , p); //画图
61.
iv.setImageBitmap(newb);
62.
}
63.
else if (v==btn3) //直接在Button上绘图
64.
{
65.
Bitmap newb = Bitmap.createBitmap( btn3.getWidth(), btn3.getHeight(), Config.ARGB_8888 );
66.
Canvas canvasTemp = new Canvas( newb );
67.
canvasTemp.drawColor(Color.WHITE);
68.
Paint p = new Paint();
69.
String familyName = "宋体" ;
70.
Typeface font = Typeface.create(familyName, Typeface.BOLD);
71.
p.setColor(Color.RED);
72.
p.setTypeface(font);
73.
p.setTextSize( 20 );
74.
canvasTemp.drawText( "写字。。。" , 30 , 30 , p);
75.
Drawable drawable = new BitmapDrawable(newb);
76.
btn3.setBackgroundDrawable(drawable);
77.
}
78.
}
79.
80.
}
81.
}
本文介绍Android开发中的绘图技巧,包括使用Bitmap、Canvas和Paint在ImageView及Button上绘制图形和文字,展示了如何从资源加载图片并进行自定义绘制。
2981

被折叠的 条评论
为什么被折叠?



