简体   繁体   English

如何在 Android Java 中关闭 Imageview 的抗锯齿功能

[英]How to turn off Anti-Aliasing of an Imageview in Android Java

I searched the net a lot of time, to make a code like this:我在网上搜索了很多时间,做了这样的代码:


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);
        
    }
}

In the MainActivity onCreate i run this code to turn off anti aliasing:在 MainActivity onCreate 我运行此代码以关闭抗锯齿:

imageview1.setImageDrawable(new math_math(getDrawable(R.drawable.auto)));

By the way, the auto is the picture, that i want to see with turned off anti aliasing顺便说一句,自动是图片,我想在关闭抗锯齿的情况下看到

BUT there is a big problem但是有一个大问题

The app did this with the image: https://i.stack.imgur.com/8IlB6.png该应用程序使用图像执行此操作: https://i.stack.imgur.com/8IlB6.png

But the image should look like this: https://i.stack.imgur.com/UnUzg.png但图像应如下所示: https://i.stack.imgur.com/UnUzg.png

Please somebody help, what's the problem in my code.请有人帮忙,我的代码有什么问题。

Thanks谢谢

You have to do MANUAL scaling if you want to display a small image in a bigger View without trigger the Bitmap auto-scaling:如果要在更大的视图中显示小图像而不触发 Bitmap 自动缩放,则必须执行手动缩放:

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM