diff --git a/README.md b/README.md index 80d7581..26c8ec2 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # DragPolygonView [![Download](https://img.shields.io/badge/download-App-blue.svg)](https://raw.githubusercontent.com/jenly1314/DragPolygonView/master/app/release/app-release.apk) -[![JCenter](https://img.shields.io/badge/JCenter-1.0.1-46C018.svg)](https://bintray.com/beta/#/jenly/maven/dragpolygonview) +[![JCenter](https://img.shields.io/badge/JCenter-1.0.2-46C018.svg)](https://bintray.com/beta/#/jenly/maven/dragpolygonview) [![JitPack](https://jitpack.io/v/jenly1314/DragPolygonView.svg)](https://jitpack.io/#jenly1314/DragPolygonView) [![CI](https://travis-ci.org/jenly1314/DragPolygonView.svg?branch=master)](https://travis-ci.org/jenly1314/DragPolygonView) [![CircleCI](https://circleci.com/gh/jenly1314/DragPolygonView.svg?style=svg)](https://circleci.com/gh/jenly1314/DragPolygonView) @@ -42,6 +42,12 @@ DragPolygonView for Android 是一个支持可拖动多边形,支持通过拖 | dpvMultipleSelection | boolean | false | 是否是多选模式,默认:单选模式 | | dpvClickToggleSelected | boolean | false | 是否点击就切换多边形的选中状态 | | dpvAllowDragOutView | boolean | false | 是否允许多边形拖出视图范围 | +| dpvTextSize | dimension | 16sp | 是否允许多边形拖出视图范围 | +| dpvTextNormalColor | color |#FFE5574C| 多边形文本的颜色 | +| dpvTextPressedColor | color | | 多边形文本按下状态的颜色 | +| dpvTextSelectedColor | color | | 多边形文本选中状态的颜色 | +| dpvShowText | boolean | true | 是否显示多边形的文本 | +| dpvFakeBoldText | boolean | false | 多边形Text的字体是否为粗体 | ## 引入 @@ -51,18 +57,18 @@ DragPolygonView for Android 是一个支持可拖动多边形,支持通过拖 com.king.view dragpolygonview - 1.0.1 + 1.0.2 pom ``` ### Gradle: ```gradle -implementation 'com.king.view:dragpolygonview:1.0.1' +implementation 'com.king.view:dragpolygonview:1.0.2' ``` ### Lvy: ```lvy - + ``` @@ -107,6 +113,9 @@ allprojects { ## 版本记录 +#### v1.0.2:2020-12-2 +* Polygon新增Text属性(可显示在多边形中间) + #### v1.0.1:2020-10-23 * 新增点击和长按事件 * 新增选中相关状态 diff --git a/app/release/app-release.apk b/app/release/app-release.apk index 63bdef1..4fa68db 100644 Binary files a/app/release/app-release.apk and b/app/release/app-release.apk differ diff --git a/app/release/output-metadata.json b/app/release/output-metadata.json index 01cbcaf..4415674 100644 --- a/app/release/output-metadata.json +++ b/app/release/output-metadata.json @@ -11,8 +11,8 @@ "type": "SINGLE", "filters": [], "properties": [], - "versionCode": 2, - "versionName": "1.0.1", + "versionCode": 3, + "versionName": "1.0.2", "enabled": true, "outputFile": "app-release.apk" } diff --git a/app/src/main/java/com/king/view/dragpolygonview/app/MainActivity.kt b/app/src/main/java/com/king/view/dragpolygonview/app/MainActivity.kt index 51ce579..b1a5ac6 100644 --- a/app/src/main/java/com/king/view/dragpolygonview/app/MainActivity.kt +++ b/app/src/main/java/com/king/view/dragpolygonview/app/MainActivity.kt @@ -19,7 +19,10 @@ class MainActivity : AppCompatActivity() { //三角形 dpv.addPolygon(PointF(514f, 80f), PointF(814f, 340f), PointF(214f, 340f)) //四边形 - dpv.addPolygon(Polygon(314f, 377f, 714f, 775f)) + val quadrilateral = Polygon(314f, 377f, 714f, 775f) + quadrilateral.text = "四边形" + dpv.addPolygon(quadrilateral) + //五边形 dpv.addPolygon(PointF(731f, 939f), PointF(652f, 1154f), PointF(403f, 1152f), PointF(322f, 931f), PointF(522f, 805f)) diff --git a/drag-polygon-view/src/main/java/com/king/view/dragpolygonview/DragPolygonView.java b/drag-polygon-view/src/main/java/com/king/view/dragpolygonview/DragPolygonView.java index 042592e..dd29032 100644 --- a/drag-polygon-view/src/main/java/com/king/view/dragpolygonview/DragPolygonView.java +++ b/drag-polygon-view/src/main/java/com/king/view/dragpolygonview/DragPolygonView.java @@ -8,7 +8,9 @@ import android.graphics.PointF; import android.os.Parcel; import android.os.Parcelable; +import android.text.TextUtils; import android.util.AttributeSet; +import android.util.Log; import android.util.TypedValue; import android.view.MotionEvent; import android.view.View; @@ -205,10 +207,39 @@ public class DragPolygonView extends View { */ private boolean isAllowDragOutView; + /** + * 文本字体大小 + */ + private float mTextSize; + + /** + * 文本的颜色 + */ + private int mTextNormalColor = 0xFFE5574C; + + /** + * 文本按下状态的颜色 + */ + private int mTextPressedColor; + + /** + * 文本选中状态的颜色 + */ + private int mTextSelectedColor; + + /** + * 是否显示多边形的Text + */ + private boolean isShowText = true; + + /** + * 多边形Text的字体是否为粗体 + */ + private boolean isFakeBoldText = false; + public DragPolygonView(Context context) { - super(context); - initDefaultValue(context); + this(context, null); } public DragPolygonView(Context context, AttributeSet attrs) { @@ -229,6 +260,7 @@ private void initDefaultValue(Context context){ mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop(); mLongPressTimeout = ViewConfiguration.getLongPressTimeout(); mAllowableOffsets = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,16,getResources().getDisplayMetrics()); + mTextSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,16,getResources().getDisplayMetrics()); } /** @@ -279,6 +311,18 @@ private void init(Context context,AttributeSet attrs, int defStyleAttr, int defS isClickToggleSelected = a.getBoolean(attr,isClickToggleSelected); }else if(attr == R.styleable.DragPolygonView_dpvAllowDragOutView){ isAllowDragOutView = a.getBoolean(attr,isAllowDragOutView); + }else if(attr == R.styleable.DragPolygonView_dpvTextSize){ + mTextSize = a.getDimension(attr,mTextSize); + }else if(attr == R.styleable.DragPolygonView_dpvTextNormalColor){ + mTextNormalColor = a.getColor(attr,mTextNormalColor); + }else if(attr == R.styleable.DragPolygonView_dpvTextPressedColor){ + mTextPressedColor = a.getColor(attr,mTextPressedColor); + }else if(attr == R.styleable.DragPolygonView_dpvTextSelectedColor){ + mTextSelectedColor = a.getColor(attr,mTextSelectedColor); + }else if(attr == R.styleable.DragPolygonView_dpvShowText){ + isShowText = a.getBoolean(attr,isShowText); + }else if(attr == R.styleable.DragPolygonView_dpvFakeBoldText){ + isFakeBoldText = a.getBoolean(attr,isFakeBoldText); } } a.recycle(); @@ -304,7 +348,7 @@ private void drawPolygons(Canvas canvas){ boolean isSelected; for(int i = 0; i < size; i++){ isSelected = isMultipleSelection ? mPolygonList.get(i).isSelected : i == mPolygonSelectPosition; - drawPolygon(canvas,mPolygonList.get(i).getPoints(),i == mPolygonPosition,isSelected); + drawPolygon(canvas,mPolygonList.get(i),i == mPolygonPosition,isSelected); } } @@ -312,7 +356,8 @@ private void drawPolygons(Canvas canvas){ * 绘制多边形 * @param canvas */ - private void drawPolygon(Canvas canvas,PointF[] points,boolean isPressed,boolean isSelected){ + private void drawPolygon(Canvas canvas,Polygon polygon,boolean isPressed,boolean isSelected){ + PointF[] points = polygon.getPoints(); if(points != null && points.length > 0){ int size = points.length; float[] lines = new float[size << 1]; @@ -348,6 +393,20 @@ private void drawPolygon(Canvas canvas,PointF[] points,boolean isPressed,boolean canvas.drawPoints(lines,mPaint); } + //绘制Text + if(isShowText && !TextUtils.isEmpty(polygon.getText())){ + mPaint.setStyle(Paint.Style.FILL); + mPaint.setFakeBoldText(isFakeBoldText); + mPaint.setTextSize(mTextSize); + mPaint.setColor(obtainColor(isPressed,isSelected,mTextNormalColor,mTextPressedColor,mTextSelectedColor)); + mPaint.setTextAlign(Paint.Align.CENTER); + Paint.FontMetrics fontMetrics = mPaint.getFontMetrics(); + float distance = (fontMetrics.bottom - fontMetrics.top)/2 - fontMetrics.bottom; + float centerX = (polygon.getLeftMostPoint().x + polygon.getRightMostPoint().x) / 2; + float centerY = (polygon.getTopMostPoint().y + polygon.getBottomMostPoint().y) / 2 + distance; + canvas.drawText(polygon.getText(),centerX,centerY,mPaint); + } + } } @@ -374,6 +433,7 @@ private int obtainColor(boolean isPressed,boolean isSelected,int normalColor,int public boolean onTouchEvent(MotionEvent event) { mEventX = event.getX(); mEventY = event.getY(); + Log.d("Jenly",mEventX +" , " + mEventY); isIntercept = event.getPointerCount() == 1; switch (event.getAction()){ case MotionEvent.ACTION_DOWN: @@ -1027,6 +1087,59 @@ public void setFillSelectedColor(int fillSelectedColor) { invalidate(); } + /** + * 设置文本字体大小 + * @param textSize + */ + public void setTextSize(float textSize) { + this.mTextSize = textSize; + invalidate(); + } + + /** + * 设置文本的颜色 + * @param textNormalColor + */ + public void setTextNormalColor(int textNormalColor) { + this.mTextNormalColor = textNormalColor; + invalidate(); + } + + /** + * 设置文本按下状态的颜色 + * @param textPressedColor + */ + public void setTextPressedColor(int textPressedColor) { + this.mTextPressedColor = textPressedColor; + } + + /** + * 设置文本选中状态的颜色 + * @param textSelectedColor + */ + public void setTextSelectedColor(int textSelectedColor) { + this.mTextSelectedColor = textSelectedColor; + invalidate(); + } + + /** + * 设置是否显示多边形的Text + * @param showText + */ + public void setShowText(boolean showText) { + isShowText = showText; + invalidate(); + } + + /** + * 设置多边形Text的字体是否为粗体 + * @param fakeBoldText + */ + public void setFakeBoldText(boolean fakeBoldText) { + isFakeBoldText = fakeBoldText; + invalidate(); + } + /** * 设置允许的触点误差偏移量 */ @@ -1219,6 +1332,8 @@ public static class Polygon implements Parcelable { */ boolean isSelected; + private String mText; + /** * 构造 * @param left 矩形左边的X坐标 @@ -1306,6 +1421,13 @@ public int getBottomMostPointIndex() { return mBottomMostPointIndex; } + /** + * 更新边界点(分别是最左,最右,最上,最下的点坐标信息) + */ + private void updateBoundaryPoints(){ + updateBoundaryPoints(mPoints); + } + /** * 更新边界点(分别是最左,最右,最上,最下的点坐标信息) * @param points @@ -1345,6 +1467,7 @@ private synchronized void updateBoundaryPoints(PointF point,int position){ } } + /** * 设置多边形坐标点信息,设置时,请确保至少三个点以上才能组成一个多边形 * @param points @@ -1352,7 +1475,7 @@ private synchronized void updateBoundaryPoints(PointF point,int position){ public void setPoints(PointF... points){ mPoints = points; size = mPoints.length; - updateBoundaryPoints(points); + updateBoundaryPoints(); } /** @@ -1379,7 +1502,7 @@ public PointF[] getPoints(){ public void updatePoint(PointF point,int position){ if(position < size){ mPoints[position] = point; - updateBoundaryPoints(point,position); + updateBoundaryPoints(); } } @@ -1393,10 +1516,18 @@ public void updatePoint(float x,float y,int position){ if(position < size){ mPoints[position].x = x; mPoints[position].y = y; - updateBoundaryPoints(mPoints[position],position); + updateBoundaryPoints(); } } + public void setText(String text){ + this.mText = text; + } + + public String getText(){ + return mText; + } + @Override public String toString() { @@ -1423,6 +1554,7 @@ public void writeToParcel(Parcel dest, int flags) { dest.writeInt(this.mTopMostPointIndex); dest.writeInt(this.mRightMostPointIndex); dest.writeInt(this.mBottomMostPointIndex); + dest.writeString(this.mText); } protected Polygon(Parcel in) { @@ -1436,6 +1568,7 @@ protected Polygon(Parcel in) { this.mTopMostPointIndex = in.readInt(); this.mRightMostPointIndex = in.readInt(); this.mBottomMostPointIndex = in.readInt(); + this.mText = in.readString(); } public static final Creator CREATOR = new Creator() { diff --git a/drag-polygon-view/src/main/res/values/attrs.xml b/drag-polygon-view/src/main/res/values/attrs.xml index d2be4ce..be1f397 100644 --- a/drag-polygon-view/src/main/res/values/attrs.xml +++ b/drag-polygon-view/src/main/res/values/attrs.xml @@ -18,5 +18,11 @@ + + + + + + \ No newline at end of file diff --git a/versions.gradle b/versions.gradle index 1f42a63..19b899f 100644 --- a/versions.gradle +++ b/versions.gradle @@ -1,7 +1,7 @@ //App def app_version = [:] -app_version.versionCode = 2 -app_version.versionName = "1.0.1" +app_version.versionCode = 3 +app_version.versionName = "1.0.2" ext.app_version = app_version //build version