弓背霞明剑照霜,秋风走马出咸阳。这篇文章主要讲述Android学习笔记:摇一摇功能的实现相关的知识,希望能为你提供帮助。
首先需要用到一个摇一摇的工具类
ShakeManager.java
1 package com.wuxianedu.oschina.utils; 2 3 import android.content.Context; 4 import android.hardware.Sensor; 5 import android.hardware.SensorEvent; 6 import android.hardware.SensorEventListener; 7 import android.hardware.SensorManager; 8 9 /** 10* 震动监听器 11*/ 12 public class ShakeManager implements SensorEventListener { 13 14private Context mContext; 15private static final int SPEED_SHRESHOLD = 4500; //这个值越大需要越大的力气来摇晃手机 16private static final int UPTATE_INTERVAL_TIME = 50; 17private SensorManager sensorManager; 18private Sensor sensor; 19private ShakeListener shakeListener; 20private float lastX; 21private float lastY; 22private float lastZ; 23private long lastUpdateTime; 24 25public ShakeManager(Context c) { 26mContext = c; 27start(); 28} 29 30/** 31* 启动 32*/ 33public void start() { 34sensorManager = (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE); 35if (sensorManager != null) { 36sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); 37} 38if (sensor != null) { 39sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_GAME); 40} 41} 42 43/** 44* 停止 45*/ 46public void stop() { 47sensorManager.unregisterListener(this); 48} 49 50public void onSensorChanged(SensorEvent event) { 51long currentUpdateTime = System.currentTimeMillis(); 52long timeInterval = currentUpdateTime - lastUpdateTime; 53if (timeInterval < UPTATE_INTERVAL_TIME) 54return; 55lastUpdateTime = currentUpdateTime; 56 57float x = event.values[0]; 58float y = event.values[1]; 59float z = event.values[2]; 60 61float deltaX = x - lastX; 62float deltaY = y - lastY; 63float deltaZ = z - lastZ; 64 65lastX = x; 66lastY = y; 67lastZ = z; 68 69double speed = Math.sqrt(deltaX * deltaX + deltaY * deltaY + deltaZ 70* deltaZ) 71/ timeInterval * 10000; 72if (speed > = SPEED_SHRESHOLD) { 73shakeListener.onShake(); 74} 75} 76 77public void onAccuracyChanged(Sensor sensor, int accuracy) {} 78 79public void setShakeListener(ShakeListener shakeListener) { 80this.shakeListener = shakeListener; 81} 82 83public interface ShakeListener { 84void onShake(); 85} 86 87 }
接下来我们在MainActivity.java中来使用
1private ShakeManager shake; 2 3shake.setShakeListener(new ShakeManager.ShakeListener() { 4@Override 5public void onShake() { 6QiStart(); 7shake.stop(); 8canrads.setVisibility(View.GONE); 9soundPool.play(map.get(1),0.3f,0.3f,1,0,0); 10} 11}); 12 13 14 15/** 16* 开始动画 17*/ 18private void QiStart() { 19TranslateAnimation up = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,0f 20,Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,-0.3f); 21up.setDuration(600L); 22up.setRepeatCount(1); 23up.setRepeatMode(Animation.REVERSE); 24imageshang.startAnimation(up); 25 26TranslateAnimation down = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,0f 27,Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,0.3f); 28down.setDuration(600L); 29down.setRepeatCount(1); 30down.setRepeatMode(Animation.REVERSE); 31imagexia.startAnimation(down); 32 33down.setAnimationListener(new Animation.AnimationListener() { 34@Override 35public void onAnimationStart(Animation animation) { 36 37} 38@Override 39public void onAnimationEnd(Animation animation) { 40xian2.setVisibility(View.VISIBLE); 41new RequestClient(getContext(),requestConfig){ 42 43@Override 44protected void loadSuccess(String result) { 45try { 46result = new String(result.getBytes("ISO-8859-1"),"UTF-8"); 47} catch (UnsupportedEncodingException e) { 48e.printStackTrace(); 49} 50shakeb = JSONParseUtil.parseObject(result,Shakeb.class); 51assert shakeb != null; 52String title = shakeb.getOwner()!=null ? shakeb.getOwner().getName():""; 53tv_neirong.setText(title); 54tv_biaoti.setText(shakeb.getDescription()); 55tv_chakan.setText(String.valueOf(shakeb.getForks_count())); 56tv_xingxing.setText(String.valueOf(shakeb.getStars_count())); 57tv_disange.setText(String.valueOf(shakeb.getWatches_count())); 58//获取图片地址 59String imageUrl = shakeb.getOwner().getNew_portrait(); 60ImageLoader imageLoader = ImageLoaderFactory.getImageLoader(); 61//加载图片 62imageLoader.loadImage(imageUrl,roundImageView); 63//隐藏正在加载 64xian2.setVisibility(View.GONE); 65//播放声音 66soundPool.play(map.get(0),0.3f,0.3f,1,0,0); 67//显示推举项目 68canrads.setVisibility(View.VISIBLE); 69//摇一摇监听开始 70shake.start(); 71} 72 73@Override 74protected void loadFail() { 75 76} 77}.get(RequestAPI.SHAKE); 78} 79 80@Override 81public void onAnimationRepeat(Animation animation) { 82 83} 84}); 85 86}
/** * 判断Fragment是否可见 * @param hidden */ @Override public void onHiddenChanged(boolean hidden) { super.onHiddenChanged(hidden); if(hidden){ shake.stop(); }else{ shake.start(); } }true为不可见
【Android学习笔记(摇一摇功能的实现)】
推荐阅读
- android开发--多线程
- wemall app商城源码Android之支付宝通知处理类
- Android Studio插件安装及使用Genymotion模拟器
- 手机端横竖屏切换,怎么做才能安卓浏览器及时改变字体大小
- Android核心组件 Activity组件
- android WebView总结
- 第一行代码 Android 第二版到货啦
- Android进度条控制图片旋转·平移·缩放·倾斜
- Android Studio第二十三期 - TextView自适应缩放大小 国外大神的library