gpt4 book ai didi

android - 如何在Android中拖拽和删除ARCore节点?

转载 作者:行者123 更新时间:2023-11-29 22:46:33 25 4
gpt4 key购买 nike

我有问题,我想在 ArFragment 中拖动和删除对象。我试图将 ontouchlistener 添加到每个节点,并将 ondraglistener 添加到删除区域。这是我的代码:

这是我的触摸监听器

    override fun onTouch(view: View, motionEvent: MotionEvent): Boolean {
if (motionEvent.getAction() === MotionEvent.ACTION_DOWN) {
val data = ClipData.newPlainText("", "")
val shadowBuilder = View.DragShadowBuilder(
view
)
view.startDrag(data, shadowBuilder, view, 0)
view.setVisibility(View.INVISIBLE)
return true
} else {
return false
}
}
}

这是我的删除区域的拖动监听器

internal class MyDragListener: View.OnDragListener {

override fun onDrag(v: View, event: DragEvent): Boolean {
when (event.getAction()) {
DragEvent.ACTION_DRAG_STARTED -> {
v.visibility = View.VISIBLE
}
DragEvent.ACTION_DRAG_ENTERED -> v.visibility = View.VISIBLE
DragEvent.ACTION_DRAG_EXITED -> v.visibility = View.GONE
DragEvent.ACTION_DROP -> {
}
DragEvent.ACTION_DRAG_ENDED -> v.visibility = View.GONE
else -> {
}
}// do nothing
return true
}
}

这是我当前删除的节点(所有节点)

fun removeAllSticker(fragment: ArFragment) {
val nodeList = ArrayList(fragment.getArSceneView().getScene().getChildren())
for (childNode in nodeList) {
if (childNode is AnchorNode) {
if ((childNode as AnchorNode).anchor != null) {
(childNode as AnchorNode).anchor!!.detach()
fragment.getArSceneView().getScene().removeChild(childNode)
(childNode as AnchorNode).setParent(null)
}
}
}
cleanupFragment(fragment)
}

private fun cleanupFragment(fragment: ArFragment) {
val scene = fragment.arSceneView.scene
fragment.setOnTapArPlaneListener(null)
scene!!.removeOnUpdateListener{
fragment.onUpdate(it)
}
}

请帮帮我,我卡住了,已经在谷歌上搜索了

最佳答案

这是一个简单的代码 fragment ,展示了如何创建和删除节点:

AnchorNode anchorsNode = new AnchorNode(anchor);
anchorsNode.setParent(arFragment.getArSceneView().getScene());

Node holdNode = new Node();
holdNode.setParent(anchorsNode);
holdNode.setRenderable(yourModelRenderable);

holdNode.setOnTouchListener(new Node.OnTouchListener() {

@override
public boolean onTouch(HitTestResult hitTestResult,
MotionEvent motionEvent) {

anchorsNode.removeChild(holdNode);
return true;
}
});

或者,您可以使用来自 this 的示例代码 发布。

如果您想知道如何在 ARCore 中拖动节点,请阅读 this RW 帖子。

关于android - 如何在Android中拖拽和删除ARCore节点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58341760/

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