【安卓】自定义View实现画板涂鸦等功能

一、实现效果

画笔测试
开始设计矩形
实现矩形的随笔画
圆形
三角形,还有bug
等腰三角形和塔型
等边三角形
等边三角形计算

二、代码

1、MainActivity.class

package com.lsl.mydrawingboarddemo;import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ImageButton;import com.lsl.mydrawingboarddemo.view.DrawingBoardView;import java.io.IOException;/*** author lishilin* date 2023/8/9* desc 负责画板UI交互*/
public class MainActivity extends AppCompatActivity implements View.OnClickListener {private ImageButton paintBtn,fillPaintBtn,rubberBtn,rectBtn,circleBtn,ellipticBtn,triangleBtn,triangleTowerBtn,selectedBtn,clearBtn,equilateralBtn,rightAngleBtn;private ImageButton blackBtn,whiteBtn,redBtn,blueBtn,grayBtn,cyanBtn,currentColorBtn;private DrawingBoardView drawingBoardView;private View selectColorBoxView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initView();}private void initView(){paintBtn = findViewById(R.id.paint);fillPaintBtn = findViewById(R.id.fillPaint);//rubberBtn = findViewById(R.id.rubber);rectBtn = findViewById(R.id.rect);circleBtn = findViewById(R.id.circle);ellipticBtn = findViewById(R.id.elliptic);triangleBtn = findViewById(R.id.triangle);triangleTowerBtn = findViewById(R.id.triangleTower);clearBtn = findViewById(R.id.clear);equilateralBtn = findViewById(R.id.equilateralTriangle);drawingBoardView = findViewById(R.id.drawView);selectColorBoxView = findViewById(R.id.selectColorBox);rightAngleBtn = findViewById(R.id.rightAngle);blackBtn = findViewById(R.id.blackBtn);whiteBtn = findViewById(R.id.whiteBtn);redBtn = findViewById(R.id.redBtn);blueBtn = findViewById(R.id.blueBtn);grayBtn = findViewById(R.id.grayBtn);cyanBtn = findViewById(R.id.cyanBtn);paintBtn.setSelected(true);selectedBtn = paintBtn;blackBtn.setSelected(true);currentColorBtn = blackBtn;paintBtn.setOnClickListener(this);fillPaintBtn.setOnClickListener(this);//rubberBtn.setOnClickListener(this);rectBtn.setOnClickListener(this);circleBtn.setOnClickListener(this);ellipticBtn.setOnClickListener(this);triangleBtn.setOnClickListener(this);triangleTowerBtn.setOnClickListener(this);clearBtn.setOnClickListener(this);blackBtn.setOnClickListener(this);whiteBtn.setOnClickListener(this);redBtn.setOnClickListener(this);blueBtn.setOnClickListener(this);grayBtn.setOnClickListener(this);equilateralBtn.setOnClickListener(this);rightAngleBtn.setOnClickListener(this);cyanBtn.setOnClickListener(this);}@Overridepublic void onClick(View v) {switch (v.getId()){case R.id.paint:setSelectBtn(paintBtn);drawingBoardView.setPaint("paint");selectColorBoxView.setVisibility(View.VISIBLE);break;case R.id.fillPaint:setSelectBtn(fillPaintBtn);drawingBoardView.setPaint("fillPaint");selectColorBoxView.setVisibility(View.VISIBLE);break;
//            case R.id.rubber:
//                setSelectBtn(rubberBtn);
//                drawingBoardView.setEraseMode();
//                selectColorBoxView.setVisibility(View.INVISIBLE);
//                break;case R.id.rect:setSelectBtn(rectBtn);selectColorBoxView.setVisibility(View.VISIBLE);drawingBoardView.setPaint("rect");break;case R.id.circle:setSelectBtn(circleBtn);selectColorBoxView.setVisibility(View.VISIBLE);drawingBoardView.setPaint("drawCircle");break;case R.id.elliptic:setSelectBtn(ellipticBtn);selectColorBoxView.setVisibility(View.VISIBLE);drawingBoardView.setPaint("drawOval");break;case R.id.triangle:setSelectBtn(triangleBtn);selectColorBoxView.setVisibility(View.VISIBLE);drawingBoardView.setPaint("drawTriangle");break;case R.id.equilateralTriangle:setSelectBtn(equilateralBtn);selectColorBoxView.setVisibility(View.VISIBLE);drawingBoardView.setPaint("drawEquilateral");break;case R.id.triangleTower:setSelectBtn(triangleTowerBtn);selectColorBoxView.setVisibility(View.VISIBLE);drawingBoardView.setPaint("drawTriangleTower");break;case R.id.rightAngle:setSelectBtn(rightAngleBtn);selectColorBoxView.setVisibility(View.VISIBLE);drawingBoardView.setPaint("drawRightAngle");break;case R.id.clear:clearStyle();//设置点击清除图标时的背景闪烁selectColorBoxView.setVisibility(View.VISIBLE);if (drawingBoardView!=null){drawingBoardView.clearBoard();}break;case R.id.blackBtn:int blackColor = ContextCompat.getColor(this,R.color.black);drawingBoardView.setPaintColor(blackColor);setCurrentColorBtn(blackBtn);break;case R.id.blueBtn:int blueColor = ContextCompat.getColor(this,R.color.blue);drawingBoardView.setPaintColor(blueColor);setCurrentColorBtn(blueBtn);break;case R.id.whiteBtn:int whiteColor = ContextCompat.getColor(this,R.color.white);drawingBoardView.setPaintColor(whiteColor);setCurrentColorBtn(whiteBtn);break;case R.id.cyanBtn:int cyanColor = ContextCompat.getColor(this,R.color.cyan);drawingBoardView.setPaintColor(cyanColor);setCurrentColorBtn(cyanBtn);break;case R.id.grayBtn:int grayColor = ContextCompat.getColor(this,R.color.gray);drawingBoardView.setPaintColor(grayColor);setCurrentColorBtn(grayBtn);break;case R.id.redBtn:int redColor = ContextCompat.getColor(this,R.color.red);drawingBoardView.setPaintColor(redColor);setCurrentColorBtn(redBtn);break;default:break;}}/*** 设置选中的图形button* @param selectBtn*/private void setSelectBtn(ImageButton selectBtn){if (this.selectedBtn!=null){this.selectedBtn.setSelected(false);}selectBtn.setSelected(true);this.selectedBtn = selectBtn;}private void setCurrentColorBtn(ImageButton currentColorBtn){if (this.currentColorBtn!=null){this.currentColorBtn.setSelected(false);}currentColorBtn.setSelected(true);this.currentColorBtn = currentColorBtn;}/*** 设置点击清除图标时的样式变化,让其背景变色500毫秒*/private void clearStyle(){clearBtn.setSelected(true);new Handler().postDelayed(new Runnable() {@Overridepublic void run() {clearBtn.setSelected(false);}}, 200);}}

2、布局代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@color/bord_color"tools:context=".MainActivity"><com.lsl.mydrawingboarddemo.view.DrawingBoardViewandroid:id="@+id/drawView"android:layout_width="match_parent"android:layout_height="650dp"android:layout_alignParentTop="true"android:background="@color/bord_color" /><LinearLayoutandroid:layout_width="600dp"android:layout_height="80dp"android:layout_alignParentBottom="true"android:layout_centerHorizontal="true"android:background="@drawable/bottom_bg_corner"android:gravity="center"android:orientation="horizontal"><LinearLayoutandroid:layout_width="100dp"android:layout_height="match_parent"android:gravity="center"><TableLayoutandroid:id="@+id/selectColorBox"android:layout_width="40dp"android:layout_height="100dp"android:gravity="center"><TableRow><ImageButtonandroid:id="@+id/blackBtn"android:background="@drawable/color_select"android:src="@drawable/black" /><ImageButtonandroid:id="@+id/whiteBtn"android:background="@drawable/color_select"android:src="@drawable/white" /></TableRow><TableRow><ImageButtonandroid:id="@+id/redBtn"android:background="@drawable/color_select"android:src="@drawable/red_circle" /><ImageButtonandroid:id="@+id/blueBtn"android:background="@drawable/color_select"android:src="@drawable/blue" /></TableRow><TableRow><ImageButtonandroid:id="@+id/grayBtn"android:background="@drawable/color_select"android:src="@drawable/gray" /><ImageButtonandroid:id="@+id/cyanBtn"android:background="@drawable/color_select"android:src="@drawable/cyan" /></TableRow></TableLayout></LinearLayout><TextViewandroid:layout_width="1dp"android:layout_height="match_parent"android:background="@color/gray" /><LinearLayoutandroid:layout_width="500dp"android:layout_height="match_parent"android:gravity="center"><ImageButtonandroid:id="@+id/paint"android:layout_width="28dp"android:layout_height="28dp"android:scaleType="centerInside"android:layout_marginLeft="8dp"android:background="@drawable/shape_selector"android:src="@drawable/paint" /><ImageButtonandroid:id="@+id/fillPaint"android:layout_width="32dp"android:layout_height="32dp"android:scaleType="centerInside"android:layout_marginLeft="8dp"android:background="@drawable/shape_selector"android:src="@drawable/brush" />
<!--            <ImageButton-->
<!--                android:id="@+id/rubber"-->
<!--                android:layout_width="32dp"-->
<!--                android:layout_height="32dp"-->
<!--                android:scaleType="centerInside"-->
<!--                android:layout_marginLeft="8dp"-->
<!--                android:background="@drawable/shape_selector"-->
<!--                android:src="@drawable/rubber" />--><ImageButtonandroid:id="@+id/rect"android:layout_width="32dp"android:layout_height="32dp"android:scaleType="centerInside"android:layout_marginLeft="8dp"android:background="@drawable/shape_selector"android:focusable="true"android:src="@drawable/rect" /><ImageButtonandroid:id="@+id/circle"android:layout_width="32dp"android:layout_height="32dp"android:scaleType="centerInside"android:layout_marginLeft="8dp"android:background="@drawable/shape_selector"android:src="@drawable/circle" /><ImageButtonandroid:id="@+id/elliptic"android:layout_width="35dp"android:layout_height="35dp"android:scaleType="centerInside"android:layout_marginLeft="8dp"android:background="@drawable/shape_selector"android:src="@drawable/elliptic" /><ImageButtonandroid:id="@+id/triangle"android:layout_width="38dp"android:layout_height="40dp"android:scaleType="centerInside"android:layout_marginLeft="8dp"android:background="@drawable/shape_selector"android:src="@drawable/triangle" /><ImageButtonandroid:id="@+id/equilateralTriangle"android:layout_width="32dp"android:layout_height="32dp"android:scaleType="centerInside"android:layout_marginLeft="8dp"android:background="@drawable/shape_selector"android:src="@drawable/equilateral_triangle" /><ImageButtonandroid:id="@+id/rightAngle"android:layout_width="37dp"android:layout_height="37dp"android:scaleType="centerInside"android:layout_marginLeft="8dp"android:background="@drawable/shape_selector"android:src="@drawable/plan" /><ImageButtonandroid:id="@+id/triangleTower"android:layout_width="28dp"android:layout_height="28dp"android:scaleType="centerInside"android:layout_marginLeft="8dp"android:background="@drawable/shape_selector"android:src="@drawable/tower" /><ImageButtonandroid:id="@+id/clear"android:layout_width="32dp"android:layout_height="32dp"android:scaleType="centerInside"android:layout_marginLeft="8dp"android:background="@drawable/shape_selector"android:src="@drawable/clear" /></LinearLayout></LinearLayout></RelativeLayout>

3、自定义View代码

package com.lsl.mydrawingboarddemo.view;import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceView;
import android.view.View;import androidx.annotation.Nullable;import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;/*** author lishilin* date 2023/8/9* desc 自定义View,实现画板功能*/
public class DrawingBoardView extends SurfaceView {private Paint erasePaint, rectPaint, circlePaint;private Path erasePath, selectPath, triangleTowerPath, trianglePath, equilateralPath, anglePath;private List<Path> paintPaths, fillPaths, triangleTowerPaths, trianglePaths, equilateralPaths, anglePaths;private List<Paint> paints, fillPaints, rectPaints, circlePaints, ovalPaints, triangleTowerPaints, trianglePaints, equilateralPaints, anglePaints;private String pathType = "paintPath";private boolean eraseMode = false;private List<Rect> rects;private List<Circle> circleList;private List<RectF> ovals;private RectF rectF;private float startX, startY, currentX, currentY;private boolean isDrawing, drawPath, drawRect, drawCircle, isDrawingCircle, isDrawingOval, drawOval, drawTriangleTower, isDrawingTriangleTower, drawTriangle, isDrawingTriangle, isDrawingEquilateral, drawEquilateral, isDrawingRightAngle, drawRightAngle;private Rect rect;private Circle circle;private int pointX, pointY, radius;private float tipX, tipY, leftX, leftY, rightX, rightY, equilateralX;public DrawingBoardView(Context context) {super(context);init();}public DrawingBoardView(Context context, @Nullable AttributeSet attrs) {super(context, attrs);init();}public DrawingBoardView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);init();}public DrawingBoardView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {super(context, attrs, defStyleAttr, defStyleRes);}private void init() {erasePaint = new Paint();circle = new Circle();rect = new Rect();triangleTowerPaints = new ArrayList<>();triangleTowerPaths = new ArrayList<>();ovals = new ArrayList<>();trianglePaints = new ArrayList<>();trianglePaths = new ArrayList<>();rectPaints = new ArrayList<>();ovalPaints = new ArrayList<>();circlePaints = new ArrayList<>();circleList = new ArrayList<>();anglePaints = new ArrayList<>();anglePath = new Path();anglePaths = new ArrayList<>();equilateralPaths = new ArrayList<>();equilateralPaints = new ArrayList<>();circlePaint = new Paint();triangleTowerPath = new Path();circlePaint.setColor(Color.BLACK);circlePaint.setStyle(Paint.Style.STROKE);circlePaint.setStrokeWidth(5);erasePath = new Path();trianglePath = new Path();equilateralPath = new Path();triangleTowerPath = new Path();paints = new ArrayList<>();fillPaints = new ArrayList<>();fillPaths = new ArrayList<>();paintPaths = new ArrayList<>();setPaintColor(Color.BLACK);rects = new ArrayList<>();rectPaint = new Paint();rectPaint.setColor(Color.BLACK);rectPaint.setStrokeWidth(5);rectPaint.setStyle(Paint.Style.STROKE);drawPath = true;isDrawing = false;}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);Paint paint = paints.get(paints.size() - 1);if (isDrawing) {int left = (int) Math.min(startX, currentX);int right = (int) Math.max(startX, currentX);int top = (int) Math.min(startY, currentY);int bottom = (int) Math.max(startY, currentY);rect.set(left, top, right, bottom);canvas.drawRect(rect, paint);isDrawing = false;}if (!isDrawing) {for (int i = 0; i < rects.size(); i++) {canvas.drawRect(rects.get(i), rectPaints.get(i));}}for (int i = 0; i < paintPaths.size(); i++) {canvas.drawPath(paintPaths.get(i), paints.get(i));}for (int i = 0; i < trianglePaths.size(); i++) {canvas.drawPath(trianglePaths.get(i), trianglePaints.get(i));}if (isDrawingCircle) {circle.setPointX(pointX);circle.setPointY(pointY);circle.setRadius(radius);canvas.drawCircle(circle.getPointX(), circle.getPointY(), circle.getRadius(), paint);isDrawingCircle = false;}if (!isDrawingCircle) {for (int i = 0; i < circleList.size(); i++) {canvas.drawCircle(circleList.get(i).getPointX(), circleList.get(i).getPointY(), circleList.get(i).getRadius(), circlePaints.get(i));}}if (isDrawingOval) {int left = (int) Math.min(startX, currentX);int right = (int) Math.max(startX, currentX);int top = (int) Math.min(startY, currentY);int bottom = (int) Math.max(startY, currentY);rectF.set(left, top, right, bottom);canvas.drawOval(rectF, paint);isDrawingOval = false;}if (!isDrawingOval) {for (int i = 0; i < ovals.size(); i++) {canvas.drawOval(ovals.get(i), ovalPaints.get(i));}}if (isDrawingTriangleTower) {float leftX = currentX - (currentX - startX) * 2;float leftY = currentY;triangleTowerPath.lineTo(leftX, leftY);triangleTowerPath.lineTo(currentX, currentY);triangleTowerPath.close();canvas.drawPath(triangleTowerPath, paint);isDrawingTriangleTower = false;}if (!isDrawingTriangleTower){for (int i = 0; i < triangleTowerPaths.size(); i++) {canvas.drawPath(triangleTowerPaths.get(i), triangleTowerPaints.get(i));}}if (isDrawingTriangle) {trianglePath.reset();trianglePath.moveTo(tipX, tipY);trianglePath.lineTo(leftX, leftY);trianglePath.lineTo(rightX, rightY);trianglePath.close();canvas.drawPath(trianglePath, paint);isDrawingTriangle = false;}if (isDrawingEquilateral) {equilateralPath.reset();equilateralPath.moveTo(equilateralX, startY);int x = (int) ((currentY - startY) / Math.sqrt(3));int rightX = (int) (equilateralX + x);int leftX = (int) (equilateralX - x);equilateralPath.lineTo(leftX, currentY);equilateralPath.lineTo(rightX, currentY);equilateralPath.close();canvas.drawPath(equilateralPath, paint);isDrawingEquilateral = false;}if (!isDrawingEquilateral) {for (int i = 0; i < equilateralPaths.size(); i++) {canvas.drawPath(equilateralPaths.get(i), equilateralPaints.get(i));}}if (isDrawingRightAngle) {anglePath.reset();anglePath.moveTo(startX, startY);anglePath.lineTo(startX, currentY);anglePath.lineTo(currentX, currentY);anglePath.close();canvas.drawPath(anglePath, paint);isDrawingRightAngle = false;}if (!isDrawingRightAngle) {for (int i = 0; i < anglePaths.size(); i++) {canvas.drawPath(anglePaths.get(i), anglePaints.get(i));}}for (int i = 0; i < fillPaths.size(); i++) {canvas.drawPath(fillPaths.get(i), fillPaints.get(i));}}@Overridepublic boolean onTouchEvent(MotionEvent event) {float x = event.getX();float y = event.getY();Paint paint = paints.get(paints.size() - 1);switch (event.getAction()) {case MotionEvent.ACTION_DOWN:startX = event.getX();startY = event.getY();if (drawPath && selectPath != null) {selectPath.moveTo(x, y);}if (drawRect) {rect = new Rect();rectPaints.add(paint);}if (drawCircle) {circle = new Circle();circlePaints.add(paint);}if (drawOval) {rectF = new RectF();ovalPaints.add(paint);}if (drawTriangleTower) {triangleTowerPaints.add(paint);triangleTowerPath.moveTo(event.getX(), event.getY());}if (drawTriangle) {trianglePath = new Path();tipY = startY;leftX = startX;trianglePaints.add(paint);}if (drawEquilateral) {equilateralPath = new Path();equilateralPaints.add(paint);}if (drawRightAngle) {anglePath = new Path();anglePaints.add(paint);}break;case MotionEvent.ACTION_MOVE:currentX = event.getX();currentY = event.getY();if (drawPath) {selectPath.lineTo(x, y);}if (drawRect) {isDrawing = true;invalidate();}if (drawOval) {isDrawingOval = true;invalidate();}if (drawCircle) {pointX = (int) (startX + (currentX - startX) / 2);pointY = (int) (startY + (currentY - startY) / 2);radius = (int) Math.abs(currentY - startY);isDrawingCircle = true;invalidate();}if (drawTriangleTower) {isDrawingTriangleTower = true;invalidate();}if (drawTriangle) {isDrawingTriangle = true;leftY = currentY;tipX = (currentX - leftX) / 2 + leftX;rightX = currentX;rightY = currentY;invalidate();}if (drawEquilateral) {equilateralX = startX + (currentX - startX) / 2;isDrawingEquilateral = true;invalidate();}if (drawRightAngle) {isDrawingRightAngle = true;invalidate();}break;case MotionEvent.ACTION_UP:if (drawRect) {rects.add(rect);isDrawing = false;}if (drawCircle) {circleList.add(circle);isDrawingCircle = false;}if (drawOval) {ovals.add(rectF);isDrawingOval = false;}if (drawTriangleTower) {triangleTowerPaths.add(triangleTowerPath);isDrawingTriangleTower = false;}if (drawTriangle) {trianglePaths.add(trianglePath);isDrawingTriangle = false;}if (drawEquilateral) {equilateralPaths.add(equilateralPath);isDrawingEquilateral = false;}if (drawRightAngle) {anglePaths.add(anglePath);isDrawingRightAngle = false;}break;}invalidate();return true;}public void clearBoard() {for (int i = 0; i < paintPaths.size(); i++) {paintPaths.get(i).reset();}for (int i = 0; i < fillPaths.size(); i++) {fillPaths.get(i).reset();}for (int i = 0; i < triangleTowerPaths.size(); i++) {triangleTowerPaths.get(i).reset();}for (int i = 0; i < trianglePaths.size(); i++) {trianglePaths.get(i).reset();}for (int i = 0; i < equilateralPaths.size(); i++) {equilateralPaths.get(i).reset();}for (Path path : anglePaths) {path.reset();}rects.clear();ovals.clear();circleList.clear();erasePath.reset();eraseMode = false;invalidate();}public void setPaint(String type) {switch (type) {case "paint":selectPath = paintPaths.get(paintPaths.size() - 1);this.pathType = "paintPath";drawPath = true;drawRect = false;drawCircle = false;drawOval = false;drawTriangle = false;drawTriangleTower = false;drawEquilateral = false;drawRightAngle = false;break;case "fillPaint":selectPath = fillPaths.get(fillPaths.size() - 1);this.pathType = "fillPath";drawPath = true;drawRect = false;drawCircle = false;drawOval = false;drawTriangle = false;drawTriangleTower = false;drawEquilateral = false;drawRightAngle = false;break;case "rect":drawPath = false;drawRect = true;drawCircle = false;drawOval = false;drawTriangle = false;drawTriangleTower = false;drawRightAngle = false;drawEquilateral = false;break;case "drawCircle":drawRect = false;drawPath = false;drawOval = false;drawCircle = true;drawTriangle = false;drawTriangleTower = false;drawEquilateral = false;drawRightAngle = false;break;case "drawOval":drawPath = false;drawRect = false;drawCircle = false;drawOval = true;drawTriangle = false;drawTriangleTower = false;drawEquilateral = false;drawRightAngle = false;break;case "drawTriangleTower":drawPath = false;drawRect = false;drawCircle = false;drawOval = false;drawTriangle = false;drawTriangleTower = true;drawEquilateral = false;drawRightAngle = false;break;case "drawTriangle":drawPath = false;drawRect = false;drawCircle = false;drawOval = false;drawTriangleTower = false;drawTriangle = true;drawRightAngle = false;drawEquilateral = false;break;case "drawEquilateral":drawPath = false;drawRect = false;drawCircle = false;drawOval = false;drawTriangleTower = false;drawTriangle = false;drawRightAngle = false;drawEquilateral = true;break;case "drawRightAngle":drawPath = false;drawRect = false;drawCircle = false;drawOval = false;drawTriangleTower = false;drawTriangle = false;drawEquilateral = false;drawRightAngle = true;break;}}public void setPaintColor(int color) {Paint paint = new Paint();paint.setColor(color);paint.setAntiAlias(true);paint.setStyle(Paint.Style.STROKE);paint.setStrokeCap(Paint.Cap.ROUND);paint.setStrokeJoin(Paint.Join.ROUND);paint.setStrokeWidth(3);paints.add(paint);Paint fillPaint = new Paint();fillPaint.setColor(color);fillPaint.setStyle(Paint.Style.STROKE);fillPaint.setStrokeWidth(20);fillPaint.setAntiAlias(true);fillPaint.setDither(true);fillPaint.setAlpha(250);fillPaint.setStrokeJoin(Paint.Join.ROUND);fillPaint.setStrokeCap(Paint.Cap.ROUND);fillPaints.add(fillPaint);Path path = new Path();Path fillPath = new Path();Path triangleTowerPath = new Path();this.triangleTowerPath = triangleTowerPath;this.triangleTowerPaints.add(paint);triangleTowerPaths.add(triangleTowerPath);Path trianglePath = new Path();trianglePaths.add(trianglePath);trianglePaints.add(paint);fillPaths.add(fillPath);paintPaths.add(path);Path anglePath = new Path();anglePaths.add(anglePath);this.anglePath = anglePath;this.anglePaints.add(paint);Path equilateralPath = new Path();this.equilateralPath = equilateralPath;equilateralPaths.add(equilateralPath);equilateralPaints.add(paint);if (pathType.equals("paintPath")) {selectPath = path;} else {selectPath = fillPath;}}public void setEraseMode() {eraseMode = true;drawPath = true;drawRect = false;drawTriangleTower = false;drawTriangle = false;drawCircle = false;drawEquilateral = false;drawRightAngle = false;erasePath = new Path();erasePaint.setAntiAlias(true);erasePaint.setDither(true);erasePaint.setStrokeWidth(30);erasePaint.setStyle(Paint.Style.STROKE);erasePaint.setStrokeJoin(Paint.Join.ROUND);erasePaint.setStrokeCap(Paint.Cap.SQUARE);erasePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));selectPath = erasePath;}private class Circle {int pointX;int pointY;int radius;public int getPointX() {return pointX;}public void setPointX(int pointX) {this.pointX = pointX;}public int getPointY() {return pointY;}public void setPointY(int pointY) {this.pointY = pointY;}public int getRadius() {return radius;}public void setRadius(int radius) {this.radius = radius;}}
}

