0

I have this scrollview in which I want to inflate a 1920x1080 background image. If I add it statically via xml, my app lags in animations and general responsiveness. I checked StackOverflow and it's something that is expected to happen because images are uncompressed in memory.

Now, in the same thread linked above, a suggested answer is to use a setBackground function which uses BitmapFactory to dynamically resize and set the image as follows:

fun setBackground(context: Context, view: View, drawableId: Int) {
    var bitmap = BitmapFactory.decodeResource(context.getResources(), drawableId)
    val width = Resources.getSystem().getDisplayMetrics().widthPixels
    val height = Resources.getSystem().getDisplayMetrics().heightPixels
    bitmap = Bitmap.createScaledBitmap(bitmap, width, height, true)
    val bitmapDrawable = BitmapDrawable(context.getResources(), bitmap)
    view.background = bitmapDrawable
}

However, as can be seen, this image requires the drawable's ID, which I haven't been able to get. I checked similar questions and found conflicting answers with some saying that it's not possible to get drawable IDs, and other's suggesting some ImageView-related methods that aren't related to my problem (inflating a scrollview). So, can anybody please name a method in Java/Kotlin that allows me to get my drawable image's ID? If that's not possible, can you please suggest another workaround?

Usama Jawad
  • 69
  • 1
  • 12

1 Answers1

0

Have you tried:

val resID = resources.getIdentifier("drawable_name", "drawable", packageName)