Skip to content

Commit cbd8237

Browse files
committed
对外提供更多Paint相关配置
1 parent 18878d3 commit cbd8237

File tree

8 files changed

+120
-46
lines changed

8 files changed

+120
-46
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ DrawBoard 是一个自定义 View实现的画板;方便对图片进行各种
3535
| android:src | drawable | | 设置图片(画板背景图层) |
3636
| dbvPaintColor | color |<font color=#FF0000>#FFFF0000</font>| 画笔的颜色 |
3737
| dbvTouchPointColor | color |<font color=#CCCCCC>#AFCCCCCC</font>| 触摸点的颜色 |
38-
| dbvDrawTextColor | color |<font color=#FF0000>#FFFF0000</font>| 绘制文本的颜色 |
3938
| dbvDrawTextSize | dimension | 15sp | 绘制文本的字体大小 |
4039
| dbvDrawTextBold | boolean | false | 绘制文本是否是粗体 |
4140
| dbvDrawTextUnderline | boolean | false | 绘制文本是否需要下划线 |
@@ -60,7 +59,7 @@ allprojects {
6059

6160
2. 在Module的 **build.gradle** 里面添加引入依赖项
6261
```gradle
63-
implementation 'com.github.jenly1314:drawboard:1.0.0'
62+
implementation 'com.github.jenly1314:drawboard:1.0.1'
6463
6564
```
6665

@@ -168,6 +167,9 @@ drawBoardView.getImageBitmap()
168167

169168
## 版本记录
170169

170+
#### v1.0.1:2022-2-17
171+
* 对外提供更多Paint相关配置
172+
171173
#### v1.0.0:2021-11-30
172174
* DrawBoard初始版本
173175

app/release/app-release.apk

209 KB
Binary file not shown.

app/release/output-metadata.json

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": 3,
2+
"version": 2,
33
"artifactType": {
44
"type": "APK",
55
"kind": "Directory"
@@ -10,11 +10,9 @@
1010
{
1111
"type": "SINGLE",
1212
"filters": [],
13-
"attributes": [],
14-
"versionCode": 1,
15-
"versionName": "1.0.0",
13+
"versionCode": 2,
14+
"versionName": "1.0.1",
1615
"outputFile": "app-release.apk"
1716
}
18-
],
19-
"elementType": "File"
17+
]
2018
}

app/src/main/java/com/king/drawboard/app/MainActivity.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import kotlinx.coroutines.withContext
2525
import java.io.File
2626
import java.io.FileOutputStream
2727
import java.lang.Exception
28-
import kotlin.math.log
2928

3029
class MainActivity : AppCompatActivity() {
3130

drawboard/src/main/java/com/king/drawboard/view/DrawBoardView.java

Lines changed: 108 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,20 @@
44
import android.content.res.TypedArray;
55
import android.graphics.Bitmap;
66
import android.graphics.BitmapFactory;
7+
import android.graphics.BlendMode;
78
import android.graphics.Canvas;
89
import android.graphics.Color;
910
import android.graphics.Matrix;
1011
import android.graphics.Paint;
12+
import android.graphics.PathEffect;
1113
import android.graphics.PixelFormat;
1214
import android.graphics.PointF;
1315
import android.graphics.PorterDuff;
1416
import android.graphics.PorterDuffXfermode;
17+
import android.graphics.Shader;
1518
import android.graphics.Xfermode;
1619
import android.graphics.drawable.Drawable;
17-
import android.text.TextPaint;
20+
import android.os.Build;
1821
import android.util.AttributeSet;
1922
import android.util.DisplayMetrics;
2023
import android.util.TypedValue;
@@ -45,6 +48,7 @@
4548
import androidx.annotation.IntDef;
4649
import androidx.annotation.NonNull;
4750
import 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
/**

drawboard/src/main/res/values/attrs.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
<attr name="android:src" />
1212
<attr name="dbvPaintColor" format="color"/>
1313
<attr name="dbvTouchPointColor" format="color"/>
14-
<attr name="dbvDrawTextColor" format="color"/>
1514
<attr name="dbvDrawTextSize" format="dimension"/>
1615
<attr name="dbvDrawTextBold" format="boolean"/>
1716
<attr name="dbvDrawTextUnderline" format="boolean"/>

gradle.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ android.enableJetifier = true
2121
kotlin.code.style = official
2222

2323
GROUP=com.github.jenly1314
24-
VERSION_NAME=1.0.0
25-
VERSION_CODE=1
24+
VERSION_NAME=1.0.1
25+
VERSION_CODE=2
2626

2727
POM_DESCRIPTION=DrawBoard for Android
2828
POM_INCEPTION_YEAR=2021

versions.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//App
22
def app_version = [:]
3-
app_version.versionCode = 1
4-
app_version.versionName = "1.0.0"
3+
app_version.versionCode = 2
4+
app_version.versionName = "1.0.1"
55
ext.app_version = app_version
66

77
//build version

0 commit comments

Comments
 (0)