旋转的ImageView边缘不光滑(锯齿状)

时间:2019-04-04 01:17:08

标签: android rotation imageview antialiasing

样本NG ImageViewNG Rotated ImageView

在iOS中,我可以通过view.layer.allowsEdgeAntialiasing = true轻松解决这个锯齿状的边缘问题。在Android中解决此问题的最佳方法是什么?

我已经尝试过的内容:
我参考了这篇帖子Bitmap not drawn anti-aliased并设置了android:layerType="software",但结果仍然不正确。

<ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layerType="software"
        android:rotation="5"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/pic" />

预期结果:平滑的边缘(抗锯齿)
实际结果:边缘不光滑(锯齿状)

1 个答案:

答案 0 :(得分:0)

因此,经过一段时间的尝试,这是我可以达到的最佳效果:

1)测试输出: NG vs OK

2)NG输出源

仅* xml(在ConstraintLayout下)

<ImageView android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           app:layout_constraintTop_toTopOf="parent"
           app:layout_constraintStart_toStartOf="parent"
           app:layout_constraintEnd_toEndOf="parent"
           app:layout_constraintBottom_toBottomOf="parent"
           android:id="@+id/test3"
           app:srcCompat="@drawable/sampleImg"
           android:rotation="5"/>

3)确定输出源

* xml(在ConstraintLayout下)

<test.ntfry.imageview_rotate_antialias.MyImageView
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           app:layout_constraintTop_toTopOf="parent"                                   
           app:layout_constraintStart_toStartOf="parent"                                    
           app:layout_constraintEnd_toEndOf="parent"
           app:layout_constraintBottom_toBottomOf="parent"
           android:id="@+id/test2"/>

*自定义ImageView类

class MyImageView : AppCompatImageView {

    private val mPaint = Paint(Paint.ANTI_ALIAS_FLAG)
    private val mMatrix = Matrix()
    private var mRotatedBitmap: Bitmap? = null
    private val mRotation = 5F

    constructor(context: Context) : super(context, null) {
        init()
    }
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        init()
    }
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        init()
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        setMeasuredDimension(mRotatedBitmap!!.width, mRotatedBitmap!!.height)
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        canvas?.drawBitmap(mRotatedBitmap!!, 0f, 0f, mPaint)
    }

    private fun init() {
        // https://stackoverflow.com/questions/14378573/bitmap-not-drawn-anti-aliased
        setLayerType(LAYER_TYPE_SOFTWARE, null)

        // get source image and set-apply rotation through matrix
        val sourceBitmap = BitmapFactory.decodeResource(resources, R.drawable.sampleImg)
        mMatrix.postRotate(mRotation)
        mRotatedBitmap = Bitmap.createBitmap(sourceBitmap, 0, 0, sourceBitmap.width, sourceBitmap.height, mMatrix, true)
    }
}

4)如果您有更好的解决方案,请务必发表评论/分享您的想法!谢谢^^