Skip to content

Commit

Permalink
* Polygon新增Text属性(可显示在多边形中间)
Browse files Browse the repository at this point in the history
  • Loading branch information
jenly1314 committed Dec 2, 2020
1 parent 3093b17 commit df6deba
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 16 deletions.
17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -42,6 +42,12 @@ DragPolygonView for Android 是一个支持可拖动多边形,支持通过拖
| dpvMultipleSelection | boolean | false | 是否是多选模式,默认:单选模式 |
| dpvClickToggleSelected | boolean | false | 是否点击就切换多边形的选中状态 |
| dpvAllowDragOutView | boolean | false | 是否允许多边形拖出视图范围 |
| dpvTextSize | dimension | 16sp | 是否允许多边形拖出视图范围 |
| dpvTextNormalColor | color |<font color=#E5574C>#FFE5574C</font>| 多边形文本的颜色 |
| dpvTextPressedColor | color | | 多边形文本按下状态的颜色 |
| dpvTextSelectedColor | color | | 多边形文本选中状态的颜色 |
| dpvShowText | boolean | true | 是否显示多边形的文本 |
| dpvFakeBoldText | boolean | false | 多边形Text的字体是否为粗体 |


## 引入
Expand All @@ -51,18 +57,18 @@ DragPolygonView for Android 是一个支持可拖动多边形,支持通过拖
<dependency>
<groupId>com.king.view</groupId>
<artifactId>dragpolygonview</artifactId>
<version>1.0.1</version>
<version>1.0.2</version>
<type>pom</type>
</dependency>
```
### Gradle:
```gradle
implementation 'com.king.view:dragpolygonview:1.0.1'
implementation 'com.king.view:dragpolygonview:1.0.2'
```

### Lvy:
```lvy
<dependency org='com.king.view' name='dragpolygonview' rev='1.0.1'>
<dependency org='com.king.view' name='dragpolygonview' rev='1.0.2'>
<artifact name='$AID' ext='pom'></artifact>
</dependency>
```
Expand Down Expand Up @@ -107,6 +113,9 @@ allprojects {

## 版本记录

#### v1.0.2:2020-12-2
* Polygon新增Text属性(可显示在多边形中间)

#### v1.0.1:2020-10-23
* 新增点击和长按事件
* 新增选中相关状态
Expand Down
Binary file modified app/release/app-release.apk
Binary file not shown.
4 changes: 2 additions & 2 deletions app/release/output-metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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());
}

/**
Expand Down Expand Up @@ -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();
Expand All @@ -304,15 +348,16 @@ 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);
}
}

/**
* 绘制多边形
* @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];
Expand Down Expand Up @@ -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);
}

}
}

Expand All @@ -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:
Expand Down Expand Up @@ -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();
}

/**
* 设置允许的触点误差偏移量
*/
Expand Down Expand Up @@ -1219,6 +1332,8 @@ public static class Polygon implements Parcelable {
*/
boolean isSelected;

private String mText;

/**
* 构造
* @param left 矩形左边的X坐标
Expand Down Expand Up @@ -1306,6 +1421,13 @@ public int getBottomMostPointIndex() {
return mBottomMostPointIndex;
}

/**
* 更新边界点(分别是最左,最右,最上,最下的点坐标信息)
*/
private void updateBoundaryPoints(){
updateBoundaryPoints(mPoints);
}

/**
* 更新边界点(分别是最左,最右,最上,最下的点坐标信息)
* @param points
Expand Down Expand Up @@ -1345,14 +1467,15 @@ private synchronized void updateBoundaryPoints(PointF point,int position){
}
}


/**
* 设置多边形坐标点信息,设置时,请确保至少三个点以上才能组成一个多边形
* @param points
*/
public void setPoints(PointF... points){
mPoints = points;
size = mPoints.length;
updateBoundaryPoints(points);
updateBoundaryPoints();
}

/**
Expand All @@ -1379,7 +1502,7 @@ public PointF[] getPoints(){
public void updatePoint(PointF point,int position){
if(position < size){
mPoints[position] = point;
updateBoundaryPoints(point,position);
updateBoundaryPoints();
}
}

Expand All @@ -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() {
Expand All @@ -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) {
Expand All @@ -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<Polygon> CREATOR = new Creator<Polygon>() {
Expand Down
6 changes: 6 additions & 0 deletions drag-polygon-view/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,11 @@
<attr name="dpvMultipleSelection" format="boolean"/>
<attr name="dpvClickToggleSelected" format="boolean"/>
<attr name="dpvAllowDragOutView" format="boolean"/>
<attr name="dpvTextSize" format="dimension"/>
<attr name="dpvTextNormalColor" format="color"/>
<attr name="dpvTextPressedColor" format="color"/>
<attr name="dpvTextSelectedColor" format="color"/>
<attr name="dpvShowText" format="boolean"/>
<attr name="dpvFakeBoldText" format="boolean"/>
</declare-styleable>
</resources>
4 changes: 2 additions & 2 deletions versions.gradle
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit df6deba

Please sign in to comment.