如何避免 Android 应用程序中 Imageview 出现像素化?

问题描述 投票:0回答:1

我正在编写一个音乐播放器应用程序,想要显示每首歌曲的专辑封面。有些专辑封面看起来不错,而其他封面则像素化。这个问题似乎没有任何模式。我检查了原始文件,它们没有像素化。例如,Eric Clapton 的 .png 图片尺寸为 3000 x 3000,在我的笔记本电脑上看起来不错,但是当加载到我的应用程序中的 Imageview 中时,它看起来像这样:enter image description here

图像不在 Android Studio 项目的资源部分中 - 它们位于混合到 iTunes 媒体文件夹中的文件夹中,因此文件的位置在我的歌曲数据库的 artLocation 字段中定义。

以下是如何在应用程序中定义 Imageview:

        <ImageView
            android:id="@+id/albumArt"
            android:layout_width="280dp"
            android:layout_height="280dp"
            android:contentDescription="@string/album_art"
            android:scaleType="fitCenter"
            android:visibility="visible"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/albumView" />

这是我将图像添加到 Imageview 的方法:

            if (!TextUtils.isEmpty(song.getArtLocation())) {
                Bitmap bitmap = BitmapFactory.decodeFile(song.getArtLocation());
                //bitmap = Util.getResizedBitmap(bitmap, 2000, 2000);
                Bitmap newbitmap = Bitmap.createScaledBitmap(bitmap,800,800,false);
                albumArtView.setImageBitmap(newbitmap);
            } else {
                albumArtView.setImageResource(R.drawable.vinylrecord);
            }

我正在运行 Android 10 的 Galaxy S9 上进行测试。我在运行 Android 11 的 Lenova 平板电脑上也发现了该问题。我的项目中的目标 api 是 31。

我尝试将位图直接设置到albumArtView中,并且还尝试使用两种不同的方法来缩放图像(如您在代码中看到的)。注释掉的 getResizedBitmap 是我在网上找到的一些看起来很有前途的代码,但它似乎对像素化没有任何影响。我在 Bitmap.createScaledBitmap 和 getResizedBitmap 方法中尝试了不同的分辨率,但似乎没有太大区别。

这不是每个专辑封面文件,只是其中的一些。有些看起来不错。我认为应用程序中的图像应该与直接在我的电脑上查看的图像相同。有人对问题所在有任何想法吗?如果我要缩放图像,应该如何设置?

android imageview
1个回答
0
投票

抱歉 - 我手机上的示例图像以同样的方式进行像素化。我正在笔记本电脑上的 itunes 文件夹中查看图像 - 它没有像素化。不知何故,当我将手机同步到笔记本电脑时,它一定已损坏。不过,毕可索是一个很好的发现——学到了一些东西!

© www.soinside.com 2019 - 2024. All rights reserved.