在Android上使用Glide从网络加载图片时,图片太小

5
我正在尝试从网络下载一张图片,并使用scaleType="centerInside"选项在ImageView中显示它,使用Glide。但是由于某种原因,从网络下载的图片在屏幕上看起来比从资源中放入ImageView时要小得多。
例如:两个图片都可以在这里找到。我认为即使是从资源中设置的图片,在与我在笔记本电脑上看到的相比时,也比它们实际上能够达到的大小要小。我知道这与屏幕密度有关,但我该如何使这些图像具有“用户友好型大小”,例如稍微大一点?
即使是600x250像素大小的不同的图像(ImageView的layout_height和layout_width设置为“wrap_content”)在手机上也非常小。
来自Activity的代码:
public class DisplayImagesActivity extends AppCompatActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.display_image_activity);
        setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
        setTitle("Hello StackOverflow!");

        ImageView top_left = (ImageView) findViewById(R.id.top_left);
        ImageView top_right = (ImageView) findViewById(R.id.top_right);
        ImageView bottom_left = (ImageView) findViewById(R.id.bottom_left);
        ImageView bottom_right = (ImageView) findViewById(R.id.bottom_right);

        String[] urls = new String[] {
            "http://imgur.com/6jMOdg0.png",
            "http://imgur.com/AhIziYr.png"
        };

        top_left.setImageResource(R.drawable.top_left);
        top_right.setImageResource(R.drawable.top_right);
        Glide.with(this)
             .load(urls[0])
             .signature(new StringSignature(new Date().toString()))
             .into(bottom_left);
        Glide.with(this)
             .load(urls[1])
             .signature(new StringSignature(new Date().toString()))
             .into(bottom_right);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                this.finish();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}

display_image_activity.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/match_parent"
    android:orientation="vertical">

    <include layout="@layout/_toolbar" />

    <ScrollView
        style="@style/match_parent">
        <RelativeLayout
            style="@style/match_parent"
            android:padding="16dp">

            <TextView
                style="@style/wrap_content"
                android:id="@+id/text_resources"
                android:layout_marginBottom="10dp"
                android:text="From Resources"/>

            <ImageView
                android:id="@+id/top_left"
                android:background="@color/Linen"
                android:layout_width="150dp"
                android:layout_height="120dp"
                android:layout_marginBottom="20dp"
                android:layout_below="@id/text_resources"
                android:scaleType="centerInside"/>

            <ImageView
                android:id="@+id/top_right"
                android:background="@color/Linen"
                android:layout_width="150dp"
                android:layout_height="120dp"
                android:layout_toRightOf="@id/top_left"
                android:layout_toEndOf="@id/top_left"
                android:layout_below="@id/text_resources"
                android:layout_marginLeft="20dp"
                android:layout_marginStart="20dp"
                android:scaleType="centerInside"/>

            <TextView
                style="@style/wrap_content"
                android:id="@+id/text_network"
                android:layout_below="@id/top_left"
                android:layout_marginBottom="10dp"
                android:text="From Network"/>

            <ImageView
                android:id="@+id/bottom_left"
                android:background="@color/Linen"
                android:layout_width="150dp"
                android:layout_height="120dp"
                android:layout_below="@id/text_network"
                android:scaleType="centerInside" />

            <ImageView
                android:id="@+id/bottom_right"
                android:background="@color/Linen"
                android:layout_width="150dp"
                android:layout_height="120dp"
                android:layout_toRightOf="@id/bottom_left"
                android:layout_toEndOf="@id/bottom_left"
                android:layout_below="@id/text_network"
                android:layout_marginLeft="20dp"
                android:layout_marginStart="20dp"
                android:scaleType="centerInside" />

        </RelativeLayout>
    </ScrollView>
</LinearLayout>

你有什么解决方法吗?我这里也遇到了同样的问题。 - Annie Aye Myat
4个回答

