如何缩放图像而不模糊?
我正在尝试将一个小图像(例如 20x40 像素)缩放为任意大的图像。我通过设置 ImageView.scaleType 属性来进行缩放,但是当它确实缩放图像时,它会从一种原始像素颜色淡入下一种颜色,从而创建一个大的模糊版本的图像。
这是这个问题的一些背景。我正在尝试为 Android 创建一款老式 16 位风格游戏。我想将精灵/等存储为小文件,然后根据用户的屏幕尺寸将它们放大。这样,我就不必为每个精灵创建大量不同尺寸的不同图像。现在我正在研究概念验证,因此我正在尝试将一个微小的 20x40 图像文件缩放到整个屏幕的大小。
*编辑:我想出了如何使用 Reflog 推荐的方法。然而,它仍然会产生一些衰落,只是远小于默认算法。
*编辑2:明白了!我必须关闭抗锯齿功能
*edit3:这对我来说神秘地停止了工作,我发现了另一个需要禁用抖动/自动缩放的地方。添加了选项行。
@Override
protected void onDraw(Canvas canvas) {
Paint paint = new Paint();
paint.setDither(false);
paint.setAntiAlias(false);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inDither = false;
options.inScaled = false;
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.tinyimg, options);
canvas.drawBitmap(bitmap, null, new RectF(getLeft(), getTop(), getRight()/2, getBottom()/2), paint);
}
I'm trying to scale a tiny image (something like 20x40 pixels) to an arbitrarily large image. I'm doing the scaling by setting the ImageView.scaleType property, but while it does scale the image, it fades from one original pixel color to the next, creating a large blurry version of the image.
Here's some context for the question. I'm trying to create an old school 16-bit style game for Android. I want to store the sprites/etc as tiny files, and then scale them larger depending on the user's screen size. That way, I don't have to create a ton of different images at different sizes for each sprite. Right now I'm just working on a proof of concept, so I'm trying to scale a tiny 20x40 image file to the size of the entire screen.
*edit: I figured out how to use the method Reflog recommended. However, it still does some fading, just far less than the default algorithm.
*edit2: Got it! I had to turn anti-aliasing off
*edit3: This mysteriously stopped working for me, and I found another place where dithering/auto scaling needed to be disabled. Added the options lines.
@Override
protected void onDraw(Canvas canvas) {
Paint paint = new Paint();
paint.setDither(false);
paint.setAntiAlias(false);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inDither = false;
options.inScaled = false;
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.tinyimg, options);
canvas.drawBitmap(bitmap, null, new RectF(getLeft(), getTop(), getRight()/2, getBottom()/2), paint);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用:
使用NN缩放,这会减少模糊,但也会降低缩放的质量。
Use:
to use NN scaling, this will reduce blurring, but also will reduce the quality of the scaling.
也许这段代码可以帮助您:
此函数只是缩放颜色数组(如果您不需要额外的功能):
我希望这很有用,并且我很乐意感谢一些改进此代码的建议。
Maybe this code can help you:
This function just scale the color array (if you don't want the extra features):
I hope this is useful, and I gladly would appreciate some advice for improving this code.