#表示するテキストの作成
背景を白で、テキストの色を濃い青に設定
TextView tv = new TextView(this);
tv.setTextColor(Color.rgb(0x0,0x0,0xaa));
tv.setBackgroundColor(Color.rgb(0xff,0xff,0xff));
tv.setText("練習だよ");
MainActivity.java
public class MainActivity extends AppCompatActivity {
private static final int CAMERA_PERMISSION_CODE = 0;
private static final String CAMERA_PERMISSION = Manifest.permission.CAMERA;
private ViewRenderable textViewRenderable;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArCoreApk.Availability availability =
ArCoreApk.getInstance().checkAvailability(this);
if (availability.isSupported()) {
Toast.makeText(this, "AR機能が利用できます", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "AR機能を利用することができません", Toast.LENGTH_SHORT).show();
finish();
return;
}
setContentView(R.layout.activity_main);
TextView tv = new TextView(this);
tv.setTextColor(Color.rgb(0x0,0x0,0xaa));
tv.setBackgroundColor(Color.rgb(0xff,0xff,0xff));
tv.setText("練習だよ");
ArFragment arFragment = (ArFragment) getSupportFragmentManager().findFragmentById(R.id.arfragment);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
ViewRenderable.builder()
.setView(this, tv)
.build()
.thenAccept(renderable -> textViewRenderable = renderable);
}
if(arFragment != null ) {
arFragment.setOnTapArPlaneListener(
(HitResult hitResult, Plane plane, MotionEvent motionEvent) -> {
Anchor anchor = hitResult.createAnchor();
AnchorNode anchorNode = new AnchorNode(anchor);
anchorNode.setParent(arFragment.getArSceneView().getScene());
TransformableNode text = new TransformableNode(arFragment.getTransformationSystem());
text.setParent(anchorNode);
text.setRenderable(textViewRenderable);
});
}
}
@Override
protected void onResume() {
super.onResume();
if (ContextCompat.checkSelfPermission(this, CAMERA_PERMISSION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[] {CAMERA_PERMISSION}, CAMERA_PERMISSION_CODE);
}
}
}
activity_main.xml
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/arfragment"
android:name="com.google.ar.sceneform.ux.ArFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
app.gradle
apply plugin: 'com.android.application'
apply plugin: 'com.google.ar.sceneform.plugin'
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "jp.shsit.arcore3"
minSdkVersion 24
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
//ARCore関係
implementation "com.gorisse.thomas.sceneform:sceneform:1.21.0"
}
これがないと、エラーが出ます
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
build.gradle
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.3'
classpath 'com.google.ar.sceneform:plugin:1.15.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="jp.shsit.arcore3">
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.ar" android:required="true"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data android:name="com.google.ar.core" android:value="requireed"/>
<activity android:name=".TextActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity" />
<activity android:name=".ViewActivity" />
</application>
</manifest>
AR機能を必須(requireed)とするか?選択(Optional)とするか?を記述します。今回は、必須としました。
meta-data android:name="com.google.ar.core" android:value="requireed"