Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
526 views
in Technique[技术] by (71.8m points)

android - Bitmap not drawn anti-aliased

I have a custom View that always draws a Bitmap at a certain rotation. I overwrite the onDraw method, rotate the Canvas and draw the bitmap with an anti-aliased Paint.

public RotatedImageView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    someBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.placeholder);
}

@Override
protected void onDraw(Canvas canvas) {

    // Save and rotate canvas
    canvas.save();
    canvas.rotate(3F, getWidth() / 2, getHeight());

    // Draw the icon
    Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
    canvas.drawBitmap(someBitmap, 0, 0, p);
    canvas.drawRoundRect(new RectF(75, 50, 225, 200), 10, 10, p);

    // All done; restore canvas
    canvas.restore();
}

However, I always get jagged edges on the Bitmap. Note that the roudned rectangle gets nicely anti-aliased edges. Also, when I apply p.setFilterBitmap(true); this works (the bitmap surface gets filtered/smoothed) correctly. Am I missing something?

No anti-aliasing on the photo

Here's a minimal Android project with isolated example of one screen that shows the View that draws the non-anti-aliased Bitmap, loaded from a resource: https://bitbucket.org/erickok/antialiastest

UPDATE: I have also tried the following:

    Paint p = new Paint();
    p.setAntiAlias(true);
    p.setFilterBitmap(true);
    p.setDither(true);
    canvas.drawBitmap(someBitmap, 0, 0, p);

But this doesn't help as setFilterBitmap filters the surface; it does not anti-alias the edges. Also, setAntiAlias does the same as directly setting the flag in the Paint constructor. If in doubt, please try my minimal test project. Thanks so much for any help!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use setFilterBitmap(true).

paint_object.setFilterBitmap(true);

it works for me too.. I got it from this question

Drawing rotated bitmap with anti alias

set both the AntiAlias flag and FilterBitmap flag to true, they together shall make bitmap's edges smooth, I have tested it on Samsung Galaxy Ace , android 2.2 ...

My Testing Code is

Paint p = new Paint();
p.setAntiAlias(true);
p.setFilterBitmap(true);
canvas.drawBitmap(someBitmap, new Matrix(), p);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...