├── .gitignore ├── LICENSE ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── xw │ │ └── sample │ │ └── vectorcompattextview │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── xw │ │ │ └── sample │ │ │ └── vectorcompattextview │ │ │ └── MainActivity.java │ └── res │ │ ├── drawable │ │ ├── selector_drawable_day_night_mode.xml │ │ ├── selector_drawable_tab.xml │ │ ├── selector_text_color_day_night_mode.xml │ │ ├── selector_text_color_tab.xml │ │ ├── shape_rect_solid_gray.xml │ │ ├── shape_rect_solid_red.xml │ │ ├── shape_stroke_bg_gray.xml │ │ ├── svg_ic_arrow_right.xml │ │ ├── svg_ic_boy.xml │ │ ├── svg_ic_day_mode.xml │ │ ├── svg_ic_fx.xml │ │ ├── svg_ic_girl.xml │ │ ├── svg_ic_github.xml │ │ ├── svg_ic_line.xml │ │ ├── svg_ic_night_mode.xml │ │ ├── svg_ic_right.xml │ │ ├── svg_ic_twitter.xml │ │ ├── svg_ic_txl.xml │ │ ├── svg_ic_user.xml │ │ ├── svg_ic_w.xml │ │ ├── svg_ic_wechat.xml │ │ ├── svg_ic_weibo.xml │ │ └── svg_ic_wx.xml │ │ ├── layout │ │ └── activity_main.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── xw │ └── sample │ └── vectorcompattextview │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── screenshot ├── demo5.jpg └── demo6.gif ├── settings.gradle └── vectorcompattextview ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src └── main ├── AndroidManifest.xml ├── java └── com │ └── xw │ └── repo │ └── VectorCompatTextView.java └── res └── values └── attrs.xml /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | .idea 5 | .DS_Store 6 | /build 7 | /captures 8 | .externalNativeBuild 9 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 woxingxiao 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 | [![](https://jitpack.io/v/woxingxiao/VectorCompatTextView.svg)](https://jitpack.io/#woxingxiao/VectorCompatTextView) 2 | [![API](https://img.shields.io/badge/API-9%2B-blue.svg?style=flat)](https://android-arsenal.com/api?level=9) 3 | [![License](http://img.shields.io/badge/license-MIT-green.svg?style=flat)]() 4 | # VectorCompatTextView 5 | 6 | With this multifunctional TextView, the VectorDrawable resources can be set as CompoundDrawables, furthermore, the majority of scenarios to manipulate CompoundDrawables have been supported, such as checked state, color tinting/setting, custom size setting, visibility, RTL, etc. AndroidX version is available. 7 | 8 | 这个炒鸡强大的库可以让你轻松将VectorDrawable资源设置为CompoundDrawable,并且支持大多数操控CompoundDrawable的情景,如:checked状态,颜色设置或着色,自定义宽高,隐藏或显示,RTL布局等等(当然常规资源如png/jpg,Drawable的子类等更不在话下)。有AndroidX版本。 9 | 10 | ## Screenshot 11 | ![demo5](https://github.com/woxingxiao/VectorCompatTextView/blob/master/screenshot/demo5.jpg) ![demo6](https://github.com/woxingxiao/VectorCompatTextView/blob/master/screenshot/demo6.gif) 12 | ## Download 13 | root project:`build.gradle` 14 | ```groovy 15 | allprojects { 16 | repositories { 17 | ... 18 | maven { url "https://jitpack.io" } 19 | } 20 | } 21 | ``` 22 | app:`build.gradle` 23 | ```groovy 24 | android { 25 | ... 26 | defaultConfig { 27 | ... 28 | vectorDrawables.useSupportLibrary = true // essential 29 | } 30 | ... 31 | 32 | dependencies { 33 | // Support 34 | // e.g. implementation 'com.github.woxingxiao:VectorCompatTextView:2.7' 35 | implementation 'com.github.woxingxiao:VectorCompatTextView:${LATEST_VERSION}' 36 | 37 | // AndroidX 38 | // e.g. implementation 'com.github.woxingxiao:VectorCompatTextView:2.7-androidx' 39 | implementation 'com.github.woxingxiao:VectorCompatTextView:${LATEST_VERSION}-androidx' 40 | } 41 | ``` 42 | ## Usage 43 | ```xml 44 | 49 | 50 | 56 | 57 | 63 | 64 | 70 | 71 | 78 | 79 | 84 | app:drawableLeftCompat="@drawable/shape_rect_solid_red" 85 | app:drawableWidth="4dp"/> 86 | 87 | 95 | app:drawableBottomCompat="@drawable/selector_drawable_tab" 96 | app:drawableHeight="2dp"/> 97 | ``` 98 | [**Check the Demo for complete usage.**](https://github.com/woxingxiao/VectorCompatTextView/blob/master/app/src/main/java/com/xw/sample/vectorcompattextview/MainActivity.java) 99 | # LICENSE 100 | [MIT](https://github.com/woxingxiao/VectorCompatTextView/blob/master/LICENSE) 101 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 28 5 | defaultConfig { 6 | applicationId "com.xw.sample.vectorcompattextview" 7 | minSdkVersion 14 8 | targetSdkVersion 28 9 | versionCode 6 10 | versionName "2.4" 11 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 12 | vectorDrawables.useSupportLibrary = true 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | implementation fileTree(include: ['*.jar'], dir: 'libs') 24 | androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', { 25 | exclude group: 'com.android.support', module: 'support-annotations' 26 | }) 27 | implementation 'com.android.support:appcompat-v7:28.0.0' 28 | testImplementation 'junit:junit:4.13.1' 29 | implementation project(':vectorcompattextview') 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 D:\Android\android-sdk-windows/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 | 19 | # Uncomment this to preserve the line number information for 20 | # debugging stack traces. 21 | #-keepattributes SourceFile,LineNumberTable 22 | 23 | # If you keep the line number information, uncomment this to 24 | # hide the original source file name. 25 | #-renamesourcefileattribute SourceFile 26 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/xw/sample/vectorcompattextview/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.xw.sample.vectorcompattextview; 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("com.xw.sample.vectorcompattextview", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /app/src/main/java/com/xw/sample/vectorcompattextview/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.xw.sample.vectorcompattextview; 2 | 3 | import android.os.Bundle; 4 | import android.support.v4.content.ContextCompat; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.support.v7.app.AppCompatDelegate; 7 | import android.view.View; 8 | import android.widget.RadioGroup; 9 | 10 | import com.xw.repo.VectorCompatTextView; 11 | 12 | public class MainActivity extends AppCompatActivity { 13 | 14 | static { 15 | AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); 16 | } 17 | 18 | @Override 19 | protected void onCreate(Bundle savedInstanceState) { 20 | super.onCreate(savedInstanceState); 21 | setContentView(R.layout.activity_main); 22 | 23 | final VectorCompatTextView checkableTv1 = findViewById(R.id.checkable_text_view1); 24 | checkableTv1.setOnClickListener(new View.OnClickListener() { 25 | @Override 26 | public void onClick(View v) { 27 | checkableTv1.toggle(); 28 | checkableTv1.setText(checkableTv1.isChecked() ? "SELECTED TAB" : "UNSELECTED TAB"); 29 | } 30 | }); 31 | final VectorCompatTextView checkableTv2 = findViewById(R.id.checkable_text_view2); 32 | checkableTv2.setOnClickListener(new View.OnClickListener() { 33 | @Override 34 | public void onClick(View v) { 35 | checkableTv2.toggle(); 36 | checkableTv2.setText(checkableTv2.isChecked() ? "Night Mode" : "Day Mode"); 37 | } 38 | }); 39 | 40 | final VectorCompatTextView demoTv = findViewById(R.id.demo_tv); 41 | ((RadioGroup) findViewById(R.id.radio_group)).setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 42 | @Override 43 | public void onCheckedChanged(RadioGroup group, int checkedId) { 44 | String txt = ""; 45 | if (checkedId == R.id.boy_rb) { 46 | demoTv.setHideDrawable(false); 47 | new VectorCompatTextView.CompoundDrawableConfigBuilder(demoTv) 48 | .setDrawableEnd(ContextCompat.getDrawable(MainActivity.this, R.drawable.svg_ic_boy)) 49 | .build(); 50 | txt = "Jack"; 51 | } else if (checkedId == R.id.girl_rb) { 52 | demoTv.setHideDrawable(false); 53 | new VectorCompatTextView.CompoundDrawableConfigBuilder(demoTv) 54 | .setDrawableEnd(R.drawable.svg_ic_girl) 55 | .build(); 56 | txt = "Mary"; 57 | } else if (checkedId == R.id.reset_rb) { 58 | demoTv.setHideDrawable(true); 59 | txt = "Name"; 60 | 61 | } 62 | demoTv.setText(txt); 63 | } 64 | }); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/selector_drawable_day_night_mode.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 9 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/selector_drawable_tab.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 9 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/selector_text_color_day_night_mode.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 9 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/selector_text_color_tab.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 9 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/shape_rect_solid_gray.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/shape_rect_solid_red.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/shape_stroke_bg_gray.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 7 | 8 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/svg_ic_arrow_right.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/svg_ic_boy.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 13 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/svg_ic_day_mode.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/svg_ic_fx.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 12 | 13 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/svg_ic_girl.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 13 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/svg_ic_github.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/svg_ic_line.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/svg_ic_night_mode.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/svg_ic_right.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/svg_ic_twitter.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/svg_ic_txl.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 12 | 13 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/svg_ic_user.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/svg_ic_w.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/svg_ic_wechat.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/svg_ic_weibo.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 12 | 15 | 18 | 21 | 24 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/svg_ic_wx.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 15 | 16 | 21 | 22 | 26 | 27 | 35 | 36 | 46 | 47 | 58 | 59 | 69 | 70 | 71 | 77 | 78 | 86 | 87 | 96 | 97 | 106 | 107 | 116 | 117 | 127 | 128 | 129 | 135 | 136 | 146 | 147 | 153 | 154 | 164 | 165 | 171 | 172 | 177 | 178 | 187 | 188 | 199 | 200 | 211 | 212 | 213 | 219 | 220 | 225 | 226 | 235 | 236 | 247 | 248 | 260 | 261 | 272 | 273 | 274 | 280 | 281 | 285 | 286 | 294 | 295 | 309 | 310 | 321 | 322 | 323 | 329 | 330 | 334 | 335 | 344 | 345 | 352 | 353 | 358 | 359 | 366 | 367 | 374 | 375 | 376 | 377 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/woxingxiao/VectorCompatTextView/3bbe8f41f8c79e9ff56c9aa24c334b924e0df300/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/woxingxiao/VectorCompatTextView/3bbe8f41f8c79e9ff56c9aa24c334b924e0df300/app/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/woxingxiao/VectorCompatTextView/3bbe8f41f8c79e9ff56c9aa24c334b924e0df300/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/woxingxiao/VectorCompatTextView/3bbe8f41f8c79e9ff56c9aa24c334b924e0df300/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/woxingxiao/VectorCompatTextView/3bbe8f41f8c79e9ff56c9aa24c334b924e0df300/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/woxingxiao/VectorCompatTextView/3bbe8f41f8c79e9ff56c9aa24c334b924e0df300/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/woxingxiao/VectorCompatTextView/3bbe8f41f8c79e9ff56c9aa24c334b924e0df300/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/woxingxiao/VectorCompatTextView/3bbe8f41f8c79e9ff56c9aa24c334b924e0df300/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/woxingxiao/VectorCompatTextView/3bbe8f41f8c79e9ff56c9aa24c334b924e0df300/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/woxingxiao/VectorCompatTextView/3bbe8f41f8c79e9ff56c9aa24c334b924e0df300/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | #f44336 7 | #8a8a8a 8 | #EFEFEF 9 | #4CAF50 10 | 11 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | VectorCompatTextView 3 | 4 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/test/java/com/xw/sample/vectorcompattextview/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.xw.sample.vectorcompattextview; 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 | google() 7 | } 8 | dependencies { 9 | classpath 'com.android.tools.build:gradle:4.1.1' 10 | 11 | // NOTE: Do not place your application dependencies here; they belong 12 | // in the individual module build.gradle files 13 | } 14 | } 15 | 16 | allprojects { 17 | repositories { 18 | jcenter() 19 | google() 20 | } 21 | } 22 | 23 | task clean(type: Delete) { 24 | delete rootProject.buildDir 25 | } 26 | -------------------------------------------------------------------------------- /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/woxingxiao/VectorCompatTextView/3bbe8f41f8c79e9ff56c9aa24c334b924e0df300/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Tue Dec 22 17:02:44 CST 2020 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-6.5-bin.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 | -------------------------------------------------------------------------------- /screenshot/demo5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/woxingxiao/VectorCompatTextView/3bbe8f41f8c79e9ff56c9aa24c334b924e0df300/screenshot/demo5.jpg -------------------------------------------------------------------------------- /screenshot/demo6.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/woxingxiao/VectorCompatTextView/3bbe8f41f8c79e9ff56c9aa24c334b924e0df300/screenshot/demo6.gif -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app', ':vectorcompattextview' 2 | -------------------------------------------------------------------------------- /vectorcompattextview/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /vectorcompattextview/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | 3 | android { 4 | compileSdkVersion 28 5 | 6 | defaultConfig { 7 | minSdkVersion 9 8 | targetSdkVersion 28 9 | versionCode 17 10 | versionName "2.9" 11 | 12 | } 13 | buildTypes { 14 | release { 15 | minifyEnabled false 16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 17 | } 18 | } 19 | } 20 | 21 | dependencies { 22 | compileOnly 'com.android.support:appcompat-v7:28.0.0' 23 | } 24 | -------------------------------------------------------------------------------- /vectorcompattextview/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 D:\Android\android-sdk-windows/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 | 19 | # Uncomment this to preserve the line number information for 20 | # debugging stack traces. 21 | #-keepattributes SourceFile,LineNumberTable 22 | 23 | # If you keep the line number information, uncomment this to 24 | # hide the original source file name. 25 | #-renamesourcefileattribute SourceFile 26 | -------------------------------------------------------------------------------- /vectorcompattextview/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /vectorcompattextview/src/main/java/com/xw/repo/VectorCompatTextView.java: -------------------------------------------------------------------------------- 1 | package com.xw.repo; 2 | 3 | import android.content.Context; 4 | import android.content.res.ColorStateList; 5 | import android.content.res.Resources; 6 | import android.content.res.TypedArray; 7 | import android.graphics.Canvas; 8 | import android.graphics.Color; 9 | import android.graphics.ColorFilter; 10 | import android.graphics.Paint; 11 | import android.graphics.PixelFormat; 12 | import android.graphics.Rect; 13 | import android.graphics.drawable.Drawable; 14 | import android.os.Build; 15 | import android.support.annotation.ColorInt; 16 | import android.support.annotation.DrawableRes; 17 | import android.support.annotation.NonNull; 18 | import android.support.annotation.Nullable; 19 | import android.support.v4.content.ContextCompat; 20 | import android.support.v4.graphics.drawable.DrawableCompat; 21 | import android.support.v7.content.res.AppCompatResources; 22 | import android.support.v7.widget.AppCompatCheckedTextView; 23 | import android.util.AttributeSet; 24 | import android.util.TypedValue; 25 | import android.view.ViewTreeObserver; 26 | 27 | import com.xw.repo.vectorcompattextview.R; 28 | 29 | /** 30 | * With this multifunctional {@code TextView}, the {@code VectorDrawable} resources can be set as 31 | * {@code CompoundDrawable}s, furthermore, the majority of scenarios to modify {@code CompoundDrawable}s 32 | * has been supported, such as checked state, color tinting/setting, custom size setting, visibility, 33 | * RTL etc. 34 | *
35 | * The most powerful {@code TextView} ever! 36 | *
37 | * Created by woxingxiao on 2017-03-19. 38 | */ 39 | public class VectorCompatTextView extends AppCompatCheckedTextView { 40 | 41 | private static final int DEFAULT_COLOR = Color.BLACK; 42 | 43 | private boolean isTintDrawableInTextColor; 44 | private ColorStateList mDrawableCompatTint; 45 | private boolean isDrawableAdjustTextWidth; 46 | private boolean isDrawableAdjustTextHeight; 47 | private boolean isDrawableAdjustViewWidth; 48 | private boolean isDrawableAdjustViewHeight; 49 | private int mDrawableWidth; 50 | private int mDrawableHeight; 51 | 52 | private Drawable mDrawableStart; 53 | private Drawable mDrawableTop; 54 | private Drawable mDrawableEnd; 55 | private Drawable mDrawableBottom; 56 | boolean hideDrawable = false; 57 | 58 | public VectorCompatTextView(Context context) { 59 | this(context, null); 60 | } 61 | 62 | public VectorCompatTextView(Context context, AttributeSet attrs) { 63 | this(context, attrs, 0); 64 | } 65 | 66 | public VectorCompatTextView(Context context, AttributeSet attrs, int defStyleAttr) { 67 | super(context, attrs, defStyleAttr); 68 | 69 | initAttrs(context, attrs); 70 | } 71 | 72 | private void initAttrs(Context context, AttributeSet attrs) { 73 | if (attrs != null) { 74 | TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.VectorCompatTextView); 75 | 76 | Drawable ds = null; 77 | Drawable dt = null; 78 | Drawable de = null; 79 | Drawable db = null; 80 | 81 | Drawable dl = null; 82 | Drawable dr = null; 83 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 84 | ds = a.getDrawable(R.styleable.VectorCompatTextView_drawableStartCompat); 85 | dt = a.getDrawable(R.styleable.VectorCompatTextView_drawableTopCompat); 86 | de = a.getDrawable(R.styleable.VectorCompatTextView_drawableEndCompat); 87 | db = a.getDrawable(R.styleable.VectorCompatTextView_drawableBottomCompat); 88 | 89 | dl = a.getDrawable(R.styleable.VectorCompatTextView_drawableLeftCompat); 90 | dr = a.getDrawable(R.styleable.VectorCompatTextView_drawableRightCompat); 91 | } else { 92 | int dsId = a.getResourceId(R.styleable.VectorCompatTextView_drawableStartCompat, -1); 93 | int dtId = a.getResourceId(R.styleable.VectorCompatTextView_drawableTopCompat, -1); 94 | int deId = a.getResourceId(R.styleable.VectorCompatTextView_drawableEndCompat, -1); 95 | int dbId = a.getResourceId(R.styleable.VectorCompatTextView_drawableBottomCompat, -1); 96 | 97 | int dlId = a.getResourceId(R.styleable.VectorCompatTextView_drawableLeftCompat, -1); 98 | int drId = a.getResourceId(R.styleable.VectorCompatTextView_drawableRightCompat, -1); 99 | 100 | if (dsId != -1) ds = AppCompatResources.getDrawable(context, dsId); 101 | if (dtId != -1) dt = AppCompatResources.getDrawable(context, dtId); 102 | if (deId != -1) de = AppCompatResources.getDrawable(context, deId); 103 | if (dbId != -1) db = AppCompatResources.getDrawable(context, dbId); 104 | 105 | if (dlId != -1) dl = AppCompatResources.getDrawable(context, dlId); 106 | if (drId != -1) dr = AppCompatResources.getDrawable(context, drId); 107 | } 108 | 109 | isTintDrawableInTextColor = a.getBoolean(R.styleable.VectorCompatTextView_tintDrawableInTextColor, false); 110 | if (a.hasValue(R.styleable.VectorCompatTextView_drawableCompatTint)) { 111 | mDrawableCompatTint = a.getColorStateList(R.styleable.VectorCompatTextView_drawableCompatTint); 112 | } else if (a.hasValue(R.styleable.VectorCompatTextView_drawableCompatColor)) { 113 | // @deprecated 114 | // Use drawableCompatTint instead. 115 | int color = a.getColor(R.styleable.VectorCompatTextView_drawableCompatColor, DEFAULT_COLOR); 116 | mDrawableCompatTint = ColorStateList.valueOf(color); 117 | } 118 | isDrawableAdjustTextWidth = a.getBoolean(R.styleable.VectorCompatTextView_drawableAdjustTextWidth, false); 119 | isDrawableAdjustTextHeight = a.getBoolean(R.styleable.VectorCompatTextView_drawableAdjustTextHeight, false); 120 | isDrawableAdjustViewWidth = a.getBoolean(R.styleable.VectorCompatTextView_drawableAdjustViewWidth, false); 121 | isDrawableAdjustViewHeight = a.getBoolean(R.styleable.VectorCompatTextView_drawableAdjustViewHeight, false); 122 | mDrawableWidth = a.getDimensionPixelSize(R.styleable.VectorCompatTextView_drawableWidth, 0); 123 | mDrawableHeight = a.getDimensionPixelSize(R.styleable.VectorCompatTextView_drawableHeight, 0); 124 | hideDrawable = a.getBoolean(R.styleable.VectorCompatTextView_hideDrawable, false); 125 | a.recycle(); 126 | 127 | if (mDrawableWidth < 0) 128 | mDrawableWidth = 0; 129 | if (mDrawableHeight < 0) 130 | mDrawableHeight = 0; 131 | if (isDrawableAdjustTextWidth) 132 | isDrawableAdjustViewWidth = false; 133 | if (isDrawableAdjustTextHeight) 134 | isDrawableAdjustViewHeight = false; 135 | 136 | mDrawableStart = ds; 137 | mDrawableTop = dt; 138 | mDrawableEnd = de; 139 | mDrawableBottom = db; 140 | if (ds == null) 141 | mDrawableStart = dl; 142 | if (de == null) 143 | mDrawableEnd = dr; 144 | 145 | boolean initHidden = hideDrawable; 146 | if (initHidden) { 147 | hideDrawable = false; 148 | } 149 | 150 | behaveDrawables(); 151 | 152 | if (initHidden) { 153 | hideDrawable = true; 154 | behaveDrawables(); 155 | } 156 | } 157 | } 158 | 159 | private void behaveDrawables(Drawable... drawables) { 160 | if (drawables == null || drawables.length != 4) { 161 | drawables = new Drawable[]{mDrawableStart, mDrawableTop, mDrawableEnd, mDrawableBottom}; 162 | } 163 | for (Drawable drawable : drawables) { 164 | tintDrawable(drawable); 165 | } 166 | 167 | if (!isDrawableAdjustTextWidth && !isDrawableAdjustTextHeight && !isDrawableAdjustViewWidth 168 | && !isDrawableAdjustViewHeight && mDrawableWidth == 0 && mDrawableHeight == 0) { 169 | compatCompoundDrawablesWithIntrinsicBounds(); 170 | return; 171 | } 172 | 173 | if (isDrawableAdjustTextWidth || isDrawableAdjustTextHeight || isDrawableAdjustViewWidth || isDrawableAdjustViewHeight) { 174 | boolean invalid = ((isDrawableAdjustTextWidth || isDrawableAdjustViewWidth) && (mDrawableStart != null || mDrawableEnd != null)) 175 | || ((isDrawableAdjustTextHeight || isDrawableAdjustViewHeight) && (mDrawableTop != null || mDrawableBottom != null)); 176 | if (invalid) { 177 | if (mDrawableWidth > 0 || mDrawableHeight > 0) { 178 | resizeCompoundDrawablesInCompatibility(drawables); 179 | } else { 180 | compatCompoundDrawablesWithIntrinsicBounds(); 181 | } 182 | } else { 183 | getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 184 | @Override 185 | public void onGlobalLayout() { 186 | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) { 187 | getViewTreeObserver().removeGlobalOnLayoutListener(this); 188 | } else { 189 | getViewTreeObserver().removeOnGlobalLayoutListener(this); 190 | } 191 | 192 | adjustCompoundDrawablesInCompatibility(); 193 | } 194 | }); 195 | } 196 | return; 197 | } 198 | 199 | if (mDrawableWidth > 0 || mDrawableHeight > 0) { 200 | resizeCompoundDrawablesInCompatibility(drawables); 201 | } 202 | } 203 | 204 | private void tintDrawable(Drawable drawable) { 205 | if (drawable != null) { 206 | Drawable wrapped = DrawableCompat.wrap(drawable.mutate()); 207 | if (isTintDrawableInTextColor) { 208 | DrawableCompat.setTint(wrapped, getCurrentTextColor()); 209 | } else if (mDrawableCompatTint != null) { 210 | DrawableCompat.setTintList(wrapped, mDrawableCompatTint); 211 | } 212 | } 213 | } 214 | 215 | private void resizeCompoundDrawablesInCompatibility(Drawable[] drawables) { 216 | for (Drawable drawable : drawables) { 217 | if (drawable == null) { 218 | continue; 219 | } 220 | 221 | if (mDrawableWidth > 0 && mDrawableHeight > 0) { 222 | drawable.setBounds(0, 0, mDrawableWidth, mDrawableHeight); 223 | } else if (mDrawableWidth > 0) { 224 | int h = mDrawableWidth * drawable.getIntrinsicHeight() / drawable.getIntrinsicWidth(); 225 | drawable.setBounds(0, 0, mDrawableWidth, h); 226 | } else { 227 | int w = mDrawableHeight * drawable.getIntrinsicWidth() / drawable.getIntrinsicHeight(); 228 | drawable.setBounds(0, 0, w, mDrawableHeight); 229 | } 230 | } 231 | 232 | compatCompoundDrawablesWithProbablyChangedBounds(); 233 | } 234 | 235 | private void adjustCompoundDrawablesInCompatibility() { 236 | int width = 0; 237 | int height = 0; 238 | 239 | if (isDrawableAdjustTextWidth) { 240 | Paint paint = new Paint(); 241 | paint.setTextSize(getTextSize()); 242 | CharSequence text = getText(); 243 | Rect rect = new Rect(); 244 | paint.getTextBounds(text.toString(), 0, text.length(), rect); 245 | 246 | width = rect.width(); 247 | } else if (isDrawableAdjustViewWidth) { 248 | width = getMeasuredWidth(); 249 | } 250 | if (isDrawableAdjustTextHeight) { 251 | Paint paint = new Paint(); 252 | paint.setTextSize(getTextSize()); 253 | CharSequence text = getText(); 254 | Rect rect = new Rect(); 255 | paint.getTextBounds(text.toString(), 0, text.length(), rect); 256 | 257 | height = rect.height(); 258 | } else if (isDrawableAdjustViewHeight) { 259 | height = getMeasuredHeight(); 260 | } 261 | 262 | int h = mDrawableHeight; 263 | int w = mDrawableWidth; 264 | 265 | if (mDrawableTop != null) { 266 | if (h == 0) 267 | h = width * mDrawableTop.getIntrinsicHeight() / mDrawableTop.getIntrinsicWidth(); 268 | mDrawableTop.setBounds(0, 0, width, h); 269 | } 270 | if (mDrawableBottom != null) { 271 | if (h == 0) 272 | h = width * mDrawableBottom.getIntrinsicHeight() / mDrawableBottom.getIntrinsicWidth(); 273 | mDrawableBottom.setBounds(0, 0, width, h); 274 | } 275 | 276 | if (mDrawableStart != null) { 277 | if (w == 0) 278 | w = height * mDrawableStart.getIntrinsicWidth() / mDrawableStart.getIntrinsicHeight(); 279 | mDrawableStart.setBounds(0, 0, w, height); 280 | } 281 | if (mDrawableEnd != null) { 282 | if (w == 0) 283 | w = height * mDrawableEnd.getIntrinsicWidth() / mDrawableEnd.getIntrinsicHeight(); 284 | mDrawableEnd.setBounds(0, 0, w, height); 285 | } 286 | 287 | compatCompoundDrawablesWithProbablyChangedBounds(); 288 | } 289 | 290 | private void compatCompoundDrawablesWithIntrinsicBounds() { 291 | if (hideDrawable) { 292 | InvisibleDrawable invisibleDrawableStart = createInvisibleDrawable(mDrawableStart); 293 | InvisibleDrawable invisibleDrawableTop = createInvisibleDrawable(mDrawableTop); 294 | InvisibleDrawable invisibleDrawableEnd = createInvisibleDrawable(mDrawableEnd); 295 | InvisibleDrawable invisibleDrawableBottom = createInvisibleDrawable(mDrawableBottom); 296 | 297 | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) { 298 | setCompoundDrawablesWithIntrinsicBounds( 299 | invisibleDrawableStart, invisibleDrawableTop, invisibleDrawableEnd, invisibleDrawableBottom); 300 | } else { 301 | setCompoundDrawablesRelativeWithIntrinsicBounds( 302 | invisibleDrawableStart, invisibleDrawableTop, invisibleDrawableEnd, invisibleDrawableBottom); 303 | } 304 | } else { 305 | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) { 306 | setCompoundDrawablesWithIntrinsicBounds( 307 | mDrawableStart, mDrawableTop, mDrawableEnd, mDrawableBottom); 308 | } else { 309 | setCompoundDrawablesRelativeWithIntrinsicBounds( 310 | mDrawableStart, mDrawableTop, mDrawableEnd, mDrawableBottom); 311 | } 312 | } 313 | } 314 | 315 | private void compatCompoundDrawablesWithProbablyChangedBounds() { 316 | if (hideDrawable) { 317 | InvisibleDrawable invisibleDrawableStart = createInvisibleDrawable(mDrawableStart); 318 | InvisibleDrawable invisibleDrawableTop = createInvisibleDrawable(mDrawableTop); 319 | InvisibleDrawable invisibleDrawableEnd = createInvisibleDrawable(mDrawableEnd); 320 | InvisibleDrawable invisibleDrawableBottom = createInvisibleDrawable(mDrawableBottom); 321 | 322 | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) { 323 | setCompoundDrawables( 324 | invisibleDrawableStart, invisibleDrawableTop, invisibleDrawableEnd, invisibleDrawableBottom); 325 | } else { 326 | setCompoundDrawablesRelative( 327 | invisibleDrawableStart, invisibleDrawableTop, invisibleDrawableEnd, invisibleDrawableBottom); 328 | } 329 | } else { 330 | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) { 331 | setCompoundDrawables(mDrawableStart, mDrawableTop, mDrawableEnd, mDrawableBottom); 332 | } else { 333 | setCompoundDrawablesRelative(mDrawableStart, mDrawableTop, mDrawableEnd, mDrawableBottom); 334 | } 335 | } 336 | } 337 | 338 | private InvisibleDrawable createInvisibleDrawable(Drawable drawable) { 339 | if (drawable != null) { 340 | InvisibleDrawable invisibleDrawable = new InvisibleDrawable(); 341 | invisibleDrawable.updateBounds(drawable.getBounds()); 342 | return invisibleDrawable; 343 | } 344 | return null; 345 | } 346 | 347 | private Drawable[] getCompoundDrawablesInCompatibility() { 348 | Drawable[] drawables; 349 | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) { 350 | drawables = getCompoundDrawables(); 351 | } else { 352 | drawables = getCompoundDrawablesRelative(); 353 | } 354 | if (!(drawables[0] instanceof InvisibleDrawable)) 355 | mDrawableStart = drawables[0]; 356 | if (!(drawables[1] instanceof InvisibleDrawable)) 357 | mDrawableTop = drawables[1]; 358 | if (!(drawables[2] instanceof InvisibleDrawable)) 359 | mDrawableEnd = drawables[2]; 360 | if (!(drawables[3] instanceof InvisibleDrawable)) 361 | mDrawableBottom = drawables[3]; 362 | 363 | return drawables; 364 | } 365 | 366 | private void tintCompoundDrawables() { 367 | Drawable[] drawables = getCompoundDrawablesInCompatibility(); 368 | for (Drawable drawable : drawables) { 369 | tintDrawable(drawable); 370 | } 371 | 372 | compatCompoundDrawablesWithIntrinsicBounds(); 373 | } 374 | 375 | @Override 376 | public void setText(CharSequence text, BufferType type) { 377 | super.setText(text, type); 378 | 379 | if (isDrawableAdjustTextWidth || isDrawableAdjustTextHeight) { 380 | Drawable[] drawables = getCompoundDrawablesInCompatibility(); 381 | if (drawables[0] == null && drawables[1] == null && drawables[2] == null && drawables[3] == null) 382 | return; 383 | 384 | adjustCompoundDrawablesInCompatibility(); 385 | } 386 | } 387 | 388 | @Override 389 | public void setTextColor(@ColorInt int color) { 390 | super.setTextColor(color); 391 | 392 | tintCompoundDrawables(); 393 | } 394 | 395 | public boolean isTintDrawableInTextColor() { 396 | return isTintDrawableInTextColor; 397 | } 398 | 399 | public void setTintDrawableInTextColor(boolean tintDrawableInTextColor) { 400 | if (isTintDrawableInTextColor == tintDrawableInTextColor) 401 | return; 402 | 403 | isTintDrawableInTextColor = tintDrawableInTextColor; 404 | tintCompoundDrawables(); 405 | } 406 | 407 | public ColorStateList getDrawableCompatTint() { 408 | return mDrawableCompatTint; 409 | } 410 | 411 | public void setDrawableCompatTint(ColorStateList colorStateList) { 412 | if (mDrawableCompatTint == colorStateList) 413 | return; 414 | 415 | mDrawableCompatTint = colorStateList; 416 | tintCompoundDrawables(); 417 | } 418 | 419 | public int getDrawableCompatColor() { 420 | return mDrawableCompatTint == null ? DEFAULT_COLOR : mDrawableCompatTint.getColorForState(getDrawableState(), DEFAULT_COLOR); 421 | } 422 | 423 | /** 424 | * @deprecated Use {@link #setDrawableCompatTint} instead. 425 | */ 426 | @Deprecated 427 | public void setDrawableCompatColor(@ColorInt int color) { 428 | mDrawableCompatTint = ColorStateList.valueOf(color); 429 | tintCompoundDrawables(); 430 | } 431 | 432 | public boolean isHideDrawable() { 433 | return hideDrawable; 434 | } 435 | 436 | public void setHideDrawable(boolean hideDrawable) { 437 | if (this.hideDrawable == hideDrawable) 438 | return; 439 | 440 | this.hideDrawable = hideDrawable; 441 | compatCompoundDrawablesWithIntrinsicBounds(); 442 | } 443 | 444 | @Override 445 | public void setChecked(boolean checked) { 446 | super.setChecked(checked); 447 | 448 | behaveDrawables(getCompoundDrawablesInCompatibility()); 449 | } 450 | 451 | @Override 452 | public void toggle() { 453 | super.toggle(); 454 | 455 | behaveDrawables(getCompoundDrawablesInCompatibility()); 456 | } 457 | 458 | @Override 459 | protected void drawableStateChanged() { 460 | super.drawableStateChanged(); 461 | 462 | if (isTintDrawableInTextColor || mDrawableCompatTint != null) { 463 | Drawable[] drawables = getCompoundDrawablesInCompatibility(); 464 | 465 | boolean needRefresh = false; 466 | for (Drawable drawable : drawables) { 467 | if (drawable != null) { 468 | needRefresh = true; 469 | break; 470 | } 471 | } 472 | 473 | if (needRefresh) { 474 | tintCompoundDrawables(); 475 | } 476 | } 477 | } 478 | 479 | 480 | //////////////////////////////////////////////////////////////////////////////////////////////// 481 | 482 | /** 483 | * When {@link #hideDrawable} set to true, a {@link InvisibleDrawable} will be created as a place 484 | * holder, which can make sure that the bounds of the {@code TextView} will not change. 485 | */ 486 | private static class InvisibleDrawable extends Drawable { 487 | 488 | private final Rect mRect = new Rect(); 489 | 490 | void updateBounds(Rect rect) { 491 | if (rect != null) { 492 | mRect.set(rect); 493 | } 494 | } 495 | 496 | @Override 497 | public void draw(@NonNull Canvas canvas) { 498 | } 499 | 500 | @Override 501 | public void setAlpha(int alpha) { 502 | } 503 | 504 | @Override 505 | public void setColorFilter(@Nullable ColorFilter colorFilter) { 506 | } 507 | 508 | @Override 509 | public int getOpacity() { 510 | return PixelFormat.TRANSPARENT; 511 | } 512 | 513 | @Override 514 | public int getIntrinsicWidth() { 515 | return mRect.width(); 516 | } 517 | 518 | @Override 519 | public int getIntrinsicHeight() { 520 | return mRect.height(); 521 | } 522 | } 523 | 524 | //////////////////////////////////////////////////////////////////////////////////////////////// 525 | public static class CompoundDrawableConfigBuilder { 526 | 527 | private final VectorCompatTextView mVectorCompatTextView; 528 | 529 | public CompoundDrawableConfigBuilder(@NonNull VectorCompatTextView vectorCompatTextView) { 530 | mVectorCompatTextView = vectorCompatTextView; 531 | } 532 | 533 | public CompoundDrawableConfigBuilder setCompoundDrawables(Drawable start, Drawable top, Drawable end, Drawable bottom) { 534 | mVectorCompatTextView.mDrawableStart = start; 535 | mVectorCompatTextView.mDrawableTop = top; 536 | mVectorCompatTextView.mDrawableEnd = end; 537 | mVectorCompatTextView.mDrawableBottom = bottom; 538 | return this; 539 | } 540 | 541 | public CompoundDrawableConfigBuilder setDrawableStart(Drawable start) { 542 | mVectorCompatTextView.mDrawableStart = start; 543 | return this; 544 | } 545 | 546 | public CompoundDrawableConfigBuilder setDrawableTop(Drawable top) { 547 | mVectorCompatTextView.mDrawableTop = top; 548 | return this; 549 | } 550 | 551 | public CompoundDrawableConfigBuilder setDrawableEnd(Drawable end) { 552 | mVectorCompatTextView.mDrawableEnd = end; 553 | return this; 554 | } 555 | 556 | public CompoundDrawableConfigBuilder setDrawableBottom(Drawable bottom) { 557 | mVectorCompatTextView.mDrawableBottom = bottom; 558 | return this; 559 | } 560 | 561 | public CompoundDrawableConfigBuilder setDrawableStart(@DrawableRes int startRes) { 562 | setDrawableStart(ContextCompat.getDrawable(mVectorCompatTextView.getContext(), startRes)); 563 | return this; 564 | } 565 | 566 | public CompoundDrawableConfigBuilder setDrawableTop(@DrawableRes int topRes) { 567 | setDrawableTop(ContextCompat.getDrawable(mVectorCompatTextView.getContext(), topRes)); 568 | return this; 569 | } 570 | 571 | public CompoundDrawableConfigBuilder setDrawableEnd(@DrawableRes int endRes) { 572 | setDrawableEnd(ContextCompat.getDrawable(mVectorCompatTextView.getContext(), endRes)); 573 | return this; 574 | } 575 | 576 | public CompoundDrawableConfigBuilder setDrawableBottom(@DrawableRes int bottomRes) { 577 | setDrawableBottom(ContextCompat.getDrawable(mVectorCompatTextView.getContext(), bottomRes)); 578 | return this; 579 | } 580 | 581 | public CompoundDrawableConfigBuilder tintDrawableInTextColor() { 582 | mVectorCompatTextView.isTintDrawableInTextColor = true; 583 | return this; 584 | } 585 | 586 | public CompoundDrawableConfigBuilder setDrawableTint(ColorStateList colorStateList) { 587 | mVectorCompatTextView.mDrawableCompatTint = colorStateList; 588 | return this; 589 | } 590 | 591 | /** 592 | * @deprecated Use {@link #setDrawableTint} instead. 593 | */ 594 | @Deprecated 595 | public CompoundDrawableConfigBuilder setDrawableColor(@ColorInt int color) { 596 | mVectorCompatTextView.mDrawableCompatTint = ColorStateList.valueOf(color); 597 | return this; 598 | } 599 | 600 | public CompoundDrawableConfigBuilder drawableWidthAdjustTextWidth() { 601 | mVectorCompatTextView.isDrawableAdjustTextWidth = true; 602 | return this; 603 | } 604 | 605 | public CompoundDrawableConfigBuilder drawableHeightAdjustTextHeight() { 606 | mVectorCompatTextView.isDrawableAdjustTextHeight = true; 607 | return this; 608 | } 609 | 610 | public CompoundDrawableConfigBuilder drawableWidthAdjustViewWidth() { 611 | mVectorCompatTextView.isDrawableAdjustViewWidth = true; 612 | return this; 613 | } 614 | 615 | public CompoundDrawableConfigBuilder drawableHeightAdjustViewHeight() { 616 | mVectorCompatTextView.isDrawableAdjustViewHeight = true; 617 | return this; 618 | } 619 | 620 | public CompoundDrawableConfigBuilder setDrawableWidth(int widthInDp) { 621 | mVectorCompatTextView.mDrawableWidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 622 | widthInDp, Resources.getSystem().getDisplayMetrics()); 623 | return this; 624 | } 625 | 626 | public CompoundDrawableConfigBuilder setDrawableHeight(int heightInDp) { 627 | mVectorCompatTextView.mDrawableHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 628 | heightInDp, Resources.getSystem().getDisplayMetrics()); 629 | return this; 630 | } 631 | 632 | public void build() { 633 | mVectorCompatTextView.behaveDrawables(); 634 | } 635 | } 636 | } -------------------------------------------------------------------------------- /vectorcompattextview/src/main/res/values/attrs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | --------------------------------------------------------------------------------