安卓基础图形的绘制(2)

上一篇了解了一下,自定义view的画布的设置。这一篇主要是基本图形的绘制。

在onDraw()中,首先要设置canvas,通过canvas.drawColor(Color.BLACK),设置画布的背景颜色。

        主要的属性有DrawPoint(),绘制一个点;DrawPoints(),绘制一组点。

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        paint.setColor(Color.WHITE);
        paint.setStrokeWidth(10);
        canvas.drawPoint(50,50,paint);
        paint.setStrokeWidth(10);
        paint.setColor(Color.RED);
        float[] pts={100,100,105,105,110,110};
        canvas.drawPoints(pts,paint);
}

DrawLine(),绘制一条线;DrawLines(),绘制一组线;

 @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        paint.setColor(Color.YELLOW);
        paint.setStrokeWidth(5);
        canvas.drawLine(100,200,300,400,paint);
        paint.setColor(Color.argb(255,0,200,255));
        float[] pts2={0,111,222,444,333,555,666,777,888,999};
        canvas.drawLines(pts2,paint);
        paint.setColor(Color.argb(100,0,200,255));
        canvas.drawLines(pts2,1,8,paint);
}

使用paint.moveTo()和paint.lineTo(),绘制五角星.

 @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        path.reset();
        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.STROKE);
        float r_out=150;
        for (int i = 0; i < 5; i++) {
            x[2*i]=(float)(540 + r_out*Math.cos(Math.toRadians(18+72*i)));
            y[2*i]=(float)(600 - r_out*Math.sin(Math.toRadians(18+72*i)));
        }
        float r_in=(float)(r_out*Math.sin(Math.toRadians(18))/Math.sin(Math.toRadians(126)));
        for (int i = 0; i < 5; i++) {
            x[2*i+1]=(float)(540 + r_in*Math.cos(Math.toRadians(54+72*i)));
            y[2*i+1]=(float)(600 - r_in*Math.sin(Math.toRadians(54+72*i)));
        }
        path.moveTo(x[0],y[0]);//移动画笔
        for (int i = 1; i < x.length; i++) {
            path.lineTo(x[i],y[i]);//直线绘制
        }
        path.close();
        canvas.drawPath(path,paint);
}

画圆,使用drawCircle()

 @Override
    protected void onDraw(Canvas canvas) {
        paint.setColor(Color.WHITE);
        canvas.drawCircle(540,1100,200,paint);
        paint.setStyle(Paint.Style.FILL);
        canvas.drawCircle(540,1100,150,paint);
        
}
画圆角矩形,使用drawRoundRect()
 @Override
 protected void onDraw(Canvas canvas) {
        paint.setColor(Color.CYAN);
        rectF.set(100,1100,300,1500);
        canvas.drawRoundRect(rectF,20,20,paint);    
}
双框圆角矩形
  @Override
 protected void onDraw(Canvas canvas) {       
        paint.setColor(Color.MAGENTA);
        if (Build.VERSION.SDK_INT >=Build.VERSION_CODES.Q){
            canvas.drawDoubleRoundRect(outer,20,20,
                    inner,10,10,paint);
        }
}

文本跟随 线 路径,drawTextOnPath()

​
 @Override
 protected void onDraw(Canvas canvas) {   
        path.reset();
        paint.setColor(Color.GREEN);
        path.rMoveTo(200,200);
        path.rLineTo(200,200);
        path.rQuadTo(100,-200,200,0);
        path.rCubicTo(100,-100,200,100,300,0);
        canvas.drawPath(path,paint);
        paint.setColor(Color.GREEN);
        paint.setTextSkewX(0.0f);
        paint.setTypeface(Typeface.DEFAULT_BOLD);
        canvas.drawTextOnPath("hello world hello world hello world",path,
                -100,50,paint);
}

​

文本跟随 圆 路径,drawTextOnPath()

 @Override
 protected void onDraw(Canvas canvas) {  
        path.reset();
        path.addCircle(540,1100,150,Path.Direction.CW);//顺时针
        paint.setColor(Color.BLUE);
        paint.setTextSize(32);
        canvas.drawTextOnPath("hello world",path,0,0,paint);
        canvas.drawTextOnPath("hello world",path,-100,50,paint);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值