繁体   English   中英

图像视图在不同设备之间不缩放

[英]Image View Not Scaling Between Different Devices

我的活动结构如下:

一个用适配器和 CardView 布局填充的回收视图。

CardView 由包含一个图像和两个文本框的约束布局组成。

Image 设置为从 URL 异步加载的 drawable。

这是一些代码:

图片加载:

  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);

CardView 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));的高度和宽度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

AVD 300: https : //gyazo.com/d5c23c9372bc0c1838866452a24af06f

S7 300: https : //gyazo.com/a9084b77d3efdd87cea9e53344c7b169

我有点困惑,因为图像位于设置为包装内容的约束布局内,并且两部手机的内容大小相同

不同手机的内容可能相同,但因为在ImageView上使用wrap_content ,不同手机的屏幕尺寸不同,所以在所有设备上看起来都不一样。

怎么修:

使用ConstraintLayout你会想要做这样的事情:

将此用作您的图像视图:

  <ImageView
    android:id="@+id/imageView2"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintWidth_percent="0.25"
    app:layout_constraintHeight_percent="0.25"
    android:scaleType="fitXY"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/your_image" />

现在,这将是其母公司的宽度和高度正好25%。

如果需要,您可以更改图像的大小,只需更改:

   app:layout_constraintWidth_percent="0.25"
   app:layout_constraintHeight_percent="0.25"

尝试在 ImageView 中添加 scaleType 和调整视图边界

<ImageView
   android:id="@+id/playlist_image"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:scaleType="fitCenter"
   android:adjustViewBounds="true"
   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" />

你可以使用

  android:scaleType="fitXY"
     <ImageView
            android:id="@+id/playlist_image"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/playlist_name"
            android:scaleType="fitXY"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"

            app:srcCompat="@mipmap/ic_launcher" />

解决了! 这是由于@ADM 建议将 glide 库与@Tamir Abutbul 的 Scale 和 Constraint Percent 建议结合使用,我的图像现在可以正确加载:)

XML:

<ImageView
                android:scaleType="fitXY"
                app:layout_constraintHeight_percent="1"
                app:layout_constraintWidth_percent="0.25"

                android:id="@+id/playlist_image"
                android:layout_width="0dp"
                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" />

滑行代码:

 Glide.with(context).load(url).into(vh.imageView);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM