【问题标题】:Android imageview performance, 1 or 3 images in layout?Android imageview 性能,布局中的 1 或 3 个图像?
【发布时间】:2021-05-26 21:27:58
【问题描述】:

我想知道在渲染和内存消耗方面什么更有效?

让我们假设总是可以看到一张图片。 在布局中包含 3 张图片并更改它们的可见性,或者以编程方式更改 1 张图片的 imageResource 更有效?

这是一个例子:

案例#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
}

案例#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)
}

在渲染和内存消耗方面什么更有效?

【问题讨论】:

    标签: android performance memory imageview rendering


    【解决方案1】:

    显然情况 2 更有效。 如果您将图像转换为 webp,那就更好了。

    【讨论】:

    • 太好了,不知道 Android 支持 webp。像 ImageKit 这样的 CDN Web 服务也很有帮助。我会把问题留在这里,也许有人会做一些测量。我的观点是,情况 1 对处理器更好,但对内存更差,情况 2 对内存更好,但对处理器更差。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-02
    • 1970-01-01
    • 2016-08-08
    相关资源
    最近更新 更多