使用 piccasso 调整 imageview 的大小使我的应用程序无法适应其他设备尺寸

resizing imageview with piccasso makes my app not adaptive to other device sizes

我有一个布局文件,代表 recyclerView 中的一个行项目。该行项目包含一个图像,该图像在转到 onBindViewHolder 方法时通过 Piccaso 库加载,并将图像的宽度和高度调整为某个 value.The 问题是,当我将值写为时,假设 -

.resize(250, 250)

这意味着无论设备尺寸如何,它始终为 250 像素 - 对于 Pixel 1 来说很棒,但对于 Pixel 3 XL 来说就很小了。

如何使piccaso的缩放功能动态化,以便根据当前屏幕大小进行调整?

这是我的行项目 XML -


<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView 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"
    android:layout_margin="10dp"
    android:background="#b0b3b7"
    android:elevation="@dimen/hero_row_elevation"
    android:tag="0"
    app:cardCornerRadius="@dimen/hero_row_corner_radius">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#b0b3b7"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <de.hdodenhof.circleimageview.CircleImageView
            android:id="@+id/heroImage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scaleType="centerCrop"
            android:src="@mipmap/ic_launcher"
            app:layout_constraintEnd_toStartOf="@id/heroTitle"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"/>

        <ProgressBar
            android:id="@+id/adapterProgressBar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scaleType="centerCrop"
            android:src="@mipmap/ic_launcher"
            app:layout_constraintEnd_toStartOf="@id/heroTitle"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"/>

        <TextView
            android:id="@+id/heroTitle"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="32dp"
            android:layout_marginTop="5dp"
            android:autoSizeMaxTextSize="25dp"
            android:autoSizeMinTextSize="15dp"
            android:text="@string/hero_row_hero_title"
            android:textColor="#000"
            android:textSize="@dimen/hero_row_title_text_size"
            android:textStyle="bold"
            app:layout_constraintBottom_toTopOf="@+id/heroAbilities"
            app:layout_constraintEnd_toStartOf="@+id/heartImageView"
            app:layout_constraintStart_toEndOf="@+id/heroImage"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/heroAbilities"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="32dp"
            android:autoSizeMaxTextSize="25dp"
            android:autoSizeMinTextSize="15dp"
            android:breakStrategy="simple"
            android:hyphenationFrequency="none"
            android:text="@string/hero_row_abilities_text"
            android:textColor="#444"
            android:textSize="@dimen/hero_row_abilities_text"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/heroImage"
            app:layout_constraintTop_toBottomOf="@+id/heroTitle" />

        <ImageView
            android:id="@+id/heartImageView"
            android:layout_width="@dimen/hero_row_heart_size"
            android:layout_height="@dimen/hero_row_heart_size"
            android:layout_marginTop="9dp"
            android:layout_marginEnd="24dp"
            android:src="@drawable/empty_heart"
            android:tag="1"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/heroTitle"
            app:layout_constraintTop_toTopOf="parent" />

    </android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>

这是我的 onBindViewHolder();方法 -


@Override
    public void onBindViewHolder(@NonNull final HeroesViewHolder holder, int position) {

        final Hero currentHero = heroList.get(position);
        String str = String.join(",", currentHero.abilities);

        holder.heroTitle.setText(currentHero.title);
        holder.heroAbilities.setText(str);
        Picasso.get()
                .load(currentHero.image)
                .resize(250, 250)
                .error(R.drawable.ic_launcher_foreground)
                .into(holder.heroesImage, new Callback() {
                    @Override
                    public void onSuccess() {
                        holder.progressBar.setVisibility(View.GONE);
                    }

                    @Override
                    public void onError(Exception e) {

                    }
                });
        if (!currentHero.isFavorite()){
            Picasso.get()
                    .load(R.drawable.empty_heart)
                    .into(holder.heartImageview);
        } else {
            Picasso.get()
                    .load(R.drawable.full_heart)
                    .into(holder.heartImageview);
        }

        setFadeAnimation(holder.itemView);
    }

每当您在 Picasso 中使用 resize() 函数时,他们都会在其文档中明确说明以下行 here:

resizes the image to these dimensions (in pixel). does not respect aspect ratio

"dp"Density Independent Pixels 出现在 Android 中是有原因的。如果您希望图像在所有设备上统一,请在 "dp" 中的布局文件中设置适当的 layout_widthlayout_height,并且它在所有设备上都是统一的,无论他们的 size/screen 密度。