Android 画图 - PorterDuffXfermode SRC_IN 不工作

Android Paint - PorterDuffXfermode SRC_IN not working

我想按照这篇文章和图像使用 PorterDuffXfermode 实现圆形图像效果:porterDuff tutorial

所以我有目的地圈 is this

我有一个源图像 which is this

我创建了一个自定义图像视图,在 ondraw 中我打算像教程一样绘制一个圆形图像。所以我这样做的代码如下所示:

    class ImageCutomView : ImageView {
        constructor(p0: Context) : super(p0)
        constructor(p0: Context, p1: AttributeSet?) : super(p0, p1)
        constructor(p0: Context, p1: AttributeSet?, p2: Int) : super(p0, p1, p2)


        override fun onDraw(canvas: Canvas?) {
            super.onDraw(canvas)
    val sideLengthY = measuredHeight
    val sideLengthX = measuredWidth
    val fullRect = Rect(0,0, sideLengthX,sideLengthY)
    if (canvas != null) {
            if (canvas != null) {
                val paint = Paint()

                val destination = BitmapFactory.decodeResource(
                    context.resources,
                    R.drawable.black_circle
                )

                val source = BitmapFactory.decodeResource(
                    context.resources,
                    R.drawable.bikeriding
                )
                canvas.drawBitmap(destination, null, fullRect, paint)

 paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN)

                canvas.drawBitmap(source, null, fullRect, paint)
            }
        }
    }

现在的问题是它没有绘制圆形图像。这是我使用自定义视图的方式:

    <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/motion_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorAccent"
>

    <com.example.mycustomVews.ImageCutomView
            android:id="@+id/red_star"
            android:layout_width="400dp"
            android:layout_height="400dp" 
            android:background="@color/aqua_green"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

但是当它呈现时它看起来像这样:

我做错了什么。我尝试了所有 porterDuff 模式,但没有任何效果。

这是这个问题的答案PorterDuffXfermode DST_IN not working as expected

他说:

"Be careful though: it will work only in a transparent Bitmap or in layer. When you draw directly on screen, the destination is already filled by the window background."

你也可以剪辑之前的 canvas 然后填充剪辑的 canvas,所以你会在 Java:

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    Path path = new Path();

    // IN CASE OF RECTANGLE
    path.addCircle(getWidth()/2f, getHeight()/2f, Math.min(getWidth(), getHeight())/2f, Path.Direction.CW);

    canvas.clipPath(path);


    // CENTERED POSITION, NOT SCALED BITMAP
    float l = (getWidth()-yourSourceBitmap.getWidth())/2f;
    float t = (getHeight()-yourSourceBitmap.getHeight())/2f;

    // DRAW INTO CLIPPED
    canvas.drawBitmap(yourSourceBitmap, l, t, null);


}