添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
// Tints the drawable by color int.
fun Drawable?.tint(@ColorInt tint: Int): Drawable? {
    return this?.apply {
        if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            colorFilter = PorterDuffColorFilter(tint, PorterDuff.Mode.SRC_IN)
        } else {
            setTint(tint)
// Tints the drawable by color String.
fun Drawable?.tint(@Size(min = 1) colorString: String): Drawable? {
    return tint(Color.parseColor(colorString))

MainActivity.kt

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        // black, Original
        val drawable = ContextCompat.getDrawable(this, R.drawable.ic_close)?.mutate()
        image.setImageDrawable(drawable)
        // Red
        val drawableRed = ContextCompat.getDrawable(this, R.drawable.ic_close)?.mutate().tint(Color.RED)
        imageRed.setImageDrawable(drawableRed)
        // Green
        val drawableGreen = ContextCompat.getDrawable(this, R.drawable.ic_close)?.mutate().tint(Color.GREEN)
        imageGreen.setImageDrawable(drawableGreen)
        // Blue
        val drawableBlue = ContextCompat.getDrawable(this, R.drawable.ic_close)?.mutate().tint(Color.BLUE)
        imageBlue.setImageDrawable(drawableBlue)

R.layout.activity_main

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <ImageView
        android:id="@+id/imageRed"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <ImageView
        android:id="@+id/imageGreen"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <ImageView
        android:id="@+id/imageBlue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>