0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Androidでアイコンや画像の色を変更する方法

0
Posted at

Android で画像やアイコンの色を変更する方法をメモします。

<ImageView
    android:id="@+id/icon"
    android:layout_width="24dp"
    android:layout_height="24dp"
    android:src="@drawable/ic_home"/>

imageTintList

これだけでアイコン全体を赤色に変更できます。

imageView.imageTintList = ColorStateList.valueOf(Color.RED)

setColorFilter を使う

昔からある方法です。現在は imageTintListの方が推奨されています。

imageView.setColorFilter(Color.BLUE)

Drawable にtint を適用する

Drawable を取得して色変更を行います。この方法は同じアイコンを別色で使いたい、動的に切り替えたい場合に便利です。

val drawable = ContextCompat.getDrawable(this, R.drawable.ic_home)
drawable?.setTint(Color.GREEN)
imageView.setImageDrawable(drawable)

画像を暗くする

画像を暗くしたい場合は ColorFilter が便利です。透過度を設定でき画像が暗く見えます。

imageView.setColorFilter(Color.argb(120, 0, 0, 0))

グラデーションを使う

背景やボタンの装飾で使われます。

val gradient = GradientDrawable(
    GradientDrawable.Orientation.LEFT_RIGHT,
    intArrayOf(Color.RED, Color.BLUE)
)

imageView.background = gradient

アニメーションで変更する

状態変化を演出したい場合に便利です。

ValueAnimator.ofArgb(
    Color.RED,
    Color.BLUE
).apply {
    duration = 1000
    addUpdateListener {
        imageView.setColorFilter(
            it.animatedValue as Int
        )
    }
    start()
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?