三、总结

绘制主要用到Paint和Path,大体思路都是监听OnTouchEvent事件,绘制路径或者其他图案,开发这个项目用时一周。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/84175.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

redis实战-项目集成git及redis实现短信验证码登录

目录 IDEA集成git 传统session存在的问题 redis方案 业务流程 选用的数据结构 整体访问流程 发送短信验证码 获取校验验证码 配置登录拦截器 拦截器注册配置类 拦截器 用户状态刷新问题 刷新问题解决方案 IDEA集成git 远程仓库采用码云&#xff0c;创建好仓库&…

RH1288V3 - 初识物理服务器

如果你拥有一台物理服务器(不是云服务器) 个人比较推荐你用物理服务器&#xff0c;虽然性能会比云要来的差&#xff0c;但是不用每月交钱上。云服务固然方便&#xff0c;但是几个核的性能和一点存储&#xff0c;想做一个动漫网站固然要很多mp4这种影视资源&#xff0c;云服务器…

UG\NX二次开发 使用录制功能录制操作记录时,如何设置默认的开发语言?

文章作者&#xff1a;里海 来源网站&#xff1a;王牌飞行员_里海_里海NX二次开发3000例,C\C,Qt-CSDN博客 简介&#xff1a; NX二次开发使用BlockUI设计对话框时&#xff0c;如何设置默认的代码语言&#xff1f; 效果&#xff1a; 方法&#xff1a; 依次打开“文件”->“实用…

