Skip to content

Commit

Permalink
Feature: added the drawable and textview utils.
Browse files Browse the repository at this point in the history
  • Loading branch information
pokk committed Feb 19, 2019
1 parent aa55dcd commit a704bd3
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 10 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ allprojects {
And add our dependency to your app `bundle.gradle`.

```gradle
implementation 'com.devrapid.jieyi:kotlinknifer:2.1.4'
implementation 'com.devrapid.jieyi:kotlinknifer:2.1.5'
implementation 'com.devrapid.jieyi:kotlinshaver:1.1.4'
implementation 'com.devrapid.jieyi:kotlinshaver:1.1.5'
```

Then you can use it!!!
Expand All @@ -107,14 +107,14 @@ Then you can use it!!!
<dependency>
<groupId>com.devrapid.jieyi</groupId>
<artifactId>kotlinknifer</artifactId>
<version>2.1.4</version>
<version>2.1.5</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>com.devrapid.jieyi</groupId>
<artifactId>kotlinshaver</artifactId>
<version>1.1.4</version>
<version>1.1.5</version>
<type>pom</type>
</dependency>
```
Expand All @@ -135,14 +135,14 @@ Then you can use it!!!
If you'd not like to use them to your project, you can add the exclude as like below

```gradle
implementation('com.devrapid.jieyi:kotlinknifer:2.1.4', {
implementation('com.devrapid.jieyi:kotlinknifer:2.1.5', {
exclude group: 'com.google.code.gson', module: 'gson'
exclude group: 'com.github.bumptech.glide', module: 'glide'
})
```

```gradle
implementation('com.devrapid.jieyi:kotlinshaverr:1.1.4', {
implementation('com.devrapid.jieyi:kotlinshaverr:1.1.5', {
exclude group: 'io.reactivex.rxjava2', module: 'rxjava'
exclude group: 'io.reactivex.rxjava2', module: 'rxkotlin'
})
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ buildscript {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.0'
classpath 'com.android.tools.build:gradle:3.3.1'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ proj_name=kotlinknifer
proj_libname=KotlinKnifer
proj_group=com.devrapid.jieyi
proj_artifactid=kotlinknifer
proj_version=2.1.4
proj_version=2.1.5
proj_description=For developing an Android conveniently and rapidly.
proj_websiteurl=https://github.com/pokk/KotlinKnifer
proj_issuetrackerurl=https://github.com/pokk/KotlinKnifer/issues
Expand All @@ -39,4 +39,4 @@ [email protected]
proj_kotlin_name=kotlinshaver
proj_kotlin_libname=KotlinShaver
proj_kotlin_artifactid=kotlinshaver
proj_kotlin_version=1.1.4
proj_kotlin_version=1.1.5
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ package com.devrapid.kotlinknifer

import android.content.Context
import android.graphics.Bitmap
import android.graphics.PorterDuff
import android.graphics.PorterDuffColorFilter
import android.graphics.Rect
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
Expand Down Expand Up @@ -40,6 +42,10 @@ fun Context.scaledDrawable(@DrawableRes drawableId: Int, ratioWidth: Float, rati
fun Context.scaledDrawable(@DrawableRes drawableId: Int, ratio: Float) =
scaledDrawable(drawableId, ratio, ratio)

inline fun Drawable.changeColor(color: Int) = apply {
colorFilter = PorterDuffColorFilter(color, PorterDuff.Mode.SRC_IN)
}

inline fun Int.toDrawable(context: Context) = context.getDrawable(this)

inline fun Bitmap.toDrawable(context: Context) = BitmapDrawable(context.resources, this)
Expand Down
44 changes: 43 additions & 1 deletion kotlinknifer/src/main/java/com/devrapid/kotlinknifer/TextView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,18 @@ annotation class DrawableDirection

fun TextView.addDrawable(
@DrawableRes drawableId: Int,
color: Int,
@DrawableDirection direct: Int,
ratioWidth: Float = 1f,
ratioHeight: Float = 1f
) {
// Modify the icon size.
context.scaledDrawable(drawableId, ratioWidth, ratioHeight)
// Modify the icon bound.
.apply { setBounds(0, 0, (minimumWidth * ratioWidth).toInt(), (minimumHeight * ratioHeight).toInt()) }
.apply {
setBounds(0, 0, (minimumWidth * ratioWidth).toInt(), (minimumHeight * ratioHeight).toInt())
if (color != 0) changeColor(color)
}
.let {
when (direct) {
DRAWABLE_DIRECTION_START -> setCompoundDrawables(it, null, null, null)
Expand All @@ -50,3 +54,41 @@ fun TextView.addDrawable(
}
}
}

fun TextView.addDrawable(
@DrawableRes drawableId: Int,
@DrawableDirection direct: Int,
ratioWidth: Float = 1f,
ratioHeight: Float = 1f
) = addDrawable(drawableId, 0, direct, ratioWidth, ratioHeight)

fun TextView.addDrawableWithIntrinsicBounds(
@DrawableRes drawableId: Int,
color: Int,
@DrawableDirection direct: Int,
ratioWidth: Float = 1f,
ratioHeight: Float = 1f
) {
// Modify the icon size.
context.scaledDrawable(drawableId, ratioWidth, ratioHeight)
// Modify the icon bound.
.apply {
setBounds(0, 0, (minimumWidth * ratioWidth).toInt(), (minimumHeight * ratioHeight).toInt())
if (color != 0) changeColor(color)
}
.let {
when (direct) {
DRAWABLE_DIRECTION_START -> setCompoundDrawablesWithIntrinsicBounds(it, null, null, null)
DRAWABLE_DIRECTION_TOP -> setCompoundDrawablesWithIntrinsicBounds(null, it, null, null)
DRAWABLE_DIRECTION_END -> setCompoundDrawablesWithIntrinsicBounds(null, null, it, null)
DRAWABLE_DIRECTION_BOTTOM -> setCompoundDrawablesWithIntrinsicBounds(null, null, null, it)
}
}
}

fun TextView.addDrawableWithIntrinsicBounds(
@DrawableRes drawableId: Int,
@DrawableDirection direct: Int,
ratioWidth: Float = 1f,
ratioHeight: Float = 1f
) = addDrawableWithIntrinsicBounds(drawableId, 0, direct, ratioWidth, ratioHeight)

0 comments on commit a704bd3

Please sign in to comment.