45 |
46 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/library/src/main/java/cn/label/library/LabelView.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2016 yanbo
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package cn.label.library;
25 |
26 | import android.content.Context;
27 | import android.graphics.Canvas;
28 | import android.util.AttributeSet;
29 | import android.view.View;
30 | /**
31 | * A View's Label implementation View.
32 | */
33 | public class LabelView extends View {
34 | private LabelViewHelper mLabelViewHelper;
35 |
36 | public LabelView(Context context) {
37 | this(context, null);
38 | }
39 |
40 | public LabelView(Context context, AttributeSet attrs) {
41 | this(context, attrs, 0);
42 | }
43 |
44 | public LabelView(Context context, AttributeSet attrs, int defStyleAttr) {
45 | super(context, attrs, defStyleAttr);
46 |
47 | mLabelViewHelper = new LabelViewHelper(context, attrs);
48 | }
49 |
50 | public void setTextContent(String content) {
51 | mLabelViewHelper.setTextContent(content);
52 | invalidate();
53 | }
54 |
55 | public void setTextTitle(String title) {
56 | mLabelViewHelper.setTextTitle(title);
57 | invalidate();
58 | }
59 |
60 | public void setLabelBackGroundColor(int color) {
61 | mLabelViewHelper.setLabelBackGroundColor(color);
62 | invalidate();
63 | }
64 |
65 | @Override
66 | protected void onDraw(Canvas canvas) {
67 | super.onDraw(canvas);
68 | mLabelViewHelper.drawLabel(this, canvas);
69 | }
70 |
71 | @Override
72 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
73 | super.onMeasure(widthMeasureSpec, heightMeasureSpec);
74 | int rotateViewWH= (int) (mLabelViewHelper.getBgTriangleHeight() * Math.sqrt(2));
75 | setMeasuredDimension(rotateViewWH, rotateViewWH);
76 | }
77 | }
78 |
--------------------------------------------------------------------------------
/app/src/main/java/cn/label/avatarlabelview/LabelImageView.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2016 yanbo
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package cn.label.avatarlabelview;
25 |
26 | import android.content.Context;
27 | import android.graphics.Canvas;
28 | import android.util.AttributeSet;
29 | import android.widget.ImageView;
30 | import cn.label.library.LabelViewHelper;
31 | /**
32 | * A ImageView's Label implementation View.
33 | * Other View's Label implement can refer to this.
34 | */
35 | public class LabelImageView extends ImageView {
36 | private LabelViewHelper mLabelViewHelper;
37 | private boolean mLabelVisable = true;
38 |
39 | public LabelImageView(Context context) {
40 | this(context, null);
41 | }
42 |
43 | public LabelImageView(Context context, AttributeSet attrs) {
44 | this(context, attrs, 0);
45 | }
46 |
47 | public LabelImageView(Context context, AttributeSet attrs, int defStyleAttr) {
48 | super(context, attrs, defStyleAttr);
49 | mLabelViewHelper = new LabelViewHelper(context, attrs);
50 | }
51 |
52 | @Override
53 | protected void onDraw(Canvas canvas) {
54 | super.onDraw(canvas);
55 | if (mLabelVisable) {
56 | mLabelViewHelper.drawLabel(this, canvas);
57 | }
58 | }
59 |
60 | public void setTextContent(String content) {
61 | mLabelViewHelper.setTextContent(content);
62 | invalidate();
63 | }
64 |
65 | public void setTextTitle(String title) {
66 | mLabelViewHelper.setTextTitle(title);
67 | invalidate();
68 | }
69 |
70 | public void setLabelBackGroundColor(int color) {
71 | mLabelViewHelper.setLabelBackGroundColor(color);
72 | invalidate();
73 | }
74 |
75 | public void setLabelVisable(boolean visable) {
76 | mLabelVisable = visable;
77 | postInvalidate();
78 | }
79 | }
80 |
--------------------------------------------------------------------------------
/app/src/main/java/cn/label/avatarlabelview/LabelLinearLayout.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2016 yanbo
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package cn.label.avatarlabelview;
25 |
26 | import android.content.Context;
27 | import android.graphics.Canvas;
28 | import android.util.AttributeSet;
29 | import android.widget.LinearLayout;
30 | import cn.label.library.LabelViewHelper;
31 | /**
32 | * A ViewGroup's Label implementation View.
33 | * Other ViewGroup's Label implement can refer to this.
34 | */
35 | public class LabelLinearLayout extends LinearLayout {
36 | private LabelViewHelper mLabelViewHelper;
37 | private boolean mLabelVisable = true;
38 |
39 | public LabelLinearLayout(Context context) {
40 | this(context, null);
41 | }
42 |
43 | public LabelLinearLayout(Context context, AttributeSet attrs) {
44 | this(context, attrs, 0);
45 | }
46 |
47 | public LabelLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
48 | super(context, attrs, defStyleAttr);
49 | mLabelViewHelper = new LabelViewHelper(context, attrs);
50 | }
51 |
52 | @Override
53 | protected void dispatchDraw(Canvas canvas) {
54 | super.dispatchDraw(canvas);
55 | if (mLabelVisable) {
56 | mLabelViewHelper.drawLabel(this, canvas);
57 | }
58 | }
59 |
60 | public void setTextContent(String content) {
61 | mLabelViewHelper.setTextContent(content);
62 | invalidate();
63 | }
64 |
65 | public void setTextTitle(String title) {
66 | mLabelViewHelper.setTextTitle(title);
67 | invalidate();
68 | }
69 |
70 | public void setLabelBackGroundColor(int color) {
71 | mLabelViewHelper.setLabelBackGroundColor(color);
72 | invalidate();
73 | }
74 |
75 | public void setLabelVisable(boolean visable) {
76 | mLabelVisable = visable;
77 | postInvalidate();
78 | }
79 | }
80 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # AvatarLabelView
2 |
3 | 一个可配置的迷你版轻量级 Label 辅助类,支持多种配置效果,具体不同配置展示效果如下图。
4 |
5 |
6 |
7 | # 说明文档
8 |
9 | 如下是关于 Label View 的相关使用方式、属性说明、拓展自定义的解释说明。
10 |
11 | ### 使用样例
12 |
13 | ```xml
14 |
26 | ```
27 |
28 | ### 已实现类说明
29 |
30 | | 类别 | 类名 | 说明 |
31 | | ----- | ----- | ----- |
32 | | library | LabelViewHelper | 标签辅助核心实现类 |
33 | | library | LabelView | 基于 LabelViewHelper 实现的一个纯标签 View,可嵌套在 ViewGroup 中使用等 |
34 | | demo | LabelImageView | 基于 LabelViewHelper 实现的一个具备标签的 ImageView,可属性配置等 |
35 | | demo | LabelLinearLayout | 基于 LabelViewHelper 实现的一个具备标签的 LinearLayout,可属性配置等 |
36 | | customer | XxxView | 类比上面 demo 中基于 LabelViewHelper 实现自己的 Label View |
37 |
38 | ### 属性说明
39 |
40 | | 属性 | 含义 |
41 | | ----- | ----- |
42 | |app:backgroundColor | Label 的背景颜色 |
43 | |app:textTitleColor | 第一行文字的颜色,如果 Label 作为单行(不设置textTitle)则无效 |
44 | |app:textContentColor | 第二行文字的颜色 |
45 | |app:textTitle | 第一行文字的内容,如果文字过长注意调节 labelTopPadding 的值变大,单行 Label 时不要设置此值 |
46 | |app:textContent | 第二行文字的内容,单行显示时推荐用此 |
47 | |app:textTitleSize | 第一行文字的大小,默认10sp,如果 Label 作为单行(不设置textTitle)则无效 |
48 | |app:textContentSize | 第二行文字的大小,默认12sp |
49 | |app:labelTopPadding | 第一行文字上边缘距离背景顶部(注意三角形或者梯形)的偏移量,默认为15dp |
50 | |app:labelCenterPadding | 第一行文字底部与第二行文字顶部之间的偏移距离 |
51 | |app:labelBottomPadding | textContent 文字与 Label 背景底部的空隙距离,默认为 10dp |
52 | |app:labelTopDistance | 当设置该值大于0时显示的 Label 为梯形的样式,梯形上顶宽度与该值成正比;当不设置时 Label 为三角形样式 |
53 | |app:textTitleStyle | 第一行文字的样式,normal、italic、bold,如果 Label 作为单行(不设置textTitle)则无效 |
54 | |app:textContentStyle | 第二行文字的样式,normal、italic、bold |
55 | |app:direction | Label 的位置,leftTop 或者 rightTop |
56 |
57 | ### 拓展为自己 View 使用方式
58 |
59 | 1. 在自己的自定义 View 构造方法创建 LabelViewHelper 对象。
60 | 2. 在自己的自定义 View 相关绘制方法(onDraw、dispatchDraw 等)中调用 LabelViewHelper 的 drawLabel 方法。
61 | 3. 至此你的自定义 View 就支持可配置的 Label 效果了,如需别的拓展可以参考 demo 或者查看 LabelViewHelper 其他方法。
62 |
63 | 具体拓展应用到自己自定义的其他控件中如下:
64 |
65 | ```java
66 | public class LabelImageView extends YourCustomerView {
67 | //......
68 | public LabelImageView(Context context, AttributeSet attrs, int defStyleAttr) {
69 | super(context, attrs, defStyleAttr);
70 | mLabelViewHelper = new LabelViewHelper(context, attrs);
71 | }
72 |
73 | //注意:ViewGroup 最好重写 dispatchDraw 方法
74 | @Override
75 | protected void onDraw(Canvas canvas) {
76 | super.onDraw(canvas);
77 | mLabelViewHelper.drawLabel(this, canvas);
78 | }
79 | //......
80 | }
81 | ```
82 |
83 | # License 声明
84 |
85 | MIT License
86 |
87 | Copyright (c) 2016 yanbo
88 |
89 | Permission is hereby granted, free of charge, to any person obtaining a copy
90 | of this software and associated documentation files (the "Software"), to deal
91 | in the Software without restriction, including without limitation the rights
92 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
93 | copies of the Software, and to permit persons to whom the Software is
94 | furnished to do so, subject to the following conditions:
95 |
96 | The above copyright notice and this permission notice shall be included in all
97 | copies or substantial portions of the Software.
98 |
99 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
100 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
101 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
102 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
103 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
104 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
105 | SOFTWARE.
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
13 |
14 |
25 |
26 |
39 |
40 |
54 |
55 |
67 |
68 |
81 |
82 |
94 |
95 |
104 |
105 |
113 |
114 |
126 |
127 |
139 |
140 |
154 |
159 |
164 |
165 |
166 |
172 |
183 |
188 |
189 |
190 |
191 |
--------------------------------------------------------------------------------
/library/src/main/java/cn/label/library/LabelViewHelper.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2016 yanbo
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in all
14 | * copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | * SOFTWARE.
23 | */
24 | package cn.label.library;
25 |
26 | import android.content.Context;
27 | import android.content.res.TypedArray;
28 | import android.graphics.Canvas;
29 | import android.graphics.Color;
30 | import android.graphics.Paint;
31 | import android.graphics.Path;
32 | import android.graphics.Rect;
33 | import android.graphics.Typeface;
34 | import android.text.TextUtils;
35 | import android.util.AttributeSet;
36 | import android.view.View;
37 |
38 | /**
39 | * A tool class that adds a label to the various Views.
40 | */
41 | public final class LabelViewHelper {
42 | private static final int ROTATE_LEFT = -45;
43 | private static final int ROTATE_RIGHT = 45;
44 |
45 | private static final int STYLE_NORMAL = 0;
46 | private static final int STYLE_ITALIC = 1;
47 | private static final int STYLE_BOLD = 2;
48 |
49 | private Paint mTextTitlePaint;
50 | private int mTextTitleColor;
51 | private float mTextTitleSize;
52 | private Rect mTextTitleRect;
53 | private int mTextTitleStyle;
54 |
55 | private Paint mTextContentPaint;
56 | private int mTextContentColor;
57 | private float mTextContentSize;
58 | private Rect mTextContentRect;
59 | private int mTextContentStyle;
60 |
61 | private Paint mBgTrianglePaint;
62 | private int mBgTriangleColor;
63 |
64 | private float mTopPadding;
65 | private float mBottomPadding;
66 | private float mCenterPadding;
67 | private float mTopDistance;
68 |
69 | private float mRouteDegrees;
70 |
71 | private String mTextTitle;
72 | private String mTextContent;
73 |
74 | private int mBgTriangleWidth;
75 | private int mBgTriangleHeight;
76 |
77 | public LabelViewHelper(Context context, AttributeSet attrs) {
78 | TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.LabelView);
79 | mTopPadding = typedArray.getDimension(R.styleable.LabelView_labelTopPadding,
80 | context.getResources().getDimensionPixelSize(R.dimen.default_label_top_padding));
81 | mCenterPadding = typedArray.getDimension(R.styleable.LabelView_labelCenterPadding, 0);
82 | mBottomPadding = typedArray.getDimension(R.styleable.LabelView_labelBottomPadding,
83 | context.getResources().getDimensionPixelSize(R.dimen.default_label_bottom_padding));
84 | mTopDistance = typedArray.getDimension(R.styleable.LabelView_labelTopDistance, 0);
85 | mBgTriangleColor = typedArray.getColor(R.styleable.LabelView_backgroundColor, Color.BLUE);
86 | mTextTitleColor = typedArray.getColor(R.styleable.LabelView_textTitleColor, Color.WHITE);
87 | mTextContentColor = typedArray.getColor(R.styleable.LabelView_textContentColor, Color.WHITE);
88 | mTextTitleSize = typedArray.getDimension(R.styleable.LabelView_textTitleSize,
89 | context.getResources().getDimensionPixelSize(R.dimen.default_label_title_size));
90 | mTextContentSize = typedArray.getDimension(R.styleable.LabelView_textContentSize,
91 | context.getResources().getDimensionPixelSize(R.dimen.default_label_content_size));
92 | mTextTitle = typedArray.getString(R.styleable.LabelView_textTitle);
93 | mTextContent = typedArray.getString(R.styleable.LabelView_textContent);
94 | mTextTitleStyle = typedArray.getInt(R.styleable.LabelView_textTitleStyle, STYLE_NORMAL);
95 | mTextContentStyle = typedArray.getInt(R.styleable.LabelView_textContentStyle, STYLE_NORMAL);
96 | mRouteDegrees = typedArray.getInt(R.styleable.LabelView_direction, ROTATE_LEFT);
97 | typedArray.recycle();
98 |
99 | initAllArt();
100 | resetAllMeasureSize();
101 | }
102 |
103 | public void drawLabel(View view, Canvas canvas) {
104 | if (canvas == null || view == null) {
105 | throw new IllegalArgumentException("LabelViewHelper draw canvas or view cant't be null!");
106 | }
107 |
108 | canvas.save();
109 | if (mRouteDegrees == ROTATE_LEFT){
110 | canvas.translate(-mBgTriangleWidth / 2, 0);
111 | canvas.rotate(mRouteDegrees, mBgTriangleWidth / 2, 0);
112 | }else if (mRouteDegrees == ROTATE_RIGHT){
113 | int rotateViewWH= (int) (mBgTriangleHeight * Math.sqrt(2));
114 | canvas.translate(view.getMeasuredWidth() - rotateViewWH, -mBgTriangleHeight);
115 | canvas.rotate(mRouteDegrees, 0, mBgTriangleHeight);
116 | }
117 |
118 | Path path = new Path();
119 | path.moveTo(0, mBgTriangleHeight);
120 | if (mTopDistance < 0) {
121 | // mTopDistance > 0 represents a trapezoid, otherwise represents a triangle.
122 | mTopDistance = 0;
123 | }
124 | path.lineTo(mBgTriangleWidth / 2 - mTopDistance, mTopDistance);
125 | path.lineTo(mBgTriangleWidth / 2 + mTopDistance, mTopDistance);
126 | path.lineTo(mBgTriangleWidth, mBgTriangleHeight);
127 | path.close();
128 | canvas.drawPath(path, mBgTrianglePaint);
129 |
130 | if (!TextUtils.isEmpty(mTextTitle)) {
131 | canvas.drawText(mTextTitle, (mBgTriangleWidth) / 2, mTopDistance + mTopPadding + mTextTitleRect.height(), mTextTitlePaint);
132 | }
133 | if (!TextUtils.isEmpty(mTextContent)) {
134 | canvas.drawText(mTextContent, (mBgTriangleWidth) / 2, (mTopDistance + mTopPadding + mTextTitleRect.height() + mCenterPadding + mTextContentRect.height()), mTextContentPaint);
135 | }
136 |
137 | canvas.restore();
138 | }
139 |
140 | public int getBgTriangleWidth() {
141 | return mBgTriangleWidth;
142 | }
143 |
144 | public int getBgTriangleHeight() {
145 | return mBgTriangleHeight;
146 | }
147 |
148 | public void setTextContent(String content) {
149 | mTextContent = content;
150 | resetAllMeasureSize();
151 | }
152 |
153 | public String getTextContent() {
154 | return mTextContent;
155 | }
156 |
157 | public void setTextTitle(String title) {
158 | mTextTitle =title;
159 | resetAllMeasureSize();
160 | }
161 |
162 | public String getTextTitle() {
163 | return mTextTitle;
164 | }
165 |
166 | public void setLabelBackGroundColor(int color) {
167 | mBgTrianglePaint.setColor(color);
168 | }
169 |
170 | private void initAllArt() {
171 | mTextTitleRect = new Rect();
172 | mTextContentRect = new Rect();
173 |
174 | mTextTitlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
175 | mTextTitlePaint.setColor(mTextTitleColor);
176 | mTextTitlePaint.setTextAlign(Paint.Align.CENTER);
177 | mTextTitlePaint.setTextSize(mTextTitleSize);
178 | if (mTextTitleStyle == STYLE_ITALIC) {
179 | mTextTitlePaint.setTypeface(Typeface.SANS_SERIF);
180 | } else if (mTextTitleStyle == STYLE_BOLD) {
181 | mTextTitlePaint.setTypeface(Typeface.DEFAULT_BOLD);
182 | }
183 |
184 | mTextContentPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
185 | mTextContentPaint.setColor(mTextContentColor);
186 | mTextContentPaint.setTextAlign(Paint.Align.CENTER);
187 | mTextContentPaint.setTextSize(mTextContentSize);
188 | if (mTextContentStyle == STYLE_ITALIC) {
189 | mTextContentPaint.setTypeface(Typeface.SANS_SERIF);
190 | } else if (mTextContentStyle == STYLE_BOLD) {
191 | mTextContentPaint.setTypeface(Typeface.DEFAULT_BOLD);
192 | }
193 |
194 | mBgTrianglePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
195 | mBgTrianglePaint.setColor(mBgTriangleColor);
196 | }
197 |
198 | private void resetAllMeasureSize() {
199 | if (!TextUtils.isEmpty(mTextTitle)) {
200 | mTextTitlePaint.getTextBounds(mTextTitle, 0, mTextTitle.length(), mTextTitleRect);
201 | }
202 |
203 | if (!TextUtils.isEmpty(mTextContent)) {
204 | mTextContentPaint.getTextBounds(mTextContent, 0, mTextContent.length(), mTextContentRect);
205 | }
206 |
207 | mBgTriangleHeight = (int) (mTopDistance + mTopPadding + mCenterPadding + mBottomPadding + mTextTitleRect.height() + mTextContentRect.height());
208 | mBgTriangleWidth = 2 * mBgTriangleHeight;
209 | }
210 | }
211 |
--------------------------------------------------------------------------------