标签:paint canvas 时钟 android save和restore
项目中各种自定义控件,用到paint和canvas的地方非常多,所以就总结了下paint和canvas的用法,如有错误欢迎大家批评指正
package com.example.paintpractice;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Cap;
import android.graphics.Paint.Join;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;
public class DefineView extends View{
Paint paint;
public DefineView(Context context, AttributeSet attrs) {
super(context, attrs);
paint= new Paint();
//设置画笔的颜色
paint.setColor(Color. RED);
//设置画笔的宽度
paint.setStrokeWidth(5);
//设置画笔的样式, Style.FILL: 实心 STORKE: 空心 FILL_OR_STORE:同时实心与空心
paint.setStyle(Style. STROKE);
//设置抗锯齿功能
paint.setAntiAlias( true);
//设置笔触样式
/**ROUND
* The stroke projects out as a square, with the center at the end
* of the path.
*/
//它的英文解释如上我们要明白他是在path的终点进行设置(个人的理解)
paint.setStrokeCap(Cap. ROUND);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Path path= new Path();
path.moveTo(50,50);
path.lineTo(100,100);
path.lineTo(30,180);
path.lineTo(50,50);
canvas.drawPath(path, paint);
}
}
效果图:package com.example.paintpractice;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Cap;
import android.graphics.Paint.Join;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;
public class DefineView extends View{
Paint paint;
public DefineView(Context context, AttributeSet attrs) {
super(context, attrs);
paint= new Paint();
//设置画笔的颜色
paint.setColor(Color. RED);
//设置画笔的宽度
paint.setStrokeWidth(5);
//设置画笔的样式, Style.FILL: 实心 STORKE: 空心 FILL_OR_STORE:同时实心与空心
paint.setStyle(Style. STROKE);
//设置抗锯齿功能
paint.setAntiAlias( true);
//设置结合处的样式,MITER为锐角,ROUND为圆弧,BEVEL为直线
paint.setStrokeJoin(Join. ROUND);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Path path= new Path();
path.moveTo(50,50);
path.lineTo(100,100);
path.lineTo(30,180);
path.lineTo(50,50);
canvas.drawPath(path, paint);
}
}
<span style="font-size:18px;"> /**
* Draw the specified circle using the specified paint. If radius is <= 0,
* then nothing will be drawn. The circle will be filled or framed based
* on the Style in the paint.
*
* @param cx The x -coordinate of the center of the cirle to be drawn
* @param cy The y -coordinate of the center of the cirle to be drawn
* @param radius The radius of the cirle to be drawn
* @param paint The paint used to draw the circle
*/
public void drawCircle (float cx, float cy, float radius, Paint paint)
cx:圆心的X坐标
cy:圆心的Y坐标
radius:半径
</span>(2)绘制弧线 //绘制弧线区域
RectF rect= new RectF(10,10,100,100);
canvas.drawArc(rect,10,90, true, paint);
canvas.translate(50, 50);
canvas.drawArc(rect, //弧线所使用的矩形区域大小
10, //开始角度
90, //扫过的角度
false, //是否使用中心
paint//采用的哪个画笔
);
/**
* Draw the specified Rect using the specified paint. The rectangle will
* be filled or framed based on the Style in the paint.
*
* @param left The left side of the rectangle to be drawn
* @param top The top side of the rectangle to be drawn
* @param right The right side of the rectangle to be drawn
* @param bottom The bottom side of the rectangle to be drawn
* @param paint The paint used to draw the rect
*/
public void drawRect( float left, float top, float right, float bottom, Paint paint) {
native_drawRect(mNativeCanvas , left, top, right, bottom, paint.mNativePaint);
}
/**
* Draw a line segment with the specified start and stop x,y coordinates,
* using the specified paint.
*
* <p>Note that since a line is always "framed", the Style is ignored in the paint.</p>
*
* <p>Degenerate lines (length is 0) will not be drawn. </p>
*
* @param startX The x -coordinate of the start point of the line
* @param startY The y -coordinate of the start point of the line
* @param paint The paint used to draw the line
*/
public void drawLine (float startX, float startY, float stopX, float stopY, Paint paint) {
native_drawLine(mNativeCanvas , startX, startY, stopX, stopY, paint.mNativePaint);
}
/**
* Draw the specified oval using the specified paint. The oval will be
* filled or framed based on the Style in the paint.
*
* @param oval The rectangle bounds of the oval to be drawn
*/
public void drawOval (RectF oval, Paint paint) {
if (oval == null) {
throw new NullPointerException();
}
native_drawOval(mNativeCanvas , oval, paint.mNativePaint);
}
绘制椭圆是以一个矩形为参考依据的 /**
* Draw the specified path using the specified paint. The path will be
* filled or framed based on the Style in the paint.
*
* @param path The path to be drawn
* @param paint The paint used to draw the path
*/
public void drawPath (Path path, Paint paint) {
native_drawPath(mNativeCanvas , path.ni(), paint.mNativePaint);
}
使用此方法需注意:假如paint设置了paint.setStyle(Style.STROKE);为描边那么以上的代码的效果不是封闭的图形而是如下图: /**
* Draw the text in the array, with each character's origin specified by
* the pos array.
*
* This method does not support glyph composition and decomposition and
* should therefore not be used to render complex scripts.
*
* @param text The text to be drawn
* @param pos Array of [x,y] positions, used to position each character
* @param paint The paint used for the text (e.g. color, size, style)
*/
@Deprecated
public void drawPosText(String text, float[] pos, Paint paint) {
if (text.length()*2 > pos. length) {
throw new ArrayIndexOutOfBoundsException();
}
native_drawPosText(mNativeCanvas , text, pos, paint.mNativePaint);
}
/**
* Draw the text, with origin at (x,y), using the specified paint, along
* the specified path. The paint's Align setting determins where along the
* path to start the text.
*
* @param text The text to be drawn
* @param path The path the text should follow for its baseline
* @param hOffset The distance along the path to add to the text's
* starting position
* @param vOffset The distance above( -) or below(+) the path to position
* the text
* @param paint The paint used for the text (e.g. color, size, style)
*/
public void drawTextOnPath (String text, Path path, float hOffset, float vOffset, Paint paint) {
if (text.length() > 0) {
native_drawTextOnPath(mNativeCanvas, text, path.ni(), hOffset, vOffset,
paint.mBidiFlags, paint.mNativePaint);
}
}
/**
* Draw the specified round -rect using the specified paint. The roundrect
* will be filled or framed based on the Style in the paint.
*
* @param rect The rectangular bounds of the roundRect to be drawn
* @param rx The x -radius of the oval used to round the corners
* @param ry The y -radius of the oval used to round the corners
* @param paint The paint used to draw the roundRect
*/
public void drawRoundRect (RectF rect, float rx, float ry, Paint paint) {
if (rect == null) {
throw new NullPointerException();
}
native_drawRoundRect(mNativeCanvas , rect, rx, ry,
paint.mNativePaint);
}
标签:paint canvas 时钟 android save和restore
原文地址:http://blog.csdn.net/dmk877/article/details/44900367