I.Step
I.Step

Reputation: 779

Android imageview performance, 1 or 3 images in layout?

I would like to know what is more efficient in an aspect of rendering and memory consumption?

Let's assume that always will be visible one image. What is more efficient having 3 images in a layout and changing visibility of them, or changing imageResource programatically of 1 image?

Here is an example:

Case #1

<!-- View case 1 -->
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/img1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/img_1" />
    
    <ImageView
        android:id="@+id/img2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/img_2" />
    
    <ImageView
        android:id="@+id/img3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/img_3" />
</LinearLayout>

// Implementation Case #1
fun showImg2() {
    binding.img1.visibility = View.INVISIBLE
    binding.img2.visibility = View.VISIBLE
    binding.img3.visibility = View.INVISIBLE
}

Case #2

<!-- View case 2 -->
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/img1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/img_1" />
</LinearLayout>

// Implementation Case #2
fun showImg2() {
    binding.img.setImageResource(R.drawable.img_2)
}

What is more efficient in an aspect of rendering and memory consumption?

Upvotes: 0

Views: 98

Answers (1)

Muhammad Farhan Arif
Muhammad Farhan Arif

Reputation: 174

Obviously case 2 is more efficient. If you convert your images to webp it will be better than best.

Upvotes: 1

Related Questions