gpt4 book ai didi

android - 如何在启用抗锯齿的情况下旋转可绘制对象

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:04:34 27 4
gpt4 key购买 nike

我需要将 ImageView 旋转几度。我通过子类化 ImageView 和重载 onDraw()

来做到这一点
@Override
protected void onDraw(Canvas canvas) {
canvas.save();
canvas.scale(0.92f,0.92f);
canvas.translate(14, 0);
canvas.rotate(1,0,0);
super.onDraw(canvas);
canvas.restore();
}

问题是生成的图像显示出一堆锯齿。

http://img.skitch.com/20100608-gmdy4sexsm1c71di9rgiktdjhu.png

如何对需要旋转以消除锯齿的 ImageView 进行抗锯齿处理?有更好的方法吗?

最佳答案

如果您知道您的 Drawable 是一个 BitmapDrawable,您可以在位图的 Paint 中使用抗锯齿来执行如下操作:

/**
* Not as full featured as ImageView.onDraw(). Does not handle
* drawables other than BitmapDrawable, crop to padding, or other adjustments.
*/
@Override
protected void onDraw(Canvas canvas) {
final Drawable d = getDrawable();

if( d!=null && d instanceof BitmapDrawable && ((BitmapDrawable)d).getBitmap()!=null ) {
final Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
final int paddingLeft = getPaddingLeft();
final int paddingTop = getPaddingTop();

canvas.save();

// do my rotation and other adjustments
canvas.scale(0.92f,0.92f);
canvas.rotate(1,0,0);

if( paddingLeft!=0 )
canvas.translate(paddingLeft,0);

if( paddingTop!=0 )
canvas.translate(0,paddingTop);

canvas.drawBitmap( ((BitmapDrawable)d).getBitmap(),0,0,p );
canvas.restore();
}
}

关于android - 如何在启用抗锯齿的情况下旋转可绘制对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3002094/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com