Android绘制-PorterDuffXfermode SRC_不工作

Android绘制-PorterDuffXfermode SRC_不工作,android,porter-duff,Android,Porter Duff,根据本文和图片,我想使用PorterDuffXfermode实现圆形图像效果: 所以我有一个目的地圈 我有一个源图像: 我创建了一个自定义的imageview,在ondraw中我要画一个像教程一样的圆形图像。因此,我的代码如下所示: class ImageCutomView : ImageView { constructor(p0: Context) : super(p0) constructor(p0: Context, p1: AttributeSet

根据本文和图片,我想使用PorterDuffXfermode实现圆形图像效果:

所以我有一个目的地圈

我有一个源图像:

我创建了一个自定义的imageview,在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模式,没有任何效果

这是这个问题的答案

他说:

但要小心:它只能在透明位图或图层中工作。当您直接在屏幕上绘制时,目标已被窗口背景填充

您还可以先剪裁画布,然后填充剪裁后的画布,因此您可以在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);


}

最终的结果是什么,显示一幅你期望最终图像的外观??应该是圆形图像。是的,我可以使用剪辑路径,但只是想知道为什么porterduff模式不起作用,谢谢