如何在 Android Java 中关闭 Imageview 的抗锯齿
How to turn off Anti-Aliasing of an Imageview in Android Java
我在网上搜索了很多时间,做了一个这样的代码:
import android.app.Activity;
import android.os.Bundle;
import android.graphics.DrawFilter;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.View;
import android.graphics.drawable.DrawableWrapper;
import android.graphics.drawable.Drawable;
import android.graphics.*;
public class math_math extends DrawableWrapper {
private final DrawFilter DRAW_FILTER =
new PaintFlagsDrawFilter(Paint.FILTER_BITMAP_FLAG, 0);
public math_math(Drawable wrapped) {
super(wrapped);
wrapped.setFilterBitmap(false);
}
@Override
public void draw(Canvas canvas) {
super.draw(canvas);
}
}
在 MainActivity onCreate i 运行 关闭抗锯齿的代码:
imageview1.setImageDrawable(new math_math(getDrawable(R.drawable.auto)));
顺便说一下,auto 是我想在关闭抗锯齿后看到的图片
但是有个大问题
该应用对图像进行了此操作:
https://i.stack.imgur.com/8IlB6.png
但是图像应该是这样的:
https://i.stack.imgur.com/UnUzg.png
请有人帮忙,我的代码有什么问题。
谢谢
如果要在较大的视图中显示小图像而不触发位图自动缩放,则必须进行手动缩放:
final Bitmap cSmallBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.auto);
final Bitmap cBigBitmap = Bitmap.createBitmap(600, 600, Bitmap.Config.ARGB_8888);
final Canvas cCanvas = new Canvas(cBigBitmap);
final Paint cPaint = new Paint();
cPaint.setAntiAlias(false);
cPaint.setDither(false);
cPaint.setFilterBitmap(false);
cCanvas.drawBitmap(cSmallBitmap, new Rect(0, 0, cSmallBitmap.getWidth(), cSmallBitmap.getHeight()), new Rect(0, 0, cBigBitmap.getWidth(), cBigBitmap.getHeight()), cPaint);
cImageView.setImageBitmap(cBigBitmap);
我在网上搜索了很多时间,做了一个这样的代码:
import android.app.Activity;
import android.os.Bundle;
import android.graphics.DrawFilter;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.View;
import android.graphics.drawable.DrawableWrapper;
import android.graphics.drawable.Drawable;
import android.graphics.*;
public class math_math extends DrawableWrapper {
private final DrawFilter DRAW_FILTER =
new PaintFlagsDrawFilter(Paint.FILTER_BITMAP_FLAG, 0);
public math_math(Drawable wrapped) {
super(wrapped);
wrapped.setFilterBitmap(false);
}
@Override
public void draw(Canvas canvas) {
super.draw(canvas);
}
}
在 MainActivity onCreate i 运行 关闭抗锯齿的代码:
imageview1.setImageDrawable(new math_math(getDrawable(R.drawable.auto)));
顺便说一下,auto 是我想在关闭抗锯齿后看到的图片
但是有个大问题
该应用对图像进行了此操作: https://i.stack.imgur.com/8IlB6.png
但是图像应该是这样的: https://i.stack.imgur.com/UnUzg.png
请有人帮忙,我的代码有什么问题。
谢谢
如果要在较大的视图中显示小图像而不触发位图自动缩放,则必须进行手动缩放:
final Bitmap cSmallBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.auto);
final Bitmap cBigBitmap = Bitmap.createBitmap(600, 600, Bitmap.Config.ARGB_8888);
final Canvas cCanvas = new Canvas(cBigBitmap);
final Paint cPaint = new Paint();
cPaint.setAntiAlias(false);
cPaint.setDither(false);
cPaint.setFilterBitmap(false);
cCanvas.drawBitmap(cSmallBitmap, new Rect(0, 0, cSmallBitmap.getWidth(), cSmallBitmap.getHeight()), new Rect(0, 0, cBigBitmap.getWidth(), cBigBitmap.getHeight()), cPaint);
cImageView.setImageBitmap(cBigBitmap);