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

Android 绘图(一) Paint

2016-07-15 10:45 423 查看
了解Android绘图或者自定义View的同学,都知道Canvas 类、Paint类等。今天就来看看Paint的有关描述。

首先看看官网的定义:

The Paint class holds the style and color information about how to draw geometries, text and bitmaps.
翻译:Paint类拥有如何绘制几何图形、文本、位图的颜色和样式等信息。

Android系统提供了设置画笔属性的Api,接下来,就来看一些常用Api的使用说明。

一. Paint类常用Api介绍。

1 public float ascent ()
返回基线以上的距离基于当前文本的字体和文字大小。
2 public float descent ()
返回基线以下的距离基于当前文本的字体和文字大小。
获取文字大小时,结合ascent()和descent()使用!

3 public float measureText (String text)
返回文字的宽度
4 public void setARGB (int a, int r, int g, int b)
参数
a 	画笔颜色的透明度,取值范围(0..255).
r 	画笔红色色值,取值范围(0..255).
g 	画笔绿色色值,取值范围(0..255).
b 	画笔蓝色色值,取值范围(0..255).
设置画笔的ARGB值
5 public void setAntiAlias (boolean aa)
参数
aa 	真,设置锯齿平滑,假,清除标识
设置是否抗锯齿
6 public void setColor (int color)
参数
color 颜色值(包含透明度)设置画笔的颜色.
设置画笔的颜色
7 public ColorFilter setColorFilter (ColorFilter filter)
参数
filter 可能为空。这个绿色器将代替画笔.
设置或清除画笔的绿色器,返回参数值
8 public void setStrokeWidth (float width)
参数
width  设置画笔的笔画宽度,无论画笔样式是的 Stroke 或者StrokeAndFill.
设置笔画的宽度。如果为0,则是hairline模式。Hairlines经常绘制一个像素依靠于画布的matrix。
9 public PathEffect setPathEffect (PathEffect effect)
参数
effect 	可能为空值。patheffect将替代画笔.
设置绘制路径的路径效果。设置或者清除路径效果。传递空值,将清除任何以前的路径效果。为方便起见,也返回传递的参数。
10 public void setStyle (Paint.Style style)
参数
style 画笔的样式 
设置画笔的样式,使用它来控制几何图形的解释(除了drawBitmap,总是假定Fill)。
画笔样式分三种:
(1).Paint.Style.STROKE:描边
(2).Paint.Style.FILL_AND_STROKE:描边并填充
(3).Paint.Style.FILL:填充
11 public void setTextAlign (Paint.Align align)
参数
align 绘制文本时的画笔的对齐方式
设置画笔的文本对齐方式。它控制文本相对于原点的位置。
12 public void setTextSize (float textSize)
参数
textSize 画笔的文字大小
设置画笔的文字大小。值必须大于0。
</pre><pre name="code" class="java">

以上就是画笔常用的一些方法,下面,我们看看具体使用。

二. 方法使用。

1.新建Android项目。

2.自定义一个View。

package cn.xinxing.customview.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

public class View1 extends View {

private Paint mPaint = new Paint();
private int DEFAULT_STROKE_WIDTH = 10;// strokeWidth
private int DEFAULT_RADIUS = 30;// 半径
private int DEFAULT_TEXT_SIZE=20;//

private int mStrokeWidth = dp2px(DEFAULT_STROKE_WIDTH);
private int mRadius = dp2px(DEFAULT_RADIUS);
private int mTextSize=sp2px(DEFAULT_TEXT_SIZE);

public View1(Context context) {
this(context, null);
// TODO Auto-generated constructor stub
}

public View1(Context context, AttributeSet attrs) {
this(context, attrs, 0);
// TODO Auto-generated constructor stub
}

public View1(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
// TODO Auto-generated constructor stub
}

@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
// 绘制一条线段
mPaint.setColor(Color.RED);// 设置画笔颜色为白色
mPaint.setAntiAlias(true);// 坑锯齿
mPaint.setStrokeWidth(mStrokeWidth);// 设置画笔的宽度
canvas.drawLine(0, 0, 400, 0, mPaint);// 绘制一条线段
// 绘制空心圆
mPaint.setColor(Color.BLUE);// 设置画笔颜色为蓝色
mPaint.setAntiAlias(true);// 坑锯齿
mPaint.setStyle(Style.STROKE);// 样式是描边
canvas.drawCircle(100, 60, mRadius, mPaint);// 绘制圆
// 绘制填充圆
mPaint.setColor(Color.GREEN);// 设置画笔颜色为绿色
mPaint.setStyle(Style.FILL);// 样式是填充
canvas.drawCircle(100, 180, mRadius, mPaint);// 绘制圆
// 绘制圆
mPaint.setColor(Color.GRAY);// 设置画笔颜色为绿色
mPaint.setStyle(Style.FILL_AND_STROKE);// 样式是描边和填充
canvas.drawCircle(100, 300, mRadius, mPaint);// 绘制圆
// 设置圆的描边和填充为不一样的颜色
mPaint.setColor(Color.RED);// 设置画笔颜色为绿色
mPaint.setStyle(Style.STROKE);// 样式是描边
canvas.drawCircle(100, 400, mRadius, mPaint);// 绘制圆
mPaint.setColor(Color.YELLOW);// 设置画笔颜色为绿色
mPaint.setStyle(Style.FILL);// 样式是填充
canvas.drawCircle(100, 400, mRadius, mPaint);// 绘制圆
// 绘制文字
mPaint.setColor(Color.BLACK);// 设置画笔颜色为绿色
mPaint.setTextSize(mTextSize);//设置画笔文字的大小
canvas.drawText("80%", 100, 500, mPaint);//绘制文字
}

/**
* @param dpVal
* @return
*/
private int dp2px(int dpVal) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
dpVal, getResources().getDisplayMetrics());
}

/**
* @param spVal
* @return
*/
private int sp2px(int spVal) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
spVal, getResources().getDisplayMetrics());
}
}
代码比较简单,就不多说 了!来张运行后的效果图,



三. 总结。

本篇只是简单的列举了Paint类的常用方法以及简单使用,仅供参考!本人水平有限,如有错误,欢迎指出!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: