44import android .content .res .TypedArray ;
55import android .graphics .Bitmap ;
66import android .graphics .BitmapFactory ;
7+ import android .graphics .BlendMode ;
78import android .graphics .Canvas ;
89import android .graphics .Color ;
910import android .graphics .Matrix ;
1011import android .graphics .Paint ;
12+ import android .graphics .PathEffect ;
1113import android .graphics .PixelFormat ;
1214import android .graphics .PointF ;
1315import android .graphics .PorterDuff ;
1416import android .graphics .PorterDuffXfermode ;
17+ import android .graphics .Shader ;
1518import android .graphics .Xfermode ;
1619import android .graphics .drawable .Drawable ;
17- import android .text . TextPaint ;
20+ import android .os . Build ;
1821import android .util .AttributeSet ;
1922import android .util .DisplayMetrics ;
2023import android .util .TypedValue ;
4548import androidx .annotation .IntDef ;
4649import androidx .annotation .NonNull ;
4750import androidx .annotation .Nullable ;
51+ import androidx .annotation .RequiresApi ;
4852
4953/**
5054 * @author <a href="mailto:jenly1314@gmail.com">Jenly</a>
@@ -120,10 +124,6 @@ public class DrawBoardView extends View {
120124 * 触摸点画笔
121125 */
122126 private Paint pointPaint ;
123- /**
124- * 文本画笔
125- */
126- private TextPaint textPaint ;
127127 /**
128128 * 画笔的颜色
129129 */
@@ -132,10 +132,6 @@ public class DrawBoardView extends View {
132132 * 触摸点画笔的颜色
133133 */
134134 private int touchPointColor = 0xAFCCCCCC ;
135- /**
136- * 文本画笔的颜色
137- */
138- private int drawTextColor = Color .RED ;
139135 /**
140136 * 绘制文本的字体大小
141137 */
@@ -160,10 +156,26 @@ public class DrawBoardView extends View {
160156 * 缩放点描边宽度
161157 */
162158 private float zoomPointStrokeWidth ;
159+ /**
160+ * 画笔的 Paint.Style
161+ */
162+ private Paint .Style paintStyle = Paint .Style .STROKE ;
163+ /**
164+ * 画笔的着色器
165+ */
166+ private Shader paintShader ;
163167 /**
164168 * 画笔的 Xfermode
165169 */
166170 private Xfermode paintXfermode ;
171+ /**
172+ * 画笔的 PathEffect
173+ */
174+ private PathEffect pathEffect ;
175+ /**
176+ * 画笔的 BlendMode
177+ */
178+ private BlendMode blendMode ;
167179 /**
168180 * 绘图模式
169181 */
@@ -416,8 +428,6 @@ private void init(Context context, @Nullable AttributeSet attrs){
416428 paintColor = a .getColor (attr , Color .RED );
417429 }else if (attr == R .styleable .DrawBoardView_dbvTouchPointColor ){
418430 touchPointColor = a .getColor (attr , Color .RED );
419- }else if (attr == R .styleable .DrawBoardView_dbvDrawTextColor ){
420- drawTextColor = a .getColor (attr , Color .RED );
421431 }else if (attr == R .styleable .DrawBoardView_dbvDrawTextSize ){
422432 drawTextSize = a .getDimension (attr , drawTextSize );
423433 }else if (attr == R .styleable .DrawBoardView_dbvDrawTextBold ){
@@ -436,14 +446,6 @@ private void init(Context context, @Nullable AttributeSet attrs){
436446 pointPaint .setAntiAlias (true );
437447 pointPaint .setColor (touchPointColor );
438448
439- textPaint = new TextPaint ();
440- textPaint .setStyle (Paint .Style .FILL_AND_STROKE );
441- textPaint .setAntiAlias (true );
442- textPaint .setColor (drawTextColor );
443- textPaint .setTextSize (drawTextSize );
444- textPaint .setFakeBoldText (isFakeBoldText );
445- textPaint .setUnderlineText (isUnderlineText );
446-
447449 drawList = new LinkedList <>();
448450 backupDrawList = new ArrayList <>();
449451 matrix = new Matrix ();
@@ -708,7 +710,7 @@ protected void onDraw(Canvas canvas) {
708710 public void draw (@ NonNull Draw draw ){
709711 if (drawingCanvas != null ){
710712 if (draw .getPaint () == null ){//如果画笔为空则根据当前配置自动创建画笔,如果draw 是绘制其他有依赖的需自己配置后再传进来
711- draw .setPaint (createPaint (false ));
713+ draw .setPaint (createPaint (drawMode ));
712714 }
713715 //进行绘制
714716 draw .draw (drawingCanvas );
@@ -811,26 +813,45 @@ private void drawTouchPoint(Canvas canvas,float x, float y, float radius){
811813
812814 /**
813815 * 创建画笔
814- * @param isEraser
816+ * @param drawMode
815817 * @return
816818 */
817- private Paint createPaint (boolean isEraser ){
819+ private Paint createPaint (int drawMode ){
820+
818821 Paint paint = new Paint ();
819- paint . setStyle ( Paint . Style . STROKE );
820- if ( isEraser ){ //当为橡皮擦模式时
822+ if ( drawMode == DrawMode . ERASER ) { // 当为橡皮擦模式时
823+ paint . setStyle ( Paint . Style . STROKE );
821824 paint .setColor (Color .BLACK );
822825 paint .setStrokeWidth (eraserStrokeWidth );
823826 paint .setAntiAlias (false );
824827 paint .setXfermode (new PorterDuffXfermode (PorterDuff .Mode .DST_OUT ));
825- }else {
828+ } else if (drawMode == DrawMode .DRAW_TEXT ) { // 当为绘制文本时
829+ paint .setStyle (Paint .Style .FILL_AND_STROKE );
830+ paint .setAntiAlias (true );
831+ paint .setColor (paintColor );
832+ paint .setTextSize (drawTextSize );
833+ paint .setFakeBoldText (isFakeBoldText );
834+ paint .setUnderlineText (isUnderlineText );
835+ } else {
836+ paint .setStyle (paintStyle );
826837 paint .setColor (paintColor );
827838 paint .setStrokeWidth (lineStrokeWidth );
828839 paint .setAntiAlias (true );
829840 paint .setStrokeJoin (Paint .Join .ROUND );
830841 paint .setStrokeCap (Paint .Cap .ROUND );
831- if (paintXfermode != null ){
842+
843+ if (paintShader != null ) {
844+ paint .setShader (paintShader );
845+ }
846+ if (paintXfermode != null ) {
832847 paint .setXfermode (paintXfermode );
833848 }
849+ if (pathEffect != null ) {
850+ paint .setPathEffect (pathEffect );
851+ }
852+ if (blendMode != null && Build .VERSION .SDK_INT >= Build .VERSION_CODES .Q ) {
853+ paint .setBlendMode (blendMode );
854+ }
834855 }
835856
836857 return paint ;
@@ -848,7 +869,7 @@ private Draw createDraw(@DrawMode int drawMode){
848869 try {
849870 Draw draw = drawClass .newInstance ();
850871 if (draw instanceof DrawText ){
851- ((DrawText ) draw ).setTextPaint (textPaint );
872+ ((DrawText ) draw ).setTextPaint (paint );
852873 ((DrawText ) draw ).setText (drawText );
853874 }else if (draw instanceof DrawBitmap ){
854875 ((DrawBitmap ) draw ).setBitmap (drawBitmap );
@@ -876,7 +897,7 @@ public boolean onTouchEvent(MotionEvent event) {
876897 if (isDrawEnabled && event .getPointerCount () == 1 ){//单指
877898 isZoom = false ;
878899 isDraw = false ;
879- paint = createPaint (drawMode == DrawMode . ERASER );
900+ paint = createPaint (drawMode );
880901 draw = createDraw (drawMode );
881902 draw .setPaint (paint );
882903 float x = event .getX ();
@@ -1269,10 +1290,37 @@ public int getTouchPointColor() {
12691290 return touchPointColor ;
12701291 }
12711292
1293+ @ NonNull
1294+ public Paint .Style getPaintStyle () {
1295+ return paintStyle ;
1296+ }
1297+
1298+ /**
1299+ * 设置画笔的 Paint.Style
1300+ * @param paintStyle
1301+ */
1302+ public void setPaintStyle (@ NonNull Paint .Style paintStyle ) {
1303+ this .paintStyle = paintStyle ;
1304+ }
1305+
1306+ @ Nullable
1307+ public Shader getPaintShader () {
1308+ return paintShader ;
1309+ }
1310+
1311+ /**
1312+ * 设置画笔的 Shader
1313+ * @param paintShader
1314+ */
1315+ public void setPaintShader (@ Nullable Shader paintShader ) {
1316+ this .paintShader = paintShader ;
1317+ }
1318+
12721319 /**
1273- * 获取画笔的Xfermode
1320+ * 获取画笔的 Xfermode
12741321 * @return
12751322 */
1323+ @ Nullable
12761324 public Xfermode getPaintXfermode () {
12771325 return paintXfermode ;
12781326 }
@@ -1281,10 +1329,38 @@ public Xfermode getPaintXfermode() {
12811329 * 设置画笔的 Xfermode
12821330 * @param xfermode
12831331 */
1284- public void setPaintXfermode (Xfermode xfermode ) {
1332+ public void setPaintXfermode (@ Nullable Xfermode xfermode ) {
12851333 this .paintXfermode = xfermode ;
12861334 }
12871335
1336+ @ Nullable
1337+ public PathEffect getPathEffect () {
1338+ return pathEffect ;
1339+ }
1340+
1341+ /**
1342+ * 设置画笔的 PathEffect
1343+ * @param pathEffect
1344+ */
1345+ public void setPathEffect (@ Nullable PathEffect pathEffect ) {
1346+ this .pathEffect = pathEffect ;
1347+ }
1348+
1349+ @ Nullable
1350+ public BlendMode getBlendMode () {
1351+ return blendMode ;
1352+ }
1353+
1354+ /**
1355+ * 设置画笔的 BlendMode
1356+ * @param blendMode
1357+ */
1358+ @ Nullable
1359+ @ RequiresApi (Build .VERSION_CODES .Q )
1360+ public void setBlendMode (@ Nullable BlendMode blendMode ) {
1361+ this .blendMode = blendMode ;
1362+ }
1363+
12881364 /**
12891365 * 设置触摸点的颜色
12901366 * @param touchPointColor
@@ -1298,15 +1374,15 @@ public void setTouchPointColor(int touchPointColor) {
12981374 * @return
12991375 */
13001376 public int getDrawTextColor () {
1301- return drawTextColor ;
1377+ return paintColor ;
13021378 }
13031379
13041380 /**
13051381 * 设置绘制文本的颜色
13061382 * @param drawTextColor
13071383 */
13081384 public void setDrawTextColor (int drawTextColor ) {
1309- this .drawTextColor = drawTextColor ;
1385+ this .paintColor = drawTextColor ;
13101386 }
13111387
13121388 /**
0 commit comments