安卓高階5 感測器和震動 模仿微信搖一搖Ui效果
阿新 • • 發佈:2019-01-29
效果圖:
所用的Ui就三張圖:
案例程式碼:
結構
MainActivity.java
package com.example.myapp;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private SensorManager sensorManager;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.activity_main);
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
List<Sensor> sensorList = sensorManager.getSensorList(Sensor.TYPE_ALL);
StringBuilder sb = new StringBuilder();
sb.append("您手機的感測器個數 :" + sensorList.size() + "\n");
sb.append("列表如下:\n");
for (Sensor sensor : sensorList) {
sb.append(sensor.getName() + "\n");
}
textView = ((TextView) findViewById(R.id.tv));
textView.setText(sb);
}
public void click(View view) {
startActivity(new Intent(this, ShowActivity.class));
}
}
ShowActivity.java
package com.example.myapp;
import android.animation.ObjectAnimator;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Vibrator;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import static android.hardware.SensorManager.SENSOR_DELAY_NORMAL;
public class ShowActivity extends AppCompatActivity implements SensorEventListener {
private SensorManager sensorManage;
private Sensor sensor;
private float xFlaot;
private float yFlaot;
private float zFlaot;
private ImageView up;
private ImageView down;
private ObjectAnimator upAnmation;
private ObjectAnimator downAnamtion;
private AnimationSet setUp;
private TranslateAnimation up1;
private Vibrator vs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show);
//獲取感測器管理器
sensorManage = ((SensorManager) getSystemService(SENSOR_SERVICE));
//獲取指定感測器
sensor = sensorManage.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
//獲取震動物件
vs = ((Vibrator) getSystemService(VIBRATOR_SERVICE));
//Ui圖片手上部分
up = (ImageView) findViewById(R.id.Myup);
//Ui圖手下部分
down = (ImageView) findViewById(R.id.Mydwon);
//初始化動畫
initAnimation();
}
private void initAnimation() {
/* upAnmation = ObjectAnimator.ofFloat(up, "translationY", 0, -200, 0);
upAnmation.setDuration(4000);*/
downAnamtion = ObjectAnimator.ofFloat(down,"translationY",0,200,0);
downAnamtion.setDuration(4000);
up1 = new TranslateAnimation(0,0,0,-200);
up1.setDuration(2000);
final TranslateAnimation up2= new TranslateAnimation(0,0,-200,0);
up2.setDuration(2000);
setUp = new AnimationSet(false);
setUp.addAnimation(up1);
up1.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
up.clearAnimation();
up.startAnimation(up2);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
/*upAnmation.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});*/
}
@Override
protected void onResume() {
super.onResume();
//註冊監聽
sensorManage.registerListener(this, sensor, SENSOR_DELAY_NORMAL);
}
@Override
public void onSensorChanged(SensorEvent event) {
float[] values = event.values;
xFlaot = values[0]; //X軸加速度 左右
yFlaot = values[1]; //Y軸加速度 前後
zFlaot = values[2]; //Z軸加速 重力
int val = 7;
if (xFlaot>val||yFlaot>val||zFlaot>13){
up.startAnimation(up1);
//自定義一個震動模式 兩個為一組 前一個引數為播放延遲時間 第二個播放時長
long [] patter =new long[]{100l,2000l,300l,4000l};
//第一個震動模式,第二個引數從振動模式選取下標中某個重複震動
//假設第二個引數是1 那麼將從 2000l,300l 作為一組陣列一直重複
// 如果第二個引數為震動模式的最後一位 那麼將不震動(因為只有一個延遲播放時間,沒有播放時間)
//需要許可權<uses-permission android:name="android.permission.VIBRATE"/>
vs.vibrate(patter,3);
downAnamtion.start();
Log.e("fmy","啟動");
}
}
@Override
protected void onPause() {
super.onPause();
//解除註冊
sensorManage.unregisterListener(this);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.myapp.MainActivity">
<LinearLayout
android:orientation="vertical"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click"
android:text="檢視實時重力指數" />
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</LinearLayout>
</ScrollView>
activity_show.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_show"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#1d1d1d"
android:orientation="vertical"
tools:context="com.example.myapp.ShowActivity">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@mipmap/shakehideimg_man2" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="vertical">
<ImageView
android:id="@+id/Myup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:src="@mipmap/shake_logo_up" />
<ImageView
android:id="@+id/Mydwon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:src="@mipmap/shake_logo_down" />
</LinearLayout>
</RelativeLayout>