android - 带背景的旋转图像抗锯齿?

标签 android android-imageview image-rotation

我创建了一个带有背景旋转的旋转 ImageView ,但结果是“混叠”。

通常的方法是创建一个带有反锯齿标志的Paint,然后用它来绘制位图。 但是,背景是由 View 的 draw(Canvas canvas) 方法绘制的,所以我不能使用 canvas.drawBitmap 方法进行绘制。

这里是我的代码,用于旋转包含背景的 ImageView :

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

}

和我的 onMeasure 方法,它根据角度调整高度和宽度

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

那么,我怎样才能在这个旋转中“启用”抗锯齿?

最佳答案

也许在 Canvas 上使用 setDrawFilter?

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.

编辑:看这里:https://stackoverflow.com/a/13191948/1954497

关于android - 带背景的旋转图像抗锯齿?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15098142/

相关文章:

android - 如何在android中相对于触摸中心以圆形方式旋转图像

android - 如何更新表值

android - 在 Subject 的 onNext 上触发 Observable 并分享结果

android - 如何在 memu 模拟器中运行 flutter

android - 单击它以全屏显示图像

android - 如何根据 EditText length() 测试更改默认的 ImageView 颜色?

java - 我怎样才能检测到所有在 Android 中被触摸过的 ImageView?

javascript - Canvas 变换旋转工件

objective-c - cocoa PDFView,setDocument,如何将文档旋转90度?

android - 在 Android 中自动捕获图像