我创建了一个带有背景旋转的旋转 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.
关于android - 带背景的旋转图像抗锯齿?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15098142/