【发布时间】:2020-03-23 14:48:06
【问题描述】:
我的活动结构如下:
使用适配器和 CardView 布局填充的回收视图。
CardView 由一个约束布局组成,其中包含一个图像和两个文本框。
图像设置为从 URL 异步加载的可绘制对象。
这是一些代码:
图片加载:
try {
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
Bitmap bitmap = ((BitmapDrawable) d).getBitmap();
Drawable temp = new BitmapDrawable(Bitmap.createScaledBitmap(bitmap, 300, 300, true));
return temp;
}
catch (Exception e) {
e.printStackTrace();
return null;
}
This is called later in a different function where vh is the ViewHolder:
vh.imageView.setImageDrawable(d);
卡片视图 XML:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.cardview.widget.CardView
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="4dp"
app:cardElevation="4dp"
app:cardUseCompatPadding="true"
app:contentPadding="16dp" >
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/playlist_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/playlist_name"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:srcCompat="@mipmap/ic_launcher" />
etc...
RecyclerView XML:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".PlaylistChoose">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/playlist_view"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="1dp"
android:layout_marginEnd="1dp"
android:clickable="true"
android:focusable="auto"
android:foreground="?attr/selectableItemBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:listitem="@layout/custom_playlist_view" >
</androidx.recyclerview.widget.RecyclerView>
</androidx.constraintlayout.widget.ConstraintLayout>
目前,当我在虚拟设备上运行此代码时,图像看起来很好,但是当我在 Galaxy S7 Edge 上运行应用程序时,图像显得非常小。然后我尝试将Drawable temp = new BitmapDrawable(Bitmap.createScaledBitmap(bitmap, 300, 300, true)); 中的高度和宽度从 300 增加到大约 1400,然后应用程序在 S7 上运行良好,但虚拟设备现在显示的图像大得离谱。我有点困惑,因为图像位于设置为包装内容的约束布局内,并且两部手机的内容大小相同,所以我不明白为什么实际大小会有所不同......提前致谢!任何帮助表示赞赏:)
一些图片:
AVD 1400 : https://gyazo.com/1c4ab4ffb9266e191357dd3e0dcd2f5c
S7 1400:https://gyazo.com/fd87f094bf7f23933543369b206b794a
【问题讨论】:
-
硬编码图像像素大小不是一个好主意..只需在
dp中使用固定大小ImageView并使用一些库来有效地为您下载图像may be this one.. -
@ADM 我正在使用该库并加载图像,但我并不真正理解您在 dp 中使用固定尺寸的意思,因此它会以非常大的尺寸加载图像。您能否进一步详细说明您的意思?
标签: java android android-recyclerview android-imageview android-drawable