java - Android 上 Web 上相同图像的不同尺寸

标签 java android drawable

我有一个像 test.png 这样的图像。

我需要从网络上购买该图像,并且在另一个位置我需要从可绘制对象中选择相同的图像。

问题是我在网络和可绘制中具有相同的 png。当我显示可绘制对象中的图像时,它以正确的尺寸显示,但是当我从网络上获取它时,它看起来更小。 只是为了说清楚,这只是我的问题的一个例子......我实际上并没有使用相同的图像,它们是不同的图像但具有相同的大小。为了验证,我在网络和可绘制上上传了相同的图像,发现相同的图像以不同的尺寸显示。 我需要做什么才能使两者看起来像可绘制模式?

我已经验证了这一点:

当我使用以下方式获取保存在 SD 卡中的图像时:

File sdCard = Environment.getExternalStorageDirectory();
Fle directory = new File(sdCard.getAbsolutePath());
File file = new File(directory, "teste.png");
File InputStream streamIn;
Bitmap bitmap = Bitmapfactory.decodeStream(streamin);
ImageView image = new ImageView(c);
image.setImageBitmap(bitmap);

图像看起来比这样大:

Drawable img = Drawable.createFromPath(new File(Environment.getExternalStorageDirectory(), "teste.png").getAbsolutePath());
ImageView image = new ImageView(c);
image.setImageDrawable(img);

但在第一种模式下,它看起来比我使用 getResouces.getDrawable 直接从可绘制资源获取图像时要小...

最佳答案

我编写了这段代码来重现错误:

LinearLayout ll = (LinearLayout) findViewById(R.id.root);

try {
    ImageView image = new ImageView(this);
    Bitmap bitmap = BitmapFactory.decodeStream(getAssets().open("tag-logo-android.png"));
    image.setImageBitmap(bitmap);
    ll.addView(image);

    image = new ImageView(this);
    Drawable drawable = Drawable.createFromStream(new URL("http://cdn.sstatic.net/stackoverflow/img/tag-logo-android.png").openStream(), null);
    image.setImageDrawable(drawable);
    ll.addView(image);
} catch (MalformedURLException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

问题不在于图像的来源。问题在于 Drawable 忽略了屏幕密度。此代码有效:

LinearLayout ll = (LinearLayout) findViewById(R.id.root);

try {
    ImageView image = new ImageView(this);
    Bitmap bitmap = BitmapFactory.decodeStream(getAssets().open("tag-logo-android.png"));
    image.setImageBitmap(bitmap);
    ll.addView(image);

    image = new ImageView(this);
    bitmap = BitmapFactory.decodeStream(new URL("http://cdn.sstatic.net/stackoverflow/img/tag-logo-android.png").openStream());
    image.setImageBitmap(bitmap);
    ll.addView(image);
} catch (MalformedURLException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

根据您的情况,您可以使用:

bitmap = BitmapFactory.decodeFile(new File(Environment.getExternalStorageDirectory(), "test.png"))

关于java - Android 上 Web 上相同图像的不同尺寸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12164390/

相关文章:

java - 正则表达式检查 Android 中的多个条件

Java观察者和可观察者

Java 字符串比较

android - 如何在可扩展 ListView 中使 child 可点击

java - 调整可绘制问题的大小(未传递引用?)

android - 调整图像大小以放入 ldpi、hdpi 屏幕的正确方法

java - 我无法在我的 debian 6 VPS 上安装 Java

android - ffmpeg - 在 5 个视频之间进行淡入淡出

android - 如果不是Rotation,请使用ViewModel

android - 如何将 hdpi、mdpi、ldpi 和 xhdpi 文件保存在 Android Assets 文件夹中?