Android绘画相关

一、画一个简单的圆

自定义DemoView集成View,重写onDraw(Canvas canvas)方法

public class DemoView extends View {
    private Paint mPaint;

    public DemoView(Context context) {
        super(context);
        mPaint = new Paint();
        mPaint.setColor(Color.BLUE);
        mPaint.setStrokeWidth(5);
        mPaint.setAntiAlias(true);
    }

    @Override
    protected void onDraw(Canvas canvas) {

        canvas.drawCircle(100, 100, 50, mPaint);
    }
}
 
关于paint:
	The Paint class holds the style and color information about how to draw geometries, text and bitmaps.

Paint类持有绘制几何图形、文字、位图(bitmap)相关的主题 和 颜色信息。

1·在构造方法中的mPaint.setColor(int color):

	Set the paint's color. Note that the color is an int containing alpha
	as well as r,g,b. This 32bit value is not premultiplied, meaning that
	its alpha can be any value, regardless of the values of r,g,b.
	See the Color class for more details.
	设置paint的颜色,注意这个颜色是一个包含红,绿,蓝同时也包含透明度的int型数值,
	这个32位值(a,r,g,b, 8*4)不是左自乘的,这意味着a可以是任意值,与r,g,b无关
 
 
	"Premultiplied" alpha技术意味着不仅像素的Alpha信息存储在通道里,而且已经扩张到红,蓝,绿通道里,
	在实际应用中这意味着如果你保存着 #fff8000橙色,而将其透明度设置为50%,那么实际设置保存的是#80803f00.
	这意味着颜色通道中的任意像素值都不会比Alpha通道中的值来的大
2·构造方法中的mPaint.setStrokeWidth(float float):
	Set the width for stroking.
	Pass 0 to stroke in hairline mode.
	Hairlines always draws a single pixel independent of the canva's matrix.
	设置画笔的宽度,不要在hairline模式下,将stroke宽度设置为0
	该模式下,永远画一像素且与画布的matrix无关
3·构造方法中的mPaint.setAntiAlias(true):
	true to set the antialias bit in the flags, false to clear it
	设置抗锯齿效果
4·onDraw(Canvas canvas)方法:
	canvas.drawCircle(100, 100, 50, mPaint);
	参数:100,100 圆心所在的x,y坐标,50是半径
5·效果:
	
二、写一段文字:(onDraw(Canvas canvas))方法中
	String text = "hello world!";
	Rect rect = new Rect();
	mPaint.setTextSize(30);
	mPaint.getTextBounds(text, 0, text.length(), rect);
	int w = rect.width() >> 1;
	int h = rect.height() >> 1;
	canvas.drawText(text, 0, text.length(), 200 - w, 200 + h, mPaint);
1·Rect rect = new Rect();用来记录文字的长宽等
2·mPaint.getTextBounds(text, 0, text.length, rect);
	return in bounds (allocated by the caller) the smallest rectangle that
	encloses all of the characters, with an implied origin	at (0,0)
	返回一个界内包含所有元素的最小矩形,起点为(0,0),把矩形边界信息存储在rect中
3·mPaint.setTextSize(int size);指定文字的大小 >0
4·canvas.drawText(text, 0, text.length, 200 - w, 200 + h, mPaint)
	200 - w, 200 + h 写文字时第一笔的坐标位置,即包含所有文字的最小矩形的左下角坐标, 200,200为文字的中心点坐标
5·效果:
	
三、画一个bitmap
	Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.nanana);
	Rect src = null;
	Rect dst = new Rect(300, 300, 300 + bitmap.getWidth(), 300+bitmap.getHeight());
	canvas.drawBitmap(bitmap, src, dst, mPaint);
1·Rect src = null;用于指定bitmap上哪一区域将被绘制到canvas上,这里为null,即将bitmap全部绘制
2·Rect dst = new Rect(300, 300, 300 + bitmap.getWidth(), 300+bitmap.getHeight());
	指定canvas上绘制bitmap的区域,若面积大于src 图片将被拉伸,否则图片将被压缩
3.效果:
	
四·画一条斜线
	float[] pos = new float[]{0,0, 300,300, 0,300, 300,0};
	canvas.drawLines(pos, mPaint);
1·float[] pos = new float[]{0,0, 300,300, 0,300, 300,0};pos指定线的起点跟终点,四个一组
2·效果:
	
五、画圆弧
	RectF rectF = new RectF(50, 300, 150, 400);
     mPaint.setStyle(Paint.Style.STROKE);
     canvas.drawArc(rectF, 90, 270, true, mPaint);

	rectF = new RectF(50, 500, 150, 600);
     canvas.drawArc(rectF, 90, 270, false, mPaint);
1·RectF rectF = new RectF(50, 300, 150, 400);指定圆弧的上下左右
2·mPaint.setStyle(Paint.Style.STROKE);只划线不填充圆弧内部
3·canvas.drawArc(rectF, 90, 270, true, mPaint);90指定圆弧开始的角度,270指定圆弧扫过的角度
4.效果:
	
六、画圆角矩形
	rectF = new RectF(50, 600, 150, 700);
	canvas.drawRoundRect(rectF, 10, 10, mPaint);
1·canvas.drawRoundRect(rectF, 10, 10, mPaint);10,10圆角点的x,y坐标
2·效果:
	
 

                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值