摇一摇换图:
1.获取手机的传感(相当于计步器):
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
2.调用手机中的震动;在换图的时候可以很好的提醒:
mVibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE);
3.给震动做监听(当震动响起的时候换图);又叫传感器事件监听SensorEventListener实现两个方法,在改变中做处理:
private class sensorListener implements SensorEventListener { @Override public void onSensorChanged(SensorEvent event) { int sensorType = event.sensor.getType(); float[] values = event.values; if (sensorType == Sensor.TYPE_ACCELEROMETER) { if ((Math.abs(values[0]) > 12 || Math.abs(values[1]) > 12 || Math .abs(values[2]) > 12)) { flag++; if (flag > 5) flag = 0; long[] pattern = { 500, 500 }; mVibrator.vibrate(pattern, -1); mHandler.sendEmptyMessage(0);//发送消息,提醒换图! } } }
4.在线程中用handler来改变震动换图:
Handler mHandler = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); Resources resources = getResources(); switch (flag){ case 0: mChange.setBackgroundDrawable(resources.getDrawable(R.drawable.pic06)); break; case 1: mChange.setBackgroundDrawable(resources.getDrawable(R.drawable.pic01)); break; case 2: mChange.setBackgroundDrawable(resources.getDrawable(R.drawable.pic02)); break; case 3: mChange.setBackgroundDrawable(resources.getDrawable(R.drawable.pic03)); break; case 4: mChange.setBackgroundDrawable(resources.getDrawable(R.drawable.pic04)); break; case 5: mChange.setBackgroundDrawable(resources.getDrawable(R.drawable.pic05)); break; } } };
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