11 |
12 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2017 yanbo
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # DreamDrawable
2 |
3 | 一个项目玩转 Android 自定义 Drawable,具体原理参见[《Android 应用层开发 Drawable 的一些叨叨絮》](http://blog.csdn.net/yanbober/article/details/56844869),效果如下图所示。
4 |
5 |
6 |
7 | # 说明文档
8 |
9 | 当前实现了多个 Customer Drawable,依据自己情况使用其一即可,互相无任何依赖。
10 |
11 | | 类型 | 定义 | 说明 |
12 | | ----- | ----- | ----- |
13 | | **RoundDrawable** | RoundDrawable | 圆角 Drawable,可实现类似上图中第一行各种效果。 |
14 | |方法| RoundDrawable(Bitmap bitmap) | 构造方法直接传递一个 Bitmap 即可,然后使用即可,譬如ImageView.setImageDrawable(drawable); |
15 | | **ReflectionDrawable** | ReflectionDrawable | 底部具备倒影效果的 Drawable,可以实现类似上图中第二行各种效果等。 |
16 | |方法| ReflectionDrawable(Bitmap bitmap) | 构造方法直接传递一个 Bitmap 即可,然后使用即可,譬如ImageView.setImageDrawable(drawable); |
17 | |方法| setReflectionHeight(@IntRange(from = 0) int height) | 给 ReflectionDrawable 设置底部反转倒影的高度。 |
18 | | **LauncherIconDrawable** | LauncherIconDrawable | 具备进度刷新效果的 Drawable,譬如 miui 桌面 app 更新图标进度,类似上图第三行等效果,还可配置。譬如ImageView.setImageDrawable(drawable); |
19 | |方法|LauncherIconDrawable(Drawable drawable)|构造方法传递一个 Drawable,譬如传递 ImageView.getDrawable(); |
20 | |方法|setDefaultColor(@ColorInt int defaultColor)|设置 LauncherIconDrawable 中图片默认的覆盖颜色,模式为 PorterDuff.Mode.MULTIPLY。|
21 | |方法|setDefaultColor(@ColorInt int defaultColor, PorterDuff.Mode mode)|同上,设置 LauncherIconDrawable 中图片默认的覆盖颜色。|
22 | |方法|setPercentColor(@ColorInt int percentColor)|设置 LauncherIconDrawable 中进度的覆盖颜色,模式为 PorterDuff.Mode.MULTIPLY。|
23 | |方法|setPercentColor(@ColorInt int percentColor, PorterDuff.Mode mode)|同上,设置 LauncherIconDrawable 中进度的覆盖颜色。|
24 | |方法|setCurPercent(@FloatRange(from = 0f, to = 1f) float curPercent)|设置当前进度为多少,类似 miui App 更新桌面图标进度更新。|
25 | |**IconView**|IconView|类似 Flyme 6.0 联系人 Icon 的 Drawable 的 View,可以实现类似上图中最后一行的各种效果,配合下面的 IconDrawable 使用。|
26 | |方法|IconDrawable getIconDrawable()|获取 IconView 中的 IconDrawable 来使用。|
27 | |**IconDrawable**|IconDrawable|配合上面 IconView 内部使用的,不用自己 new,只用通过 IconView 的 IconDrawable getIconDrawable() 获取来操作。|
28 | |方法|setTextLabel(String str)|设置文字,譬如联系人姓或者首字母等。|
29 | |方法|setTextFontSize(int size)|设置文字大小,不设置默认会自适应。|
30 | |方法|setTextColor(int color)|设置文字颜色。|
31 | |方法|setBackgroundColor(int color)|设置为文字时的背景颜色。|
32 | |方法|setIconLabel(Bitmap bitmap)|设置为联系人默认或者真是头像。|
33 |
34 | # License 声明
35 |
36 | MIT License
37 |
38 | Copyright (c) 2017 yanbo
39 |
40 | Permission is hereby granted, free of charge, to any person obtaining a copy
41 | of this software and associated documentation files (the "Software"), to deal
42 | in the Software without restriction, including without limitation the rights
43 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
44 | copies of the Software, and to permit persons to whom the Software is
45 | furnished to do so, subject to the following conditions:
46 |
47 | The above copyright notice and this permission notice shall be included in all
48 | copies or substantial portions of the Software.
49 |
50 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
51 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
52 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
53 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
54 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
55 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
56 | SOFTWARE.
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 25
5 | buildToolsVersion "25.0.0"
6 | defaultConfig {
7 | applicationId "cn.yan.dreamdrawable"
8 | minSdkVersion 14
9 | targetSdkVersion 25
10 | versionCode 1
11 | versionName "1.0"
12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 | }
21 |
22 | dependencies {
23 | compile fileTree(dir: 'libs', include: ['*.jar'])
24 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
25 | exclude group: 'com.android.support', module: 'support-annotations'
26 | })
27 | compile 'com.android.support:appcompat-v7:25.1.0'
28 | testCompile 'junit:junit:4.12'
29 | compile project(':library')
30 | }
31 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /usr/yan/android-sdk-linux/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/cn/yan/dreamdrawable/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package cn.yan.dreamdrawable;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumentation test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() throws Exception {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("cn.yan.dreamdrawable", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/app/src/main/java/cn/yan/dreamdrawable/MainActivity.java:
--------------------------------------------------------------------------------
1 | package cn.yan.dreamdrawable;
2 |
3 | import android.graphics.BitmapFactory;
4 | import android.graphics.Color;
5 | import android.os.Bundle;
6 | import android.support.v7.app.AppCompatActivity;
7 | import android.view.View;
8 | import android.widget.ImageView;
9 |
10 | import cn.yan.library.IconDrawable;
11 | import cn.yan.library.IconView;
12 | import cn.yan.library.LauncherIconDrawable;
13 | import cn.yan.library.ReflectionDrawable;
14 | import cn.yan.library.RoundDrawable;
15 |
16 | public class MainActivity extends AppCompatActivity {
17 |
18 | @Override
19 | protected void onCreate(Bundle savedInstanceState) {
20 | super.onCreate(savedInstanceState);
21 | setContentView(R.layout.activity_main);
22 | demoDataHandle();
23 | }
24 |
25 | private void demoDataHandle() {
26 | //TODO Ignore the code style, write to this style that just to test customer drawable.
27 | handleRoundDrawable();
28 | handleReflectionDrawable();
29 | handleLauncherIconDrawable();
30 | handleIconDrawable();
31 | }
32 |
33 | private void handleRoundDrawable() {
34 | ImageView imageView1 = (ImageView) this.findViewById(R.id.imageview1);
35 | imageView1.setImageDrawable(new RoundDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.timg)).setCornerRadius(50));
36 |
37 | ImageView imageView2 = (ImageView) this.findViewById(R.id.imageview2);
38 | imageView2.setImageDrawable(new RoundDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.timg)).setCornerRadius(300));
39 |
40 | ImageView imageView3 = (ImageView) this.findViewById(R.id.imageview3);
41 | imageView3.setImageDrawable(new RoundDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.timg)).setCornerRadius(250));
42 | }
43 |
44 | private void handleReflectionDrawable() {
45 | ImageView imageView4 = (ImageView) this.findViewById(R.id.imageview4);
46 | imageView4.setImageDrawable(new ReflectionDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.timg)).setReflectionHeight(80));
47 |
48 | ImageView imageView5 = (ImageView) this.findViewById(R.id.imageview5);
49 | imageView5.setImageDrawable(new ReflectionDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.timg)).setReflectionHeight(120));
50 | }
51 |
52 | private void handleLauncherIconDrawable() {
53 | ImageView imageView6 = (ImageView) this.findViewById(R.id.imageview6);
54 | final LauncherIconDrawable drawable = new LauncherIconDrawable(imageView6.getDrawable().mutate());
55 | imageView6.setImageDrawable(drawable);
56 | drawable.setPercentColor(Color.DKGRAY);
57 | imageView6.setOnClickListener(new View.OnClickListener() {
58 | float percent = 0;
59 | @Override
60 | public void onClick(View view) {
61 | drawable.setCurPercent(percent*1.0f/100);
62 | percent += 5;
63 | if (percent > 100) {
64 | percent = 0;
65 | }
66 | }
67 | });
68 |
69 | ImageView imageView7 = (ImageView) this.findViewById(R.id.imageview7);
70 | final LauncherIconDrawable drawable1 = new LauncherIconDrawable(imageView7.getDrawable().mutate());
71 | imageView7.setImageDrawable(drawable1);
72 | drawable1.setDefaultColor(Color.DKGRAY);
73 | drawable1.setPercentColor(Color.WHITE);
74 | imageView7.setOnClickListener(new View.OnClickListener() {
75 | float percent = 0;
76 | @Override
77 | public void onClick(View view) {
78 | drawable1.setCurPercent(percent*1.0f/100);
79 | percent += 5;
80 | if (percent > 100) {
81 | percent = 0;
82 | }
83 | }
84 | });
85 | }
86 |
87 | private void handleIconDrawable() {
88 | IconView imageView8 = (IconView) this.findViewById(R.id.imageview8);
89 | IconDrawable drawable = imageView8.getIconDrawable();
90 | drawable.setBackgroundColor(Color.GREEN);
91 | drawable.setTextLabel("M");
92 |
93 | IconView imageView9 = (IconView) this.findViewById(R.id.imageview9);
94 | IconDrawable drawable1 = imageView9.getIconDrawable();
95 | drawable1.setBackgroundColor(Color.YELLOW);
96 | drawable1.setTextColor(Color.DKGRAY);
97 | drawable1.setTextLabel("王");
98 |
99 | IconView imageView10 = (IconView) this.findViewById(R.id.imageview10);
100 | IconDrawable drawable2 = imageView10.getIconDrawable();
101 | drawable2.setIconLabel(BitmapFactory.decodeResource(getResources(), R.drawable.timg));
102 |
103 | IconView imageView11 = (IconView) this.findViewById(R.id.imageview11);
104 | IconDrawable drawable3 = imageView11.getIconDrawable();
105 | drawable3.setIconLabel(BitmapFactory.decodeResource(getResources(), R.drawable.timg1));
106 | }
107 | }
108 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/timg.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yanbober/DreamDrawable/2af605e31cc9b172ee19b3cbd27786f9407d26e1/app/src/main/res/drawable/timg.jpg
--------------------------------------------------------------------------------
/app/src/main/res/drawable/timg1.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yanbober/DreamDrawable/2af605e31cc9b172ee19b3cbd27786f9407d26e1/app/src/main/res/drawable/timg1.jpg
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
14 |
15 |
21 |
22 |
28 |
29 |
35 |
36 |
44 |
45 |
52 |
53 |
62 |
63 |
69 |
70 |
78 |
79 |
87 |
88 |
96 |
97 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yanbober/DreamDrawable/2af605e31cc9b172ee19b3cbd27786f9407d26e1/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yanbober/DreamDrawable/2af605e31cc9b172ee19b3cbd27786f9407d26e1/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yanbober/DreamDrawable/2af605e31cc9b172ee19b3cbd27786f9407d26e1/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yanbober/DreamDrawable/2af605e31cc9b172ee19b3cbd27786f9407d26e1/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yanbober/DreamDrawable/2af605e31cc9b172ee19b3cbd27786f9407d26e1/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 64dp
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 16dp
4 | 16dp
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | DreamDrawable
3 |
4 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/test/java/cn/yan/dreamdrawable/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package cn.yan.dreamdrawable;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() throws Exception {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 |
3 | buildscript {
4 | repositories {
5 | jcenter()
6 | }
7 | dependencies {
8 | classpath 'com.android.tools.build:gradle:2.2.0'
9 |
10 | // NOTE: Do not place your application dependencies here; they belong
11 | // in the individual module build.gradle files
12 | }
13 | }
14 |
15 | allprojects {
16 | repositories {
17 | jcenter()
18 | }
19 | }
20 |
21 | task clean(type: Delete) {
22 | delete rootProject.buildDir
23 | }
24 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | org.gradle.jvmargs=-Xmx1536m
13 |
14 | # When configured, Gradle will run in incubating parallel mode.
15 | # This option should only be used with decoupled projects. More details, visit
16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
17 | # org.gradle.parallel=true
18 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yanbober/DreamDrawable/2af605e31cc9b172ee19b3cbd27786f9407d26e1/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Mon Dec 28 10:00:20 PST 2015
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
7 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/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/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/library/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 |
3 | android {
4 | compileSdkVersion 25
5 | buildToolsVersion "25.0.0"
6 |
7 | defaultConfig {
8 | minSdkVersion 14
9 | targetSdkVersion 25
10 | versionCode 1
11 | minSdkVersion 14
12 | versionName "1.0"
13 |
14 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
15 |
16 | }
17 | buildTypes {
18 | release {
19 | minifyEnabled false
20 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
21 | }
22 | }
23 | }
24 |
25 | dependencies {
26 | compile fileTree(dir: 'libs', include: ['*.jar'])
27 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
28 | exclude group: 'com.android.support', module: 'support-annotations'
29 | })
30 | compile 'com.android.support:appcompat-v7:25.1.0'
31 | testCompile 'junit:junit:4.12'
32 | }
33 |
--------------------------------------------------------------------------------
/library/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /usr/yan/android-sdk-linux/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/library/src/androidTest/java/cn/yan/library/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package cn.yan.library;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumentation test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() throws Exception {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("cn.yan.library.test", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/library/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/library/src/main/java/cn/yan/library/IconDrawable.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 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.yan.library;
25 |
26 | import android.graphics.Bitmap;
27 | import android.graphics.Canvas;
28 | import android.graphics.Color;
29 | import android.graphics.ColorFilter;
30 | import android.graphics.Matrix;
31 | import android.graphics.Paint;
32 | import android.graphics.Path;
33 | import android.graphics.PixelFormat;
34 | import android.graphics.Rect;
35 | import android.graphics.RectF;
36 | import android.graphics.drawable.Drawable;
37 | import android.text.TextUtils;
38 |
39 | /**
40 | * 类似 Flyme 6.0 联系人 Icon 的 Drawable。
41 | * 当设置图片后显示联系人图片,当设置一个字母或者一个汉字时显示字符图。
42 | */
43 |
44 | public class IconDrawable extends Drawable {
45 | private Paint mTextPaint;
46 | private String mContentText;
47 | private int mFontSize;
48 | private int mBgColor = Color.GREEN;
49 | private int mTextColor = Color.WHITE;
50 |
51 | private Bitmap mContentBitmap;
52 |
53 | private Path mClipPath;
54 | private Matrix mMatrix;
55 |
56 | public IconDrawable() {
57 | mTextPaint = new Paint();
58 | mTextPaint.setAntiAlias(true);
59 | mTextPaint.setColor(Color.WHITE);
60 | mTextPaint.setStyle(Paint.Style.FILL);
61 | mTextPaint.setTextAlign(Paint.Align.CENTER);
62 |
63 | mClipPath = new Path();
64 | mMatrix = new Matrix();
65 | }
66 |
67 | public IconDrawable setTextLabel(String str) {
68 | mContentText = str;
69 | mContentBitmap = null;
70 | invalidateSelf();
71 | return this;
72 | }
73 |
74 | public IconDrawable setIconLabel(Bitmap bitmap) {
75 | mContentBitmap = bitmap;
76 | mContentText = null;
77 | invalidateSelf();
78 | return this;
79 | }
80 |
81 | public IconDrawable setTextFontSize(int size) {
82 | mFontSize = size;
83 | invalidateSelf();
84 | return this;
85 | }
86 |
87 | public IconDrawable setTextColor(int color) {
88 | mTextColor = color;
89 | invalidateSelf();
90 | return this;
91 | }
92 |
93 | public IconDrawable setBackgroundColor(int color) {
94 | mBgColor = color;
95 | invalidateSelf();
96 | return this;
97 | }
98 |
99 | @Override
100 | public void draw(Canvas canvas) {
101 | Rect rect = getBounds();
102 |
103 | int count = canvas.saveLayer(new RectF(rect), null, Canvas.ALL_SAVE_FLAG);
104 | canvas.translate(rect.left, rect.top);
105 | mClipPath.reset();
106 | mClipPath.addCircle(rect.width() / 2, rect.height() / 2,
107 | Math.min(rect.width(), rect.height()) / 2, Path.Direction.CCW);
108 | canvas.clipPath(mClipPath);
109 |
110 | if (mContentBitmap == null && !TextUtils.isEmpty(mContentText)) {
111 | mTextPaint.setColor(mBgColor);
112 | canvas.drawRect(rect, mTextPaint);
113 | int fontSize = this.mFontSize <= 0 ? (Math.min(rect.width(), rect.height()) / 2) : this.mFontSize;
114 | mTextPaint.setTextSize(fontSize);
115 | mTextPaint.setColor(mTextColor);
116 | canvas.drawText(mContentText, rect.width() / 2, rect.height() / 2 - ((mTextPaint.descent() + mTextPaint.ascent()) / 2), mTextPaint);
117 | } else {
118 | mMatrix.setScale(rect.width() * 1.0f / mContentBitmap.getWidth(),
119 | rect.height() * 1.0f / mContentBitmap.getHeight());
120 | Bitmap scaleContentBitmap = Bitmap.createBitmap(mContentBitmap, 0, 0,
121 | mContentBitmap.getWidth(), mContentBitmap.getHeight(),
122 | mMatrix, true);
123 | canvas.drawBitmap(scaleContentBitmap, rect, rect, null);
124 | }
125 | canvas.restoreToCount(count);
126 | }
127 |
128 | @Override
129 | public void setAlpha(int alpha) {
130 | mTextPaint.setAlpha(alpha);
131 | }
132 |
133 | @Override
134 | public void setColorFilter(ColorFilter cf) {
135 | mTextPaint.setColorFilter(cf);
136 | }
137 |
138 | @Override
139 | public int getOpacity() {
140 | return PixelFormat.TRANSLUCENT;
141 | }
142 |
143 | @Override
144 | public int getIntrinsicWidth() {
145 | Rect rect = getContentRect();
146 | return (rect.width() > 0 ? rect.width() : -1);
147 | }
148 |
149 | @Override
150 | public int getIntrinsicHeight() {
151 | Rect rect = getContentRect();
152 | return (rect.height() > 0 ? rect.height() : -1);
153 | }
154 |
155 | private Rect getContentRect() {
156 | Rect rect = new Rect();
157 | if (!TextUtils.isEmpty(mContentText)) {
158 | mTextPaint.getTextBounds(mContentText, 0, mContentText.length(), rect);
159 | } else {
160 | rect.set(0, 0, mContentBitmap.getWidth(), mContentBitmap.getHeight());
161 | }
162 | return rect;
163 | }
164 | }
165 |
--------------------------------------------------------------------------------
/library/src/main/java/cn/yan/library/IconView.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 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.yan.library;
25 |
26 | import android.content.Context;
27 | import android.graphics.Canvas;
28 | import android.graphics.Rect;
29 | import android.util.AttributeSet;
30 | import android.view.View;
31 |
32 | /**
33 | * 类似 Flyme 6.0 联系人 Icon 的 Drawable 的 View。
34 | * 当设置图片后显示联系人图片,当设置一个字母或者一个汉字时显示字符图。
35 | * TODO 该 View 仅仅为了表明自定义 Drawable 结合 View 的一种用法。
36 | */
37 |
38 | public class IconView extends View {
39 | private IconDrawable mDrawable;
40 |
41 | public IconView(Context context) {
42 | this(context, null);
43 | }
44 |
45 | public IconView(Context context, AttributeSet attrs) {
46 | super(context, attrs);
47 | init();
48 | }
49 |
50 | public IconDrawable getIconDrawable() {
51 | return mDrawable;
52 | }
53 |
54 | private void init() {
55 | setLayerType(LAYER_TYPE_SOFTWARE, null);
56 | mDrawable = new IconDrawable();
57 | }
58 |
59 | @Override
60 | protected void onSizeChanged(int w, int h, int oldw, int oldh) {
61 | super.onSizeChanged(w, h, oldw, oldh);
62 | mDrawable.setBounds(new Rect(0, 0, w, h));
63 | }
64 |
65 | @Override
66 | protected void onDraw(Canvas canvas) {
67 | super.onDraw(canvas);
68 | mDrawable.draw(canvas);
69 | }
70 | }
71 |
--------------------------------------------------------------------------------
/library/src/main/java/cn/yan/library/LauncherIconDrawable.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 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.yan.library;
25 |
26 | import android.graphics.Canvas;
27 | import android.graphics.ColorFilter;
28 | import android.graphics.PixelFormat;
29 | import android.graphics.PorterDuff;
30 | import android.graphics.PorterDuffColorFilter;
31 | import android.graphics.Rect;
32 | import android.graphics.drawable.Drawable;
33 | import android.support.annotation.ColorInt;
34 | import android.support.annotation.FloatRange;
35 |
36 | /**
37 | * 类似许多 Android Launcher 桌面图标更新的 Drawable。
38 | * 譬如 miui 桌面 app 更新图标进度。
39 | */
40 | public class LauncherIconDrawable extends Drawable {
41 | private Drawable mDrawable;
42 | private float mPercent;
43 |
44 | private ColorFilter mDefaultColorFilter;
45 | private ColorFilter mPercentColorFilter;
46 |
47 | public LauncherIconDrawable(Drawable drawable) {
48 | mDrawable = drawable;
49 | }
50 |
51 | public LauncherIconDrawable setDefaultColor(@ColorInt int defaultColor) {
52 | return setDefaultColor(defaultColor, PorterDuff.Mode.MULTIPLY);
53 | }
54 |
55 | public LauncherIconDrawable setDefaultColor(@ColorInt int defaultColor, PorterDuff.Mode mode) {
56 | mDefaultColorFilter = new PorterDuffColorFilter(defaultColor, mode);
57 | invalidateSelf();
58 | return this;
59 | }
60 |
61 | public LauncherIconDrawable setPercentColor(@ColorInt int percentColor) {
62 | return setPercentColor(percentColor, PorterDuff.Mode.MULTIPLY);
63 | }
64 |
65 | public LauncherIconDrawable setPercentColor(@ColorInt int percentColor, PorterDuff.Mode mode) {
66 | mPercentColorFilter = new PorterDuffColorFilter(percentColor, mode);
67 | invalidateSelf();
68 | return this;
69 | }
70 |
71 | public void setCurPercent(@FloatRange(from = 0f, to = 1f) float curPercent) {
72 | mPercent = curPercent;
73 | invalidateSelf();
74 | }
75 |
76 | @Override
77 | public void draw(Canvas canvas) {
78 | if (mDrawable == null) return;
79 |
80 | Rect rect = getBounds();
81 | int curOffset = (int) (mPercent * rect.height());
82 | canvas.save();
83 | canvas.clipRect(rect.left, rect.top, rect.width(), rect.height() - curOffset);
84 | mDrawable.setColorFilter(mDefaultColorFilter);
85 | mDrawable.draw(canvas);
86 | canvas.restore();
87 |
88 | canvas.save();
89 | canvas.clipRect(rect.left, rect.height() - curOffset, rect.width(), rect.height());
90 | mDrawable.setColorFilter(mPercentColorFilter);
91 | mDrawable.draw(canvas);
92 | canvas.restore();
93 | }
94 |
95 | @Override
96 | protected void onBoundsChange(Rect bounds) {
97 | super.onBoundsChange(bounds);
98 | if (mDrawable != null) {
99 | mDrawable.setBounds(getBounds());
100 | }
101 | }
102 |
103 | @Override
104 | public void invalidateSelf() {
105 | super.invalidateSelf();
106 | if (mDrawable != null) {
107 | mDrawable.invalidateSelf();
108 | }
109 | }
110 |
111 | @Override
112 | public void setAlpha(int alpha) {
113 | if (mDrawable != null) {
114 | mDrawable.setAlpha(alpha);
115 | }
116 | }
117 |
118 | @Override
119 | public void setColorFilter(ColorFilter cf) {
120 | if (mDrawable != null) {
121 | mDrawable.setColorFilter(cf);
122 | }
123 | }
124 |
125 | @Override
126 | public int getIntrinsicWidth() {
127 | if (mDrawable != null) {
128 | return mDrawable.getIntrinsicWidth();
129 | }
130 | return -1;
131 | }
132 |
133 | @Override
134 | public int getIntrinsicHeight() {
135 | if (mDrawable != null) {
136 | return mDrawable.getIntrinsicHeight();
137 | }
138 | return -1;
139 | }
140 |
141 | @Override
142 | public int getOpacity() {
143 | if (mDrawable != null) {
144 | return mDrawable.getOpacity();
145 | }
146 | return PixelFormat.TRANSLUCENT;
147 | }
148 | }
149 |
--------------------------------------------------------------------------------
/library/src/main/java/cn/yan/library/ReflectionDrawable.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 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.yan.library;
25 |
26 | import android.graphics.Bitmap;
27 | import android.graphics.Canvas;
28 | import android.graphics.Color;
29 | import android.graphics.ColorFilter;
30 | import android.graphics.LinearGradient;
31 | import android.graphics.Matrix;
32 | import android.graphics.Paint;
33 | import android.graphics.PixelFormat;
34 | import android.graphics.Rect;
35 | import android.graphics.Shader;
36 | import android.graphics.drawable.Drawable;
37 | import android.support.annotation.FloatRange;
38 | import android.support.annotation.IntRange;
39 |
40 | /**
41 | * 带底部反射倒影的Drawable。
42 | * 注意:content自动缩放适配倒影height以外的高度。
43 | * 通过setReflectionHeight(int height)方法设置底部倒影的高度。
44 | */
45 |
46 | public class ReflectionDrawable extends Drawable {
47 | private Bitmap mSrcBitmap;
48 | private Bitmap mReflectBitmap;
49 |
50 | private Paint mPaint;
51 | private int mReflectionHeight;
52 |
53 | public ReflectionDrawable(Bitmap bitmap) {
54 | mSrcBitmap = bitmap;
55 |
56 | mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
57 |
58 | Matrix matrix = new Matrix();
59 | matrix.preScale(1.0f, -1.0f);
60 | mReflectBitmap = Bitmap.createBitmap(mSrcBitmap, 0, 0,
61 | mSrcBitmap.getWidth(), mSrcBitmap.getHeight(),
62 | matrix, true);
63 | }
64 |
65 | public ReflectionDrawable setReflectionHeight(@IntRange(from = 0) int height) {
66 | mReflectionHeight = height;
67 | invalidateSelf();
68 | return this;
69 | }
70 |
71 | @Override
72 | public void draw(Canvas canvas) {
73 | Rect rect = getBounds();
74 | Rect rectSrc = new Rect(rect.left, rect.top, rect.right, rect.bottom - mReflectionHeight);
75 | Rect rectReflect = new Rect(rect.left, rect.bottom - mReflectionHeight, rect.right, rect.bottom);
76 |
77 | canvas.drawBitmap(mSrcBitmap, new Rect(0, 0, mSrcBitmap.getWidth(), mSrcBitmap.getHeight()), rectSrc, null);
78 |
79 | canvas.drawBitmap(mReflectBitmap, new Rect(0, 0, mReflectBitmap.getWidth(), mReflectionHeight), rectReflect, null);
80 | mPaint.setShader(new LinearGradient(rectReflect.left, rectReflect.top,
81 | rectReflect.left, rectReflect.bottom,
82 | Color.TRANSPARENT, Color.BLACK,
83 | Shader.TileMode.CLAMP));
84 | canvas.drawRect(rectReflect, mPaint);
85 | }
86 |
87 | @Override
88 | public void setAlpha(int alpha) {
89 | mPaint.setAlpha(alpha);
90 | }
91 |
92 | @Override
93 | public void setColorFilter(ColorFilter colorFilter) {
94 | mPaint.setColorFilter(colorFilter);
95 | }
96 |
97 | @Override
98 | public int getOpacity() {
99 | return PixelFormat.TRANSLUCENT;
100 | }
101 | }
102 |
--------------------------------------------------------------------------------
/library/src/main/java/cn/yan/library/RoundDrawable.java:
--------------------------------------------------------------------------------
1 | /**
2 | * MIT License
3 | *
4 | * Copyright (c) 2017 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.yan.library;
25 |
26 | import android.graphics.Bitmap;
27 | import android.graphics.BitmapShader;
28 | import android.graphics.Canvas;
29 | import android.graphics.ColorFilter;
30 | import android.graphics.Matrix;
31 | import android.graphics.Paint;
32 | import android.graphics.PixelFormat;
33 | import android.graphics.Rect;
34 | import android.graphics.RectF;
35 | import android.graphics.Shader;
36 | import android.graphics.drawable.Drawable;
37 | import android.support.annotation.FloatRange;
38 |
39 | /**
40 | * 圆角 Drawable,通过 setCornerRadius(int radius) 方法设置四个圆角半径。
41 | * 若想成为圆形 Drawable,只用将 radius 值设置为控件宽度的一半即可。
42 | */
43 | public class RoundDrawable extends Drawable {
44 | private Paint mPaint;
45 | private Bitmap mBitmap;
46 | private BitmapShader mShader;
47 | private Matrix mMatrix;
48 | private float mCornerRadius;
49 |
50 | public RoundDrawable(Bitmap bitmap) {
51 | mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
52 | mBitmap = bitmap;
53 |
54 | mShader = new BitmapShader(mBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
55 | mPaint.setShader(mShader);
56 |
57 | mMatrix = new Matrix();
58 | }
59 |
60 | public RoundDrawable setCornerRadius(@FloatRange(from = 0f) float radius) {
61 | mCornerRadius = radius;
62 | invalidateSelf();
63 | return this;
64 | }
65 |
66 | @Override
67 | public void draw(Canvas canvas) {
68 | Rect rect = getBounds();
69 | mMatrix.preTranslate(rect.left, rect.top);
70 | mMatrix.preScale(rect.width()*1.0f/mBitmap.getWidth(),
71 | rect.height()*1.0f/mBitmap.getHeight());
72 | mShader.setLocalMatrix(mMatrix);
73 | canvas.drawRoundRect(new RectF(rect), mCornerRadius, mCornerRadius, mPaint);
74 | }
75 |
76 | @Override
77 | public int getIntrinsicHeight() {
78 | return mBitmap.getHeight();
79 | }
80 |
81 | @Override
82 | public int getIntrinsicWidth() {
83 | return mBitmap.getWidth();
84 | }
85 |
86 | @Override
87 | public void setAlpha(int i) {
88 | mPaint.setAlpha(i);
89 | }
90 |
91 | @Override
92 | public void setColorFilter(ColorFilter colorFilter) {
93 | mPaint.setColorFilter(colorFilter);
94 | }
95 |
96 | @Override
97 | public int getOpacity() {
98 | return PixelFormat.TRANSLUCENT;
99 | }
100 | }
101 |
--------------------------------------------------------------------------------
/library/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | library
3 |
4 |
--------------------------------------------------------------------------------
/library/src/test/java/cn/yan/library/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package cn.yan.library;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() throws Exception {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app', ':library'
2 |
--------------------------------------------------------------------------------