您的位置:首页 > 移动开发 > Android开发

Android应用开发学习笔记之绘图

2013-08-22 19:33 405 查看
作者:刘昊昱

博客:http://blog.csdn.net/liuhaoyutz

一、绘图常用类介绍

在Android中绘图时,常用到的几个类是Paint、Canvas、Bitmap和BitmaptFactory。其中,Paint类代表画笔,Canvas类代表画布。有了Paint和Canvas类就可以进行绘图操作了。

1、 Paint类

Android官方文档中对Paint类的描述如下:

The Paint class holds the styleand color information about how to draw geometries, text and bitmaps.

Paint类代表画笔,用来描述图形的颜色和风格,如线宽、颜色、透明度、填充效果等值。使用Paint类时,首先需要创建Paint类的对象,然后通过该类的成员函数对画笔的默认设置进行更改,例如改变画笔的颜色、线宽等。

例如,要创建一个画笔,设置画笔的颜色为红色,并且带一个浅灰色的阴影,可以使用下面的代码:

Paint paint = new Paint();

paint.setColor(Color.RED);

paint.setShadowLayer(2, 3, 3,Color.rgb(180, 180, 180));

2、 Canvas类

Android官方文档对Canvas类的描述如下:

The Canvas class holds the"draw" calls. To draw something, you need 4 basic components: ABitmap to hold the pixels, a Canvas to host the draw calls (writing into thebitmap), a drawing primitive (e.g. Rect, Path, text, Bitmap), and a paint (todescribe the colors and styles for the drawing).

Canvas代表画布,通过该类提供的方法,可以绘制各种图形(如矩形、图形、线条等等),通常情况下,要在Android绘图,首先要创建一个继承自View类的自定义View,并且在该类中重写onDraw(Canvas canvas)方法,然后在显示绘图的Acticity中添加该自定义View。

3、 Bitmap类

Bitmap类代表一个位图,使用Bitmap类提供了函数,可以获取位图文件的信息,可以对位图进行剪切、旋转、缩放等操作,还可以以指定格式保存图像文件。

4、 BitmapFactory类

BitmapFactory类是一个工具类,用于从不同的数据源来解析、创建Bitmap对象。

例如,要利用图片文件/sdcard/pictures/imag1.jpg创建Bitmap对象,可使用如下代码:

String picture_path = “/sdcard/pictures/imag1.jpg”;

Bitmap bitmap = BitmapFactory.decodeFile(path);

再举一个例子,如果要利用Drawable资源中保存的图片文件image2.jpg创建对应的Bitmap对象,可使用如下代码:

Bitmap bitmap =BitmapFactory.decodeResource(MainActivity.this.getResource(),

R.drawable.image2);

二、绘图实例

下面我们来看一个Android绘图实例,该程序运行效果如下:



先来看主Activity文件,其内容如下:

package com.liuhaoyu;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}


只是简单的导入主布局文件main.xml,下面来看主布局文件的内容:

<?xml version="1.0"encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="25dp"
android:text="Android绘图示例" />

<com.liuhaoyu.MyView
android:id="@+id/myView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>


在主布局文件中导入了一个自定义的视图类com.liuhoayu.MyView,我们在这个类中完成绘图操作,其内容如下:

package com.liuhaoyu;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

public classMyView extendsView {

public MyView(Context context,AttributeSet attrs) {
super(context, attrs);
}

@Override
protected void onDraw(Canvas canvas) {
Paintpaint=newPaint();
paint.setColor(Color.RED);
paint.setShadowLayer(5,10, 10, Color.rgb(180, 180, 180));
canvas.drawRect(0,0, 60, 60, paint);

//绘制弧
paint.setColor(Color.YELLOW);
RectFrectf=newRectF(70, 0, 150, 70);
canvas.drawArc(rectf,0, 60, true,paint);

//绘制圆形
paint.setColor(Color.GRAY);
canvas.drawCircle(100,150, 30, paint);

//绘制一条线
paint.setColor(Color.WHITE);
paint.setStyle(Style.FILL);
canvas.drawLine(30,250, 150, 250, paint);

super.onDraw(canvas);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: