Skip to content

Commit

Permalink
update library
Browse files Browse the repository at this point in the history
  • Loading branch information
shellljx committed Jan 3, 2017
1 parent f0b8c2d commit cbf3171
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 45 deletions.
9 changes: 5 additions & 4 deletions library/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.licrafter.tagview">

<application android:allowBackup="true"
android:label="@string/app_name"
android:supportsRtl="true"
>
<application
android:allowBackup="true"
android:label="@string/app_name"
android:supportsRtl="true"
>

</application>

Expand Down
45 changes: 42 additions & 3 deletions library/src/main/java/com/licrafter/tagview/DIRECTION.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,46 @@
*/

public enum DIRECTION {
LEFT_CENTER, LEFT_TOP, LEFT_TOP_TILT, LEFT_BOTTOM, LEFT_BOTTOM_TILT,
RIGHT_CENTER, RIGHT_TOP, RIGHT_TOP_TILT, RIGHT_BOTTOM, RIGHT_BOTTOM_TILT,
CENTER
LEFT_CENTER(8), LEFT_TOP(10), LEFT_TOP_TILT(9), LEFT_BOTTOM(6), LEFT_BOTTOM_TILT(7),
RIGHT_CENTER(3), RIGHT_TOP(1), RIGHT_TOP_TILT(2), RIGHT_BOTTOM(5), RIGHT_BOTTOM_TILT(4),
CENTER(0);

private int value;

DIRECTION(int value) {
this.value = value;
}

public int getValue() {
return value;
}

public static DIRECTION valueOf(int value) {
switch (value) {
case 0:
return CENTER;
case 1:
return RIGHT_TOP;
case 2:
return RIGHT_TOP_TILT;
case 3:
return RIGHT_CENTER;
case 4:
return RIGHT_BOTTOM_TILT;
case 5:
return RIGHT_BOTTOM;
case 6:
return LEFT_BOTTOM;
case 7:
return LEFT_BOTTOM_TILT;
case 8:
return LEFT_CENTER;
case 9:
return LEFT_TOP_TILT;
case 10:
return LEFT_TOP;
default:
return CENTER;
}
}
}
121 changes: 94 additions & 27 deletions library/src/main/java/com/licrafter/tagview/TagViewGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
import com.licrafter.tagview.views.ITagView;
import com.licrafter.tagview.views.RippleView;

import java.util.ArrayList;
import java.util.List;


/**
* author: shell
Expand All @@ -33,6 +36,7 @@ public class TagViewGroup extends ViewGroup {
public static final int DEFAULT_V_DISTANCE = 28;//默认竖直(上/下)方向线条长度
public static final int DEFAULT_TILT_DISTANCE = 20;//默认斜线长度
public static final int DEFAULT_BODER_WIDTH = 1;//默认线宽
public static final int DEFAULT_MAX_TAG = 6;//默认标签最大数量

private Paint mPaint;
private Path mPath;
Expand All @@ -41,7 +45,7 @@ public class TagViewGroup extends ViewGroup {
private Animator mShowAnimator;
private Animator mHideAnimator;
private GestureDetectorCompat mGestureDetector;
private TagGroupClickListener mClickListener;
private OnTagGroupClickListener mClickListener;

private RippleView mRippleView;
private int mRadius;//外圆半径
Expand Down Expand Up @@ -289,15 +293,64 @@ private boolean checkAnimating() {
|| mShowAnimator.isRunning() || mHideAnimator.isRunning();
}

/**
* 添加 Tag 列表
*
* @param tagList 要添加的 Tag 列表
* @return 返回 标签组
*/
public TagViewGroup addTagList(@NonNull List<ITagView> tagList) {
for (ITagView tag : tagList) {
addTag(tag);
}
return this;
}

/**
* 添加单个 Tag
*
* @param tag 要添加的 Tag
* @return 返回 标签组
*/
public TagViewGroup addTag(@NonNull ITagView tag) {
View tagView = ((View) tag);
tagView.setTag(mTagCount);
if (mTagCount >= DEFAULT_MAX_TAG) {
throw new RuntimeException("The number of tags exceeds the maximum value(6)");
}
tag.setTag(mTagCount);
addView((View) tag);
mRectArray[mTagCount] = new RectF();
addView(tagView);
mTagCount++;
return this;
}

/**
* 得到 TagViewGroup 中的所有标签列表
*
* @return
*/
public List<ITagView> getTagList() {
List<ITagView> list = new ArrayList<>();
for (int i = 0; i < getChildCount(); i++) {
ITagView tag = (ITagView) getChildAt(i);
if (tag.getDirection() != DIRECTION.CENTER) {
list.add(tag);
}
}
return list;
}

/**
* 得到 TagViewGroup 中的标签数量
*
* @return
*/
public int getTagCount() {
return mTagCount;
}

/**
* 添加水波纹
*/
public void addRipple() {
mRippleView = new RippleView(getContext());
mRippleView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
Expand All @@ -307,21 +360,32 @@ public void addRipple() {

private void refreshTagsRect() {
for (int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
if (((ITagView) child).getDirection() != DIRECTION.CENTER) {
mRectArray[(int) child.getTag()].set(child.getLeft(), child.getTop(), child.getRight(), child.getBottom());
ITagView child = (ITagView) getChildAt(i);
if (child.getDirection() != DIRECTION.CENTER) {
int index = (int) child.getTag();
if (mRectArray[index] == null) {
mRectArray[index] = new RectF();
}
mRectArray[index].set(child.getLeft(), child.getTop(), child.getRight(), child.getBottom());
}
}
}

private boolean isTouchingTags(float x, float y) {
/**
* 检测 Touch 事件发生在哪个 Tag 上
*
* @param x
* @param y
* @return
*/
private ITagView isTouchingTags(float x, float y) {
for (int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
if (((ITagView) child).getDirection() != DIRECTION.CENTER && mRectArray[(int) child.getTag()].contains(x, y)) {
return true;
ITagView child = (ITagView) getChildAt(i);
if (child.getDirection() != DIRECTION.CENTER && mRectArray[(int) child.getTag()].contains(x, y)) {
return child;
}
}
return false;
return null;
}

//设置中心圆点坐标占整个 ViewGroup 的比例
Expand Down Expand Up @@ -400,33 +464,34 @@ public boolean onTouchEvent(MotionEvent event) {
return super.onTouchEvent(event);
}

public void setTagGroupClickListener(TagGroupClickListener listener) {
public void setOnTagGroupClickListener(OnTagGroupClickListener listener) {
mClickListener = listener;
}

public interface TagGroupClickListener {
public interface OnTagGroupClickListener {

//TagGroup 中心圆点被点击
void onCircleClicked();
void onCircleClick(TagViewGroup group);

//TagGroup Tag子view被点击
void onTagsClicked();
void onTagClick(TagViewGroup group, ITagView tag, int index);

//TagGroup 被长按
void onLongPress();
void onLongPress(TagViewGroup group);

//TagGroup 移动
void onScroll(TagViewGroup group, float percentX, float percentY);
}

//内部处理 touch 事件监听器
private class TagOnGestureListener extends GestureDetector.SimpleOnGestureListener {

@Override
public boolean onDown(MotionEvent e) {
if (mClickListener != null) {
float x = e.getX();
float y = e.getY();
if (mCenterRect.contains(x, y) || isTouchingTags(x, y)) {
return true;
}
float x = e.getX();
float y = e.getY();
if (mCenterRect.contains(x, y) || isTouchingTags(x, y) != null) {
return true;
}
return super.onDown(e);
}
Expand All @@ -437,9 +502,10 @@ public boolean onSingleTapUp(MotionEvent e) {
float y = e.getY();

if (mCenterRect.contains(x, y)) {
mClickListener.onCircleClicked();
mClickListener.onCircleClick(TagViewGroup.this);
} else {
mClickListener.onTagsClicked();
ITagView clickedTag = isTouchingTags(x, y);
mClickListener.onTagClick(TagViewGroup.this, clickedTag, (int) clickedTag.getTag());
}
return true;
}
Expand All @@ -453,6 +519,7 @@ public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float d
mPercentX = currentX / getMeasuredWidth();
mPercentY = currentY / getMeasuredHeight();
requestLayout();
mClickListener.onScroll(TagViewGroup.this, mPercentX, mPercentY);
return true;
}

Expand All @@ -461,8 +528,8 @@ public void onLongPress(MotionEvent e) {
super.onLongPress(e);
float x = e.getX();
float y = e.getY();
if (mCenterRect.contains(x, y) || isTouchingTags(x, y)) {
mClickListener.onLongPress();
if (mCenterRect.contains(x, y) || isTouchingTags(x, y) != null) {
mClickListener.onLongPress(TagViewGroup.this);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package com.licrafter.tagview.utils;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.view.View;
import android.view.animation.DecelerateInterpolator;

import com.licrafter.tagview.TagViewGroup;
Expand Down
24 changes: 15 additions & 9 deletions library/src/main/java/com/licrafter/tagview/views/ITagView.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,29 @@
public interface ITagView {

//设置Tag的方向
void setDirection(DIRECTION direction);
void setDirection(DIRECTION direction);

//得到Tag的方向
DIRECTION getDirection();
DIRECTION getDirection();

int getMeasuredWidth();
int getMeasuredWidth();

int getMeasuredHeight();
int getMeasuredHeight();

int getTop();
int getTop();

int getLeft();
int getLeft();

int getRight();
int getRight();

int getBottom();
int getBottom();

void layout(int left, int top, int right, int bottom);
Object getTag();

void setTag(Object tag);

void setTag(int key, Object tag);

void layout(int left, int top, int right, int bottom);

}

0 comments on commit cbf3171

Please sign in to comment.