【问题标题】:How to display a ImageView pixelated without interpolating low res files?如何在不插入低分辨率文件的情况下显示像素化的 ImageView?
【发布时间】:2018-10-01 18:18:26
【问题描述】:

我想加载一个带有低分辨率 png 文件的 ImageView。我想显示像素,但 ImageView 显示它与插值模糊。如何避免这种情况?我想显示所有像素。

这是我显示 ImageView 的方式:

Drawable dr = getResources().getDrawable(R.drawable.cloud);
Bitmap cloud1Bitmap = ((BitmapDrawable) dr).getBitmap();
float cloud1Ratio = (float)cloud1Bitmap.getHeight()/cloud1Bitmap.getWidth();

cloud1ImageView = new ImageView(this);
cloud1ImageView.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
cloud1ImageView.setImageBitmap(Bitmap.createScaledBitmap(cloud1Bitmap, cloud1Width, (int) (cloud1Width*cloud1Ratio), true));
main.addView(cloud1ImageView);

编辑:

用那个功能试过了,没用。也许是因为我正在缩放图像?

    view.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

    width = (int) (sw/3);
    Drawable dr = context.getResources().getDrawable(R.drawable.ufo);
    BitmapDrawable bd = ((BitmapDrawable) dr);
    Bitmap bitmap = bd.getBitmap();
    float ratio = (float)bitmap.getHeight()/bitmap.getWidth();
    height = (int)(width*ratio);

    BitmapDrawable drawable = new BitmapDrawable(context.getResources(), Bitmap.createScaledBitmap(bitmap, width, height, false));
    drawable.setFilterBitmap(false);        
    view.setImageDrawable(drawable);

在左侧,您可以看到图像在游戏中的显示方式,而在右侧,您可以看到原始 png 中的图像:

【问题讨论】:

  • 你能展示你想要的图像效果吗?
  • @pskink 这是一个游戏,我需要缩放位图以准确使用内存中所需的尺寸
  • 你能添加一个演示你的意思吗?
  • 好的,我试过了,但是应用了相同的模糊过滤器,请测试它:view.setLayoutParams(new RelativeLayout.LayoutParams((int) (sw/3f), RelativeLayout.LayoutParams.WRAP_CONTENT) ); view.setScaleType(ImageView.ScaleType.FIT_XY); view.setAdjustViewBounds(true);
  • 一张超小不明飞行物图片,无法欣赏像素,超小,32x24 什么的。我需要将其缩放到屏幕宽度的 1/3 以识别像素

标签: android imageview android-imageview pixel android-image


【解决方案1】:

正如@pskink 所说,您可以通过调用setFilterBitmap(false) 来停止模糊/插值。对于这些示例,我在 240dp ImageView 中使用 24px 图像:

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_android);
BitmapDrawable drawable = new BitmapDrawable(getResources(), bitmap);

ImageView image = findViewById(R.id.image);
image.setImageDrawable(drawable);

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_android);
BitmapDrawable drawable = new BitmapDrawable(getResources(), bitmap);
drawable.setFilterBitmap(false);

ImageView image = findViewById(R.id.image);
image.setImageDrawable(drawable);

【讨论】:

  • 但是如果您需要将图像缩放到我需要的所需大小会发生什么。您可以在我的示例代码中看到它。我尝试添加该功能,但没有任何反应。也许是因为规模?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-12-19
  • 2012-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-17
相关资源
最近更新 更多