sensor = (TextView) findViewById(R.id.sensor);
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
List<Sensor> sensorList = mSensorManager.getSensorList(Sensor.TYPE_ALL);
StringBuilder builder = new StringBuilder();
builder.append("传感器总数: "+sensorList.size()+"\n");
for (int i = 0; i < sensorList.size(); i++) {
Sensor sensor = sensorList.get(i);
builder.append("传感器名称: "+sensor.getName()+", 传感器生产厂商: "+sensor.getVendor()+"\n");
}
sensor.setText(builder.toString());
//摇一摇---->加速传感器
//1、初始化控件
initView();
//2、初始化音乐SoundPool
initSoundPool();
//3、震动
initVibrator();
up_logo = (ImageView) findViewById(R.id.up_logo); down_logo = (ImageView) findViewById(R.id.down_logo);
private void initSoundPool() {
if(Build.VERSION.SDK_INT>=21){
SoundPool.Builder builder = new SoundPool.Builder();
//设置
builder.setMaxStreams(1);
AudioAttributes attributes = new AudioAttributes.Builder()
.setLegacyStreamType(AudioManager.STREAM_MUSIC)
.build();
builder.setAudioAttributes(attributes);
mSoundPool = builder.build();
}else {
//已经过时,老版本
mSoundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);//参数三保留参数
}
//添加音乐
//参数三是音乐池中音乐播放的优先级
mSoundPool_id = mSoundPool.load(this, R.raw.awe, 1);
}
@Override
protected void onStart() {
super.onStart();
//4、设置传感器监听,加速传感器
initSensor();
}
@Override
protected void onStop() {
super.onStop();
//解除注册
mSensorManager.unregisterListener(this);
}
private void initSensor() {
Sensor accelerometerSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
//通过SensorManager实现监听加速传感器
mSensorManager.registerListener(this,accelerometerSensor,SensorManager.SENSOR_DELAY_UI);
}
//数据发生变化
@Override
public void onSensorChanged(SensorEvent event) {
}
//精度发生变化,传感器的,该方法用不到
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
@Override
public void onSensorChanged(SensorEvent event) {//数据发生变化
Sensor sensor = event.sensor;
int type = sensor.getType();
switch (type){
case Sensor.TYPE_ACCELEROMETER://加速传感器
float[] values = event.values;
//x,y,z 三个方向
//9.8
float x = values[0];
float y = values[1];
float z = values[2];
if(Math.abs(x)>25||Math.abs(y)>25||Math.abs(z)>25){
//触发摇一摇
//音乐播放
mSoundPool.play(mSoundPool_id,1,1,0,0,1);
//震动(-1代表只执行一次)
mVibrator.vibrate(new long[]{200,300,400,200},-1);
//动画执行
initAnimation();
}
break;
}
}
private void initAnimation() {
//up_logo 向上移动,同时有上下震动
AnimationSet set_up = new AnimationSet(true);
TranslateAnimation up_up = new TranslateAnimation(
TranslateAnimation.RELATIVE_TO_SELF,0,//x轴起点
TranslateAnimation.RELATIVE_TO_SELF,0,//x轴终点
TranslateAnimation.RELATIVE_TO_SELF,0,//y轴起点
TranslateAnimation.RELATIVE_TO_SELF,-1//y轴终点
);
up_up.setDuration(1000);
TranslateAnimation up_down = new TranslateAnimation(
TranslateAnimation.RELATIVE_TO_SELF,0,//x轴起点
TranslateAnimation.RELATIVE_TO_SELF,0,//x轴终点
TranslateAnimation.RELATIVE_TO_SELF,-1,//y轴起点
TranslateAnimation.RELATIVE_TO_SELF,0//y轴终点
);
up_down.setDuration(1000);
//延迟执行set中的某一动画
up_down.setStartOffset(500);
set_up.addAnimation(up_up);//移动上去;
set_up.addAnimation(up_down);//拉下来
up_logo.startAnimation(set_up);
//----------
AnimationSet set_down = new AnimationSet(true);
TranslateAnimation down_down = new TranslateAnimation(
TranslateAnimation.RELATIVE_TO_SELF,0,//x轴起点
TranslateAnimation.RELATIVE_TO_SELF,0,//x轴终点
TranslateAnimation.RELATIVE_TO_SELF,0,//y轴起点
TranslateAnimation.RELATIVE_TO_SELF,1//y轴终点
);
down_down.setDuration(1000);
TranslateAnimation down_up = new TranslateAnimation(
TranslateAnimation.RELATIVE_TO_SELF,0,//x轴起点
TranslateAnimation.RELATIVE_TO_SELF,0,//x轴终点
TranslateAnimation.RELATIVE_TO_SELF,1,//y轴起点
TranslateAnimation.RELATIVE_TO_SELF,0//y轴终点
);
down_up.setDuration(1000);
down_up.setStartOffset(500);
set_down.addAnimation(down_down);//向下移动
set_down.addAnimation(down_up);//往上拉动
down_logo.startAnimation(set_down);
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有