티스토리 뷰

반응형

Stack Overflow에 자주 검색, 등록되는 문제들과 제가 개발 중 찾아 본 문제들 중에서 나중에도 찾아 볼 것 같은 문제들을 정리하고 있습니다.

Stack Overflow에서 가장 먼저 확인하게 되는 가장 높은 점수를 받은 Solution과 현 시점에 도움이 될 수 있는 가장 최근에 업데이트(최소 점수 확보)된 Solution을 각각 정리하였습니다.

 

아래 word cloud를 통해 이번 포스팅의 주요 키워드를 미리 확인하세요.

How to set tint for an image view programmatically in android?

안드로이드에서 ImageView에 tint(색조)를 어떻게 프로그래밍 방식으로 설정하나요?

 문제 내용 

Need to set tint for an image view... I am using it the following way:

이미지 뷰에 tint를 설정해야합니다... 저는 다음과 같이 사용하고 있습니다:
imageView.setColorFilter(R.color.blue,android.graphics.PorterDuff.Mode.MULTIPLY);

 

But it doesn't change...

하지만 변경되지 않습니다...

 

 

 

 높은 점수를 받은 Solution 

UPDATE:
@ADev has newer solution in his answer here, but his solution requires newer support library - 25.4.0 or above.

업데이트: @ADev의 답변에서 더 나은 솔루션이 제공되었지만, 그 솔루션은 지원 라이브러리의 새로운 버전인 25.4.0 이상이 필요합니다.

 


 

You can change the tint, quite easily in code via:

코드에서 쉽게 틴트를 변경할 수 있습니다.

 

imageView.setColorFilter(Color.argb(255, 255, 255, 255)); // White Tint

imageView.setColorFilter(Color.argb(255, 255, 255, 255)); // 흰색 틴트

 

If you want color tint then

만약 색상 틴트를 원한다면
imageView.setColorFilter(ContextCompat.getColor(context, R.color.COLOR_YOUR_COLOR), android.graphics.PorterDuff.Mode.MULTIPLY);

 

For Vector Drawable

이는 벡터 드로어블(Vector Drawable)에 대한 설명입니다.
imageView.setColorFilter(ContextCompat.getColor(context, R.color.COLOR_YOUR_COLOR), android.graphics.PorterDuff.Mode.SRC_IN);

 

 

 가장 최근 달린 Solution 

An extension function in kotlin, to set and unset the tinting.

Kotlin에서 사용할 수 있는 확장 함수로서, Tint를 설정하거나 해제할 수 있습니다.
fun ImageView.setTint(@ColorRes color: Int?) {
  if (color == null) {
    ImageViewCompat.setImageTintList(this, null)
  } else {
    ImageViewCompat.setImageTintList(this, ColorStateList.valueOf(ContextCompat.getColor(context, color)))
}}

 

Usage: yourImageView.setTint(R.color.white) for setting and for removing just: yourImageView.setTint(null)

사용법: `yourImageView.setTint(R.color.white)`로 설정하고 삭제할 때는 `yourImageView.setTint(null)`로 지정합니다.

 

 

 

출처 : https://stackoverflow.com/questions/20121938/how-to-set-tint-for-an-image-view-programmatically-in-android

반응형
댓글
공지사항
최근에 올라온 글