简体   繁体   中英

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:

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

But the image should look like this: 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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