package com.view;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;
public class SimpleDraw extends View implements Runnable {
/*
* 我们继续来介绍Android平台底层绘图类的相关内容,在Android UI开发专题(一)
* 之界面设计中我们介绍了有关Android平台资源使用以及Bitmap相关类的操作
* ,接下来将会以实例的方式给大家演示各种类的用处以及注意点.今天我们继续
* 了解android.graphics包中比较重要的绘图类.
*
* 一, android.graphics.Matrix有关图形的变换,缩放等相关操作常用的方法有:
* Java代码: void reset() // 重置一个matrix对象.
* void set(Matrix src) //复制一个源矩阵,和本类的构造方法 Matrix(Matrix src) 一样
* boolean isIdentity() //返回这个矩阵是否定义(已经有意义)
* void setRotate(float degrees) //指定一个角度以0,0为坐标进行旋转
* void setRotate(float degrees, float px, float py) //指定一个角度以px,py为坐标进行旋转
* void setScale(float sx, float sy) // 缩放
* void setScale(float sx, float sy, float px, float py) //以坐标px,py进行缩放
* void setTranslate(float dx, float dy) //平移
* void setSkew (float kx, float ky, float px, float py) //以坐标px,py进行倾斜
* void setSkew (float kx, float ky) //倾斜 复制代码 二,android.graphics.NinePatch
*
* NinePatch是Android平台特有的一种非矢量图形自然拉伸处理方法,可以帮助常规的
* 图形在拉伸时不会缩放,
* 实例中Android开发网提示大家对于Toast的显示就是该原理,同时SDK中提供了一
* 个工具名为Draw
* 9-Patch,有关该工具的使用方法可以参考我们经发布的 Draw
* 9-Patch使用方法介绍一文.由于该类提供了高质量支持透明的缩放方式,所以图形
* 格式为PNG,文件命名方式为.9.png
* 的后缀比如eoeandroid.
*
* 三,android.graphics.Paint.Paint类我们可以理解为画笔,画刷的属性定义,本类
* 常用的方法如下:
*
* Java代码: void reset() //重置
* void setARGB(int a, int r, int g, int b) 或 void setColor(int color)
* //均为设置Paint对象的颜色
* void setAntiAlias(boolean aa)
*
* //是否抗锯齿,需要配合void setFlags (Paint.ANTI_ALIAS_FLAG) 来帮助消除锯齿
* 使其边缘更平滑.
* Shader setShader(Shader shader)
*
* //设置阴影,Shader类是一个矩阵对象,如果为NULL将清除阴影.
* void setStyle(Paint.Style style) //设置样式,一般为 FILL 填充,或者STROKE凹陷
* 效果.
* void setTextSize(float textSize) //设置字体大小
* void setTextAlign(Paint.Align align) //文本对齐方式
* Typeface setTypeface(Typeface typeface)
* //设置字体,通过Typeface可以加载Android内部的字体,一般为宋体对于中文,
* 部分ROM可以自己添加比如雅黑等等
* void setUnderlineText(boolean underlineText)
*
* //是否设置下划线,需要撇和void setFlags (Paint.UNDERLINE_TEXT_FLAG) 方法. 复制代码
* 四,android.graphics.Rect
*
* Rect我们可以理解为矩形区域,类似的还有Point一个点,Rect类除了表示一个矩
* 形区域位置描述外,
* eoeandroid提示主要可以帮助我们计算图形之间是否碰撞
* (包含)关系,对于Android游戏开发比较有用,其主要的成员contains包含了三种
* 重载方法,来判断包含关系.
*
* Java代码:
* boolean contains(int left, int top, int right, int bottom)
* boolean contains(int x, int y)
*
* boolean contains(Rect r) 复制代码 五,android.graphics.Region
* Region在Android平台中表示一个区域和Rect不同的是
* ,它表示的是一个不规则的样子,可以是椭圆,多边形等等,而Rect仅仅是矩形.
* 同样Region的boolean contains(int x,
* int y) 成员可以判断一个点是否在该区域内.
*
* 六,android.graphics.Typeface
* Typeface类是帮助描述一个字体对象,在TextView中通过使用setTypeface方法来
* 制定一个输出文本的字体
* ,其直接构造调用成员create方法可以直接指定一个字体名称和样式,比如
* Java代码: static Typeface create(Typeface family, int style)
*
* static Typeface create(String familyName, int style) 复制代码
* 同时使用isBold和isItalic方法可以判断出是否包含粗体或斜体的字型.
*
* Java代码: final boolean isBold()
* final boolean isItalic() 复制代码 该类的创建方法还有从apk的资源或从一个具体的
* 文件路径,其具体方法为
* Java代码: static Typeface createFromAsset(AssetManager mgr, String path)
* static Typeface createFromFile(File path)
* static Typeface createFromFile(String path) 复制代码
*/
private Paint paint;
public SimpleDraw(Context context) {
super(context);
paint = new Paint();
new Thread(this).start();
}@Override protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(Color.GRAY); //
paint.setAntiAlias(true); //
canvas.save(); //
canvas.clipRect(10, 10, 110, 110); //
canvas.drawColor(Color.WHITE); //
// canvas.rotate(m);//以屏幕左上角为坐标原点旋转
m += 45.0f;
if (m == 360.0f) {
m = 0.0f;
}
canvas.rotate(m, 60, 60); // 以(60,60)为原点旋转
paint.setColor(Color.GREEN);
canvas.drawRect(new Rect(50, 50, 70, 70), paint);
canvas.restore();
canvas.save();
canvas.translate(140, 10);
canvas.clipRect(0, 0, 100, 100); // 一定要先剪辑出矩形区域再设画布背景,
//否则会覆盖整张画布
canvas.drawColor(Color.BLACK);
paint.setColor(Color.BLUE);
canvas.drawRect(new Rect(10, 10, 50, 50), paint);
canvas.restore();
//
canvas.save();
canvas.translate(120, 120);
canvas.clipRect(new Rect(0, 0, 100, 100));
canvas.drawColor(Color.GREEN);
// paint.setColor(Color.BLUE);
paint.setStrokeWidth(4);
paint.setColor(Color.BLACK);
canvas.drawLine(0, 60, 100, 60, paint);
paint.setARGB(255, 51, 51, 51);
paint.setTextSize(20);
paint.setFlags(Paint.ANTI_ALIAS_FLAG);
paint.setUnderlineText(true);
// paint.setFlags(Paint.UNDERLINE_TEXT_FLAG);
canvas.drawText("陈建立", 25, 80, paint);
paint.setColor(Color.WHITE);
canvas.drawRect(new Rect(10, 10, 50, 50), paint);
canvas.restore();
}
float m = 0.0f;
public void run() {
while (!Thread.currentThread().isInterrupted()) {
try {
Thread.sleep(500); // 每半秒执行一次
} catch(InterruptedException e) {
Thread.currentThread().interrupt();
}
postInvalidate();
}
}
}
来源: http://lib.csdn.net/snippet/android/35926