Android之Canvas的相关方法

这里简单介绍一下Canvas的相关方法~

使用Canvas可以画线,画矩形,画圆,画扇形,画多变形~

画这些都有一个共同点

要准备画板,纸,画笔,因为我们要在上面画东西,看以下代码:(init方法)

<span style="font-size:14px;">                //创建了一张纸
		bitmap=Bitmap.createBitmap(500, 500, Config.ARGB_8888);
		//把纸固定在画板上
		canvas=new Canvas(bitmap);
		//设置画板颜色--黑色
		canvas.drawColor(Color.BLACK);
		paint=new Paint();
		//设置画笔颜色
		paint.setColor(Color.RED);
		//抗锯齿
		paint.setAntiAlias(true);
		//设置画笔粗细
		paint.setStrokeWidth(5);
		//设置画笔的风格
		//paint.setStyle(Style.STROKE);</span>
有了上述东西,现在就开始画东西了~

画线:

//画线
	public void drawLine(View View) {
		// TODO Auto-generated method stub
		init();
		canvas.drawLine(20, 20, 200, 200, paint);
		iv.setImageBitmap(bitmap);
	}

jar包里的drwaLine方法:

public void drawLine(float startX, float startY, float stopX, float stopY, Paint paint) {
        native_drawLine(mNativeCanvas, startX, startY, stopX, stopY, paint.mNativePaint);
    }

drawLine方法有5个参数,含义分别为开始的点的X和Y坐标,结束的点的X和Y坐标,画笔~


画矩形:

//画矩形
	public void drawRect(View view) {
		// TODO Auto-generated method stub
		init();
		canvas.drawRect(20, 20, 200, 200, paint);
		iv.setImageBitmap(bitmap);
	}
jar包的drawRect方法:

 public void drawRect(float left, float top, float right, float bottom, Paint paint) {
        native_drawRect(mNativeCanvas, left, top, right, bottom, paint.mNativePaint);
    }
drawRect方法有5个参数,含义的分别为:

 left   The left side of the rectangle to be drawn
 top    The top side of the rectangle to be drawn
 right  The right side of the rectangle to be drawn
 bottom The bottom side of the rectangle to be drawn
 paint  The paint used to draw the rect
通俗的来说就是矩形的左上角的坐标和右下角的坐标和画笔


画圆:

//画圆
	public void drawCircle(View View) {
		// TODO Auto-generated method stub

		init();
		canvas.drawCircle(250, 250, 250, paint);
		iv.setImageBitmap(bitmap);
	}

drawCircle方法:

/**
     * 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) {
        native_drawCircle(mNativeCanvas, cx, cy, radius, paint.mNativePaint);
    }
参数含义:圆点坐标和半径和画笔~


画扇形:

//画扇形
	public void drawArc(View view) {
		// TODO Auto-generated method stub
		init();
		RectF rectF=new RectF(20,20,400,400);//矩形
		canvas.drawArc(rectF, -90, 90,true, paint);
		iv.setImageBitmap(bitmap);
	}

drawArc方法:

 * @param oval       The bounds of oval used to define the shape and size
     *                   of the arc
     * @param startAngle Starting angle (in degrees) where the arc begins
     * @param sweepAngle Sweep angle (in degrees) measured clockwise
     * @param useCenter If true, include the center of the oval in the arc, and
                        close it if it is being stroked. This will draw a wedge
     * @param paint      The paint used to draw the arc
     */
    public void drawArc(RectF oval, float startAngle, float sweepAngle, boolean useCenter,
            Paint paint) {
        if (oval == null) {
            throw new NullPointerException();
        }
        native_drawArc(mNativeCanvas, oval, startAngle, sweepAngle,
                       useCenter, paint.mNativePaint);
    }
参数含义:矩形,开始角度,扫(要画)的角度,是否顺时针(true代表顺时针),画笔~

注意:开始角度负的度数代表Y轴正半轴,而正的度数是Y轴负半轴,这个与数学不一样~这点要注意~


画多边形:

	//画多边形
	public void drawPath(View view) {
		// TODO Auto-generated method stub
		init();
		Path path=new Path();
		path.moveTo(250, 10);
		path.lineTo(100,300);
		RectF rectF=new RectF(100,200,400,400);
		path.lineTo(400, 300);
		path.lineTo(250, 10);
		path.addArc(rectF, 0, 180);
		canvas.drawPath(path, paint);
		iv.setImageBitmap(bitmap);
	}

效果图:










  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值