Android之Canvas的相关方法

来源:互联网 发布:python mysql的参数化 编辑:程序博客网 时间:2024/06/15 14:06

这里简单介绍一下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 stubinit();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 stubinit();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 stubinit();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 stubinit();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 stubinit();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
原创粉丝点击