Android Studio brush function to draw simple graphics

1. Set the page layout

Setting activity_main.xml as the frame layout is convenient for drawing directly using the position when drawing

2. Create a new java class for writing drawing code

Create a new myview class in the mainactivity directory

Three inherit the original view method and set the canvas

public class MyView extends View {
    
    
    public MyView(Context context) {
    
    
        super(context);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onDraw(Canvas canvas) {
    
    //编辑画布类
        super.onDraw(canvas);
    }
}

Four definition brushes

        Paint paint2 = new Paint();//定义画笔
        paint2.setStyle(Paint.Style.FILL);//设定画笔类型 fill为填充
        paint2.setColor(0xff99ffff);//定义画笔颜色 34位为透明度 默认空为100%透明 ff为完全不透明 最后6位为颜色

        Paint paint1 = new Paint();//定义画笔
        paint1.setStyle(Paint.Style.STROKE);//设定画笔类型 stroke为描边
        paint1.setColor(0xff663399);//定义画笔颜色 34位为透明度 默认空为100%透明 ff为完全不透明 最后6位为颜色
        paint1.setStrokeWidth(20);//定义边框宽度

Five drawing graphics

(1) rectangle

         canvas.drawRect(100,100,400,300,paint1);
         //绘制矩形 前俩为左上角 后俩为右下角 最后为使用的画笔

output graphics
output graphics

(2) round

         canvas.drawCircle(250, 600, 150, paint1);
         //绘制空心圆 前两为圆心位置 后一个半径 最后为使用的画笔

output graphics
output graphics

(3) Ellipse

        canvas.drawOval(100,900,400,1100, paint1);
        //绘制空心椭圆 设定外边框矩形 前俩为左上角 后俩为右下角 最后为使用的画笔

output graphics
output graphics

(4) Draw a custom polygon with a path

        //使用路径法绘制自定义图形
        Path path1 = new Path();
        path1.moveTo(250, 1250);//设定起始点
        path1.lineTo(100,1500);//顶点1
        path1.lineTo(400,1500);//顶点2
        path1.close();//最后一个点与顶点相连
        canvas.drawPath(path1, paint1);//绘制空心三角形

output graphics
output graphics

(5) arc or sector

        //绘制弧形或者扇形
        RectF rectF2 = new RectF(600, 1700, 900, 2000);//定义弧形的外边框矩形
        canvas.drawArc(rectF2,0,225,true, paint1);
        RectF rectF3 = new RectF(600, 1200, 900, 1500);//定义弧形的外边框矩形
        canvas.drawArc(rectF3,0,225,false, paint2);
        //第一个为外边框矩形
        //第二个为初始方向 0为3点钟方向 顺时针增加 逆时针减少
        //第三个为划过角度 顺时针增加 逆时针减少
        //第四个为是否连接中心点
        //第五个为使用的画笔类型

output graphics
insert image description here

example

myview.java

package com.example.paint;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.os.Build;
import android.view.View;

import androidx.annotation.RequiresApi;

public class MyView extends View {
    
    
    public MyView(Context context) {
    
    
        super(context);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onDraw(Canvas canvas) {
    
    //编辑画布类
        super.onDraw(canvas);
        Paint paint2 = new Paint();//定义画笔
        paint2.setStyle(Paint.Style.FILL);//设定画笔类型 fill为填充
        paint2.setColor(0xff99ffff);//定义画笔颜色


        Paint paint1 = new Paint();//定义画笔
        paint1.setStyle(Paint.Style.STROKE);//设定画笔类型 stroke为描边
        paint1.setColor(0xff663399);//定义画笔颜色
        paint1.setStrokeWidth(20);//定义边框宽度

        canvas.drawRect(100,100,400,300,paint1);//绘制空心矩形 前俩为左上角 后俩为右下角 最后为使用的画笔
        canvas.drawCircle(250, 600, 150, paint1);//绘制空心圆 前两为圆心位置 后一个半径 最后为使用的画笔
        canvas.drawOval(100,900,400,1100, paint1);//绘制空心椭圆 设定外边框矩形 前俩为左上角 后俩为右下角 最后为使用的画笔

        canvas.drawRect(600,100,900,300,paint2);//绘制实心矩形 前俩为左上角 后俩为右下角 最后为使用的画笔
        canvas.drawCircle(750, 600, 150, paint2);//绘制实心圆 前两为圆心位置 后一个半径 最后为使用的画笔
        canvas.drawOval(600,900,900,1100, paint2);//绘制实心椭圆 设定外边框矩形 前俩为左上角 后俩为右下角 最后为使用的画笔

        //使用路径法绘制自定义图形
        Path path1 = new Path();
        path1.moveTo(250, 1250);//设定起始点
        path1.lineTo(100,1500);//顶点1
        path1.lineTo(400,1500);//顶点2
        path1.close();//最后一个点与顶点相连
        canvas.drawPath(path1, paint1);//绘制空心三角形

        //绘制弧形或者扇
        RectF rectF1 = new RectF(100, 1700, 400, 2000);//定义弧形的外边框矩形
        canvas.drawArc(rectF1,0,225,false, paint1);
        RectF rectF2 = new RectF(600, 1700, 900, 2000);//定义弧形的外边框矩形
        canvas.drawArc(rectF2,0,225,true, paint1);
        RectF rectF3 = new RectF(600, 1200, 900, 1500);//定义弧形的外边框矩形
        canvas.drawArc(rectF3,0,225,false, paint2);
        //第一个为外边框矩形
        //第二个为初始方向 0为3点钟方向 顺时针增加 逆时针减少
        //第三个为划过角度 顺时针增加 逆时针减少
        //第四个为是否连接中心点
        //第五个为使用的画笔类型
    }
}

Running result graph
Running result graph

Author: Zhang Fengteng
Student ID: 116052020132
Original address: https://blog.csdn.net/m0_56687634/article/details/128178373?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22 %2C%22rId%22%3A%22128178373%22%2C%22source%22%3A%22m0_56687634%22%7D

Guess you like

Origin blog.csdn.net/fjnu_se/article/details/128179730