我已经能够使用Glide大大提高我的应用程序的性能,但这意味着我必须从布局xml中的ImageView中删除srcCompat行。
但是,这使设计变得更加困难,因为现在我在安排我的xml时无法在我的设计选项卡中引用任何内容。
有人有解决方案吗?无法在xml中查看图像的工作非常不方便。
例:
在使用Glide之前:
在我的activity_main.xml中:
<ImageView
android:id="@+id/image"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:adjustViewBounds="true"
app:layout_constraintEnd_toStartOf="@id/rightNarrowGuideline"
app:layout_constraintStart_toEndOf="@id/leftNarrowGuideline"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/money_coins" />
<Button
android:id="@+id/example"
style="@style/BigRedButton"
android:layout_width="0dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="50dp"
android:text="EXAMPLE"
app:layout_constraintEnd_toEndOf="@id/rightNarrowGuideline"
app:layout_constraintStart_toStartOf="@id/leftNarrowGuideline"
app:layout_constraintTop_toBottomOf="@+id/image" />
它看起来像在Design tab。
使用Glide后:
现在我在我的MainActivity.java中加载图像
ImageView mainImage = findViewById(R.id.image);
Glide.with(this).load(R.drawable.money_coins).into(mainImage);
我删除了这一行:
app:srcCompat="@drawable/money_coins"
来自我的activity_main.xml,所以我留下了这个:
<ImageView
android:id="@+id/image"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:adjustViewBounds="true"
app:layout_constraintEnd_toStartOf="@id/rightNarrowGuideline"
app:layout_constraintStart_toEndOf="@id/leftNarrowGuideline"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/example"
style="@style/BigRedButton"
android:layout_width="0dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="50dp"
android:text="EXAMPLE"
app:layout_constraintEnd_toEndOf="@id/rightNarrowGuideline"
app:layout_constraintStart_toStartOf="@id/leftNarrowGuideline"
app:layout_constraintTop_toBottomOf="@+id/image" />
现在我的设计选项卡看起来像this。
如果我理解正确,您希望在预览中看到您的图像。好吧,我想你可以设置tools:srcCompat="@drawable/money_coins"
,它基本上设置一个“占位符”图像进行预览。但是通过这样做,你需要在两个地方更新drawable。
Dunno确切地说,如果你也在改变你的图像,但是对于静态图像的使用,直接在布局中分配src应该是很好的性能。除非你的可绘图像像巨大的。如果是这种情况,您应该正确调整大小甚至更好,请使用矢量可绘制资源。我猜你的滑动缩减样本图像,因此加载速度更快。