数据库结构差异对比工具

简介 前几年写了一个数据库对比工具&#xff0c;但是由于实现方式的原因&#xff0c;数据库支持有限&#xff0c;所以重新设计了一下&#xff0c;便于支持多种数据库&#xff0c;并且更新了UI。 新版地址&#xff1a;https://gitee.com/xgpxg/db-diff 旧版地址&#xff1a;h…

Hightopo 使用心得(6)- 3D场景环境配置(天空球,雾化,辉光,景深)

在前一篇文章《Hightopo 使用心得&#xff08;5&#xff09;- 动画的实现》中&#xff0c;我们将一个直升机模型放到了3D场景中。同时&#xff0c;还利用动画实现了让该直升机围绕山体巡逻。在这篇文章中&#xff0c;我们将对上一篇的场景进行一些环境上的丰富与美化。让场景更…

SpringCloud学习笔记(三)_服务提供者集群与服务发现Discovery

服务提供者集群 既然SpringCloud的是微服务结构&#xff0c;那么对于同一种服务&#xff0c;当然不可能只有一个节点&#xff0c;需要部署多个节点 架构图如下&#xff1a; 由上可以看出存在多个同一种服务提供者&#xff08;Service Provider&#xff09; 搭建服务提供者集…

picGo+gitee+typora设置图床

picGogiteetypora设置图床 picGogitee设置图床下载picGo软件安装picGo软件gitee操作在gitee中创建仓库在gitee中配置私人令牌 配置picGo在插件设置中搜索gitee插件并进行下载 TyporapicGo设置Typora 下载Typora进行图像设置 picGogitee设置图床 当我了解picGogitee可以设置图床…

Cpp学习——编译链接

目录 ​编辑 一&#xff0c;两种环境 二&#xff0c;编译环境下四个部分的 1.预处理 2.编译 3.汇编 4.链接 三&#xff0c;执行环境 一&#xff0c;两种环境 在程序运行时会有两种环境。第一种便是编译环境&#xff0c;第二种则是执行环境。如下图&#xff1a; 在程序运…

Win11安装VMware中的镜像的下载

首先&#xff0c;下载好VMware之后需要许可证&#xff0c;在VMware选择许可证填上即可&#xff08;可以解决一部分VMware创建虚拟机过程中出现的问题&#xff09;。 百度网盘自取&#xff1a; 链接&#xff1a;https://pan.baidu.com/s/17gBySqoPi2HeGJJlalp-VQ 提取码&…

opencv 案例实战01-停车场车牌识别实战

需求分析&#xff1a; 车牌识别技术主要应用领域有停车场收费管理&#xff0c;交通流量控制指标测量&#xff0c;车辆定位&#xff0c;汽车防盗&#xff0c;高速公路超速自动化监管、闯红灯电子警察、公路收费站等等功能。对于维护交通安全和城市治安&#xff0c;防止交通堵塞…

list使用

list的使用于string的使用都类似&#xff0c;首先通过查阅来看list有哪些函数&#xff1a; 可以看到函数还是蛮多的&#xff0c;我们值重点一些常用的和常见的&#xff1a; 1.关于push_back,push_front,和对应迭代器的使用 //关于push_back和push_front void test_list1() {l…

G. The Morning Star - 思维

分析&#xff1a; 直接暴力就会tle&#xff0c;不知道怎么下手&#xff0c;可以统计八个方向一条线上的所有坐标&#xff0c;这些坐标一定可以放在一起满足&#xff0c;分析都有哪些线&#xff0c;当横坐标相同时会有竖着的一条线都可以&#xff0c;也就是x c&#xff0c;当纵…