5
我遇到了同样的问题。Glide试图解释我的应用程序需要什么,并相应地转换图像,导致某些地方的图像过小。在我的情况下,ImageViews使用adjustViewBounds="true"和MaxWidth / Height会导致问题。
虽然我不是Glide专家,但我找到了一个适合我的快速修复方法。
我只需添加一个.dontTransform()方法调用即可,对我来说这是可以的,因为我使用的是已经预先缩放的缩略图。
GlideApp.with(context).load(fireStorage).dontTransform().into(imgView);

(使用占位符可能也有帮助,但对我来说这是最简单的方法。)

0

如果需要,您的 ImageView 可以是固定大小的,但是它们应该使用 match_parent / wrap_content 来实现灵活性。

我不确定Glide具体做了什么,但是看起来网络图片的分辨率比资源中的小。 android:scaleType =“centerInside”会使图像缩小,直到图像的两个维度都适合于 ImageView 并且其纵横比保持不变的行为。如果您希望图像扩展以适合 ImageView ,则可能需要改用 android:scaleType =“fitCenter”。如果您决定使尺寸灵活,则还可以根据所需行为将 android:adjustViewBounds 设置为true / false。

这里的 scaleType 文档非常有用: https://developer.android.com/reference/android/widget/ImageView.ScaleType.html https://developer.android.com/reference/android/graphics/Matrix.ScaleToFit.html#CENTER


来自资源和网络的图像是完全相同的,我不想使用"fitCenter",因为对于小图片来说这并不起作用(它们会被拉伸到ImageView的全尺寸)。我仍然想使用"centerInside",但问题是无论图像的大小如何,当放置在ImageView中时,它都显示得太小了。我很难相信在第二个屏幕(横屏)中,一个600像素宽的图像应该显示得如此之小。 - user2628342
在具有QHD屏幕的手机上,分辨率为2560x1440。在横向模式下,600x250像素的图像宽度不到屏幕的四分之一,高度不到屏幕的五分之一。因此,包装内容可以使ImageView与图像的大小匹配。如果考虑屏幕的高分辨率,该图像看起来是一个完全合理的大小(至少对我来说是这样)。 - Uwais A
嗯。你的解释听起来是正确的。但如果手机分辨率不断变大,同样的图像就会在屏幕上变得越来越小。这听起来不对。也许可以通过屏幕密度的方式“放大”给定图像的尺寸?不确定,这该怎么实现。 - user2628342
这就是Android手机上“dp”单位的用途。它代表“密度无关像素”https://developer.android.com/guide/practices/screens_support.html#overview 这是一个有关dp的有用链接。尽管您使用dp来正确缩放图像视图,但您正在使图像基于其像素进行缩放,这似乎不是您想要做的。您可以使用LinearLayout指定不同颜色的背景,并在其中放置一个居中的正确大小的ImageView,然后使用fitCenter。 - Uwais A

0

这段代码节省了我的时间。它对我很有效!

        //Get actual width and height of image
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        Bitmap bitmap = null;
        try {
            URL url = new URL(imgUrl);
            bitmap = BitmapFactory.decodeStream((InputStream)url.getContent());
        } catch (IOException e) {
            Timber.e("Image Loading Error %s", e.getLocalizedMessage());
        }

        if (bitmap != null) {
            final float scale = resources.getDisplayMetrics().density;
            final int dpWidthInPx = (int) (bitmap.getWidth() * scale + 0.5f);
            final int dpHeightInPx = (int) (bitmap.getHeight() * scale + 0.5f);

            //Set result width and height to image
            GlideApp.with(imgAns.getContext())
                    .load(imgUrl)
                    .override(dpWidthInPx, dpHeightInPx)
                    .into(imgAns);
        }

0

您的图像大小不能超过您定义的大小:

android:layout_width="150dp"
android:layout_height="120dp"

尝试

android:layout_width="match_parent"
android:layout_height="wrap_content"

您的建议并没有改变任何东西,也不应该。我定义的大小是我想要的 ImageView 的大小(图像周围的不同背景是为 ImageView 设计的)。里面的图像应该更大。 - user2628342

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接