[Android实例] 仿微信摇一摇功能实现
2016-01-05 12:02
676 查看
该篇文章从eoeAndroid搬迁过来的,原文地址:[Android实例] 仿微信摇一摇功能实现
前段时间,由于换了新工作,参与到公司新项目里,很忙,也没时间去弄一些好玩的东西。这些天不算忙,突然想到我在项目里仿照微信做了摇一摇功能,也就吧该功能提取出来,共享给大家。 摇一摇功能其实很简单,主要用到了加速度传感器,因此需要加入权限
主要代码如下:
1.监听传感器加速变化并进行处理
2.动画效果
下载地址:项目代码
前段时间,由于换了新工作,参与到公司新项目里,很忙,也没时间去弄一些好玩的东西。这些天不算忙,突然想到我在项目里仿照微信做了摇一摇功能,也就吧该功能提取出来,共享给大家。 摇一摇功能其实很简单,主要用到了加速度传感器,因此需要加入权限
<uses-permission android:name="android.permission.VIBRATE"/>
主要代码如下:
1.监听传感器加速变化并进行处理
@Override public void onSensorChanged(SensorEvent event) { // TODO Auto-generated method stub int sensorType = event.sensor.getType(); // values[0]:X轴,values[1]:Y轴,values[2]:Z轴 float[] values = event.values; if (sensorType == Sensor.TYPE_ACCELEROMETER) { if ((Math.abs(values[0]) > 17 || Math.abs(values[1]) > 17 || Math .abs(values[2]) > 17) && !isShake) { isShake = true; new Thread() { public void run() { try { runOnUiThread(new Runnable() { public void run() { // 摇动手机后,再伴随震动提示~~ vibrator.vibrate(300); topLineIv.setVisibility(View.VISIBLE); bottomLineIv.setVisibility(View.VISIBLE); startAnimation(false); } }); Thread.sleep(500); runOnUiThread(new Runnable() { public void run() { // 摇动手机后,再伴随震动提示~~ vibrator.vibrate(300); } }); Thread.sleep(500); runOnUiThread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub isShake = false; startAnimation(true); } }); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } }; }.start(); } } }
2.动画效果
private void startAnimation(boolean isBack) { int type = TranslateAnimation.RELATIVE_TO_SELF; float topFromYValue; float topToYValue; float bottomFromYValue; float bottomToYValue; if (isBack) { topFromYValue = -0.5f; topToYValue = 0; bottomFromYValue = 0.5f; bottomToYValue = 0; } else { topFromYValue = 0; topToYValue = -0.5f; bottomFromYValue = 0; bottomToYValue = 0.5f; } TranslateAnimation topAnimation = new TranslateAnimation(type, 0, type, 0, type, topFromYValue, type, topToYValue); topAnimation.setDuration(200); topAnimation.setFillAfter(true); TranslateAnimation bottomAnimation = new TranslateAnimation(type, 0, type, 0, type, bottomFromYValue, type, bottomToYValue); bottomAnimation.setDuration(200); bottomAnimation.setFillAfter(true); if (isBack) { bottomAnimation .setAnimationListener(new TranslateAnimation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { // TODO Auto-generated method stub } @Override public void onAnimationRepeat(Animation animation) { // TODO Auto-generated method stub } @Override public void onAnimationEnd(Animation animation) { // TODO Auto-generated method stub topLineIv.setVisibility(View.GONE); bottomLineIv.setVisibility(View.GONE); } }); } bottomLayout.startAnimation(bottomAnimation); topLayout.startAnimation(topAnimation); }
下载地址:项目代码
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories