Goo
Goo

Reputation: 1328

Anti aliasing on rotated image with background?

I created an rotated imageview with background rotation but the result is "aliased".

The usual way is to create a Paint with anti-alias flag then use it to draw bitmap. However, the background is draw by the view's draw(Canvas canvas) method, so i can't use a paint with canvas.drawBitmap method.

Here my code to rotate the imageview including the background :

@Override
    public void draw(Canvas canvas)
    {
        canvas.save();
        canvas.rotate(mAngle%360, canvas.getClipBounds().exactCenterX(), canvas.getClipBounds().exactCenterY());
        canvas.scale(scaleWidth, scaleHeight, canvas.getClipBounds().exactCenterX(), canvas.getClipBounds().exactCenterY());
        super.draw(canvas);

        canvas.restore();

}

and my onMeasure method which adapts the height and the width depending of the angle

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    int w = getDrawable().getIntrinsicWidth();
    int h = getDrawable().getIntrinsicHeight();
    double a = Math.toRadians(mAngle);

    int width = (int) (Math.abs(w * Math.cos(a)) + Math.abs(h * Math.sin(a)));
    int height = (int) (Math.abs(w * Math.sin(a)) + Math.abs(h * Math.cos(a)));

    scaleWidth = (width != 0) ? ((float) w) / width : 1;
    scaleHeight = (height != 0) ? ((float) h) / height : 1;

    setMeasuredDimension(width, height);
}

So, How can i "enable" anti aliasing on this rotation?

Upvotes: 1

Views: 1642

Answers (1)

riot
riot

Reputation: 556

Maybe use setDrawFilter on the canvas?

A DrawFilter subclass can be installed in a Canvas. When it is present, it can modify the paint that is used to draw (temporarily). With this, a filter can disable/enable antialiasing, or change the color for everything this is drawn.

edit: see here: https://stackoverflow.com/a/13191948/1954497

Upvotes: 1

Related Questions