【发布时间】: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