Skip to content

Instantly share code, notes, and snippets.

@S-Maxime
Created January 1, 2021 23:13
Show Gist options
  • Select an option

  • Save S-Maxime/7e6d66e622d59e71c2a6e029f29ef33c to your computer and use it in GitHub Desktop.

Select an option

Save S-Maxime/7e6d66e622d59e71c2a6e029f29ef33c to your computer and use it in GitHub Desktop.
<template>
<Page :actionBarHidden="true">
<AbsoluteLayout height="100%" width="100%" backgroundColor="red">
<Image src="~/assets/back.jpg"
height="100%"
width="100%"
stretch="fill"/>
<Image src="~/assets/perso.png"
id="perso"
ref="perso"/>
</AbsoluteLayout>
</Page>
</template>
<script lang="ts">
import {
AndroidSensorListener,
AndroidSensors,
SensorDelay,
} from 'nativescript-android-sensors';
import { Enums } from '@nativescript/core'
import { Component, Vue } from 'vue-property-decorator'
@Component
export default class TestParallax extends Vue {
public message: string;
private androidSensors: AndroidSensors;
public sensorMaxFifoEventCount: string = ''
public sensorData: string = ''
constructor() {
super();
this.androidSensors = new AndroidSensors();
const listener = new AndroidSensorListener({
onSensorChanged: (result) => {
// console.log('SensorChangedEvent', result);
// result is being returned as a string currently
const parsedData = JSON.parse(result);
const rawSensorData = parsedData.data;
this.sensorData = rawSensorData.x;
// const sensor = parsedData.sensor;
// const time = parsedData.time;
// @ts-ignore
this.$refs.perso.nativeView.animate({
translate: {
x: rawSensorData.x * 100,
y: rawSensorData.y * 100
},
duration: 500,
curve: Enums.AnimationCurve.linear
})
},
onAccuracyChanged: (sensor, accuracy) => {
// console.log('Accuracy Changed', sensor, accuracy);
},
});
this.androidSensors.setListener(listener);
}
public startLinearAcceleration() {
// starting the acceleration sensor
const acceleration = this.androidSensors.startSensor(
// @ts-ignore
android.hardware.Sensor.TYPE_LINEAR_ACCELERATION,
SensorDelay.GAME,
8000000
);
// checking if it supports FIFO
const x = (acceleration as any).getFifoMaxEventCount();
this.sensorMaxFifoEventCount = `Max Fifo Event Count: ${x}`
// after 8 seconds we are stopping the acceleration sensor
/*setTimeout(() => {
this.androidSensors.stopSensor(acceleration);
this.sensorData = 'Sensor has stopped!'
}, 20000);*/
}
private mounted () {
setTimeout(() => {
this.startLinearAcceleration()
}, 1000)
}
}
</script>
<style>
#perso {
height: 70%;
width: 70%;
top: 280;
left: 100;
}
</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment