-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainActivity.java
104 lines (80 loc) · 3.44 KB
/
MainActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package com.example.fda;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Button;
import com.google.ar.core.Anchor;
import com.google.ar.sceneform.AnchorNode;
import com.google.ar.sceneform.rendering.ModelRenderable;
import com.google.ar.sceneform.ux.ArFragment;
import com.google.ar.sceneform.ux.TransformableNode;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private ArFragment arFragment;
private ArrayList<Integer> imagesPath = new ArrayList<Integer>();
private ArrayList<String> namesPath = new ArrayList<>();
private ArrayList<String> modelNames = new ArrayList<>();
AnchorNode anchorNode;
private Button btnRemove;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
arFragment = (ArFragment)getSupportFragmentManager().findFragmentById(R.id.fragment);
btnRemove = findViewById(R.id.remove);
getImages();
arFragment.setOnTapArPlaneListener((hitResult, plane, motionEvent) -> {
Anchor anchor = hitResult.createAnchor();
ModelRenderable.builder()
.setSource(this,Uri.parse(Common.model))
.build()
.thenAccept(modelRenderable -> addModelToScene(anchor,modelRenderable));
});
btnRemove.setOnClickListener(view -> removeAnchorNode(anchorNode));
}
private void getImages() {
imagesPath.add(R.drawable.table);
imagesPath.add(R.drawable.bookshelf);
imagesPath.add(R.drawable.lamp);
imagesPath.add(R.drawable.odltv);
imagesPath.add(R.drawable.clothdryer);
imagesPath.add(R.drawable.chair);
namesPath.add("Table");
namesPath.add("BookShelf");
namesPath.add("Lamp");
namesPath.add("Old Tv");
namesPath.add("Cloth Dryer");
namesPath.add("Chair");
modelNames.add("table.sfb");
modelNames.add("model.sfb");
modelNames.add("lamp.sfb");
modelNames.add("tv.sfb");
modelNames.add("cloth.sfb");
modelNames.add("chair.sfb");
initaiteRecyclerview();
}
private void initaiteRecyclerview() {
LinearLayoutManager layoutManager = new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false);
RecyclerView recyclerView = (RecyclerView)findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(layoutManager);
RecyclerviewAdapter adapter = new RecyclerviewAdapter(this,namesPath,imagesPath,modelNames);
recyclerView.setAdapter(adapter);
}
private void addModelToScene(Anchor anchor, ModelRenderable modelRenderable) {
anchorNode = new AnchorNode(anchor);
TransformableNode node = new TransformableNode(arFragment.getTransformationSystem());
node.setParent(anchorNode);
node.setRenderable(modelRenderable);
arFragment.getArSceneView().getScene().addChild(anchorNode);
node.select();
}
public void removeAnchorNode(AnchorNode nodeToremove) {
if (nodeToremove != null) {
arFragment.getArSceneView().getScene().removeChild(nodeToremove);
nodeToremove.getAnchor().detach();
nodeToremove.setParent(null);
}
}
}