Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/198.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 图形透明区域未使用拖动侦听器更新_Android_Drag And Drop_Android Canvas_Paint_Porter Duff - Fatal编程技术网

Android 图形透明区域未使用拖动侦听器更新

Android 图形透明区域未使用拖动侦听器更新,android,drag-and-drop,android-canvas,paint,porter-duff,Android,Drag And Drop,Android Canvas,Paint,Porter Duff,我从一个自定义的视图组中看到了很多关于透明的背景的问题,但是似乎没有人有这个问题 背景: 我创建了一个自定义的框架布局;此容器动态添加了视图。其子对象应具有透明背景,但容器的另一个表面必须具有背景色。子视图可以拖放到该容器的任何位置 我的工作: 我覆盖dispatchDraw(),创建一个位图和一个新的画布,然后用白色背景填充新画布。 我在children视图上创建一个循环,从children的维度创建一个新的Paint,一个Rect,并使用PorterDuff.Mode.DST_OUT清除孩子

我从一个自定义的
视图组中看到了很多关于
透明的
背景的问题,但是似乎没有人有这个问题

背景:
我创建了一个自定义的
框架布局
;此容器动态添加了视图。其子对象应具有透明背景,但容器的另一个表面必须具有背景色。子视图可以
拖放到该容器的任何位置

我的工作:
我覆盖
dispatchDraw()
,创建一个
位图和一个新的
画布
,然后用白色背景填充新画布。
我在children视图上创建一个循环,从children的维度创建一个新的
Paint
,一个
Rect
,并使用
PorterDuff.Mode.DST_OUT
清除孩子的区域。对于每个孩子,我将画图和矩形添加到新画布中。
最后,我通过传递创建的位图,在主画布上使用
dispatchDraw()
给出的
drawBitmap

问题:
这很好:孩子们有一个透明的背景,其余的都是白色背景。但是,当我将
DragListener
添加到子视图时,“剪切”区域不会更新(而
dispatchDraw
被正确调用):换句话说,当我拖动子视图时,它会被很好地删除,但透明区域仍然在同一位置。

代码:
自定义的
框架布局

@Override
public void dispatchDraw(Canvas canvas) {
    super.dispatchDraw(canvas);
    drawCanvas(canvas);
}

private void drawCanvas(Canvas canvas) {
    // Create an off-screen bitmap and its canvas
    Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
    Canvas auxCanvas = new Canvas(bitmap);

    // Fill the canvas with the desired outside color
    auxCanvas.drawColor(Color.WHITE);

    // Create a paint for each child into parent
    for (int i = 0; i < getChildCount(); ++i) {
        // Create a transparent area for the Rect child
        View child = this.getChildAt(i);
        Paint childPaint = new Paint();
        childPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
        Rect childRect = new Rect(child.getLeft(), child.getTop(), child.getRight(), child.getBottom());
        auxCanvas.drawRect(childRect, childPaint);
    }

    // Draw the bitmap into the original canvas
    canvas.drawBitmap(bitmap, 0, 0, null);
}
屏幕截图:

我尝试了很多关于我发现的所有问答的事情,但似乎没有任何效果。

任何帮助都将不胜感激。

最后,我发现了一条线索:透明的
绘制在更新后没有获得正确的x轴和y轴值

我想当跌落发生时,
getLeft()
getTop()
getRight()
getBottom()
不会改变。奇怪的是,在我的日志中,这些值似乎被更新了。因此,我使用
getX()
getY()
尝试在
DragEvent.ACTION\u DROP
中使用更新的值,它正确地更改了透明区域的坐标

循环中的子项的解决方案:

Paint childPaint = new Paint();
childPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
// Use the x and y axis (plus the width and height)
Rect childRect = new Rect(
    (int) child.getX(),
    (int) child.getY(),
    (int) child.getX() + child.getWidth(),
    (int) child.getY() + child.getHeight()
);
auxCanvas.drawRect(childRect, childPaint);
Paint childPaint = new Paint();
childPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
// Use the x and y axis (plus the width and height)
Rect childRect = new Rect(
    (int) child.getX(),
    (int) child.getY(),
    (int) child.getX() + child.getWidth(),
    (int) child.getY() + child.getHeight()
);
auxCanvas.drawRect(childRect, childPaint);