본문 바로가기

ANDROID

[안드로이드] SDK 버전에 구애받지 않고 별도의 클래스에서 이미지 asset 참조해 적용하기

작업을 하면서 Activity가 아닌 클래스에서 이미지 asset을 참조해

 

이를 이미지뷰에 적용하고자 하였는데, 리소스 혹은 context로부터 getDrawable을 하면

 

SDK limit이 걸리는 걸 확인할 수 있었다. (SDK 버전 21 아래에서는 먹히지 않는다는 코드)

 

 

대안을 찾고자 검색을 하였고 ContextComnpat을 통해 SDK 버전에 구애받지 않으면서

 

Activity가 아닌 별도의 클래스에서 이미지 asset을 참조할 수 있었다.

 


 

ContextCompat.getDrawable(getContext(), R.drawable.참조할Drawable)

 

이를 이미지뷰에 넣고자 할 땐

 

myImageView.setImageDrawable(상단의코드)

 

< 참조 >

 

https://stackoverflow.com/questions/41175941/what-to-use-in-place-of-getdrawableint-in-lower-api-like-15

 

What to use In place of getDrawable(int) in lower API like 15

I am writing an app where I use below code but it's for API 21 and above. ImageView i = (ImageView) findViewById(R.id.tickcross); i.setImageDrawable(getDrawable(R.drawable.weirdtick)); What can I...

stackoverflow.com