利用传感器(sensor)实现微信摇一摇动画
所需要的权限:
<uses-permission android:name="android.permission.VIBRATE"></uses-permission>
xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="fanggao.qf.sensor.MainActivity">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_marginTop="150dp"
android:layout_marginLeft="80dp"
android:src="@mipmap/down"/>
<ImageView
android:id="@+id/image_up"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/yaoyiyao"/>
<ImageView
android:id="@+id/image_down"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/yaoyiyao"/>
</FrameLayout>
</LinearLayout>
源代码:
package fanggao.qf.sensor; import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Vibrator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView; /**
* 1.初始化事件
* 2.获得监听
*/
public class MainActivity extends AppCompatActivity { private ImageView imageUp;
private ImageView imageDown;
private SensorManager sensorManager;
private SensorEventListener sensorEventListener;
private Sensor sensor;
private AnimationSet downAnimationSet;
private AnimationSet upAnimationSet;
//判断动画是否开始
private boolean flag = true;
private SoundPool soundPool;
private int soundId;
private Vibrator vibrator; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
initEvent();
//参数一传感器监听 参数二:监听的传感器对象
//注册摇一摇事件
sensorManager.registerListener(sensorEventListener,sensor,SensorManager.SENSOR_DELAY_NORMAL); } private void initEvent() { /*传感器事件监听器*/
sensorEventListener = new SensorEventListener() {
//当值发生改变的时候调用
@Override
public void onSensorChanged(SensorEvent event) {
float[] values = event.values;
//获取控件的值,设置触发条件
float x = values[0];
float y = values[1];
float z = values[2];
if(x > 15 || y > 15 || z > 15){//表示摇一摇
if(flag) {//正在执行动画的同时不能再次触发
//播放动画
imageUp.startAnimation(upAnimationSet);
imageDown.startAnimation(downAnimationSet);
//播放小音乐,不用MediaPlayer是因为mediaplayer适合播放耗时的文件,并且比较消耗资源
/**
* int soundID 音乐
* float leftVolume左声道
* float rightVolume 右声道
* int priority
* int loop 循环播放
* float rate 优先级
*/
soundPool.play(soundId,1.0f,1.0f,1,1,1.0f);
//震动
//long[] pattern 1,第一次震动延迟的时间 2,第一次震动的持续时间 3,时间间隔 4,第二次震动的时间
//int repeat震动的重复次数 -1表示不重复
vibrator.vibrate(new long[]{400,500,500,500},-1);
}
}
} @Override
public void onAccuracyChanged(Sensor sensor, int accuracy) { }
};
//设置动画监听
upAnimationSet.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
flag = false;
} @Override
public void onAnimationEnd(Animation animation) {
flag = true;
} @Override
public void onAnimationRepeat(Animation animation) { }
});
} /*初始化事件*/
private void initData() {
//获得传感器的管理器
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
//获得加速度传感器
sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
//int maxStreams参数一:表示音乐池数量
//int streamType 参数二:类型
// int srcQuality参数三:资源的质量
soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0);
//将音乐加载到soundPool
soundId = soundPool.load(this, R.raw.music, 1);
//获得震动的服务
vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE); //初始化动画() 两个图片同时进行不能共用,
//图片最终需要回到原点,因此使用补间动画
//上面图片动画集合
upAnimationSet = new AnimationSet(true);
//上面图片动画
//1.先上移
TranslateAnimation upUptranslateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, -0.5f);
//设置时间
upUptranslateAnimation.setDuration(300);
//1.后下移
TranslateAnimation upDowntranslateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, -0.5f, Animation.RELATIVE_TO_SELF, 0);
upDowntranslateAnimation.setDuration(300);
//设置启动延迟,300ms后开始启动
upDowntranslateAnimation.setStartOffset(300);
upAnimationSet.addAnimation(upUptranslateAnimation);
upAnimationSet.addAnimation(upDowntranslateAnimation);
upAnimationSet.setDuration(800);
upAnimationSet.setStartOffset(200); downAnimationSet = new AnimationSet(true);
//下面图片的动画
//1.先上移
TranslateAnimation downUptranslateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0);
downUptranslateAnimation.setDuration(300);
downUptranslateAnimation.setStartOffset(300);
//1.后下移
TranslateAnimation downDowntranslateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0.5f);
downDowntranslateAnimation.setDuration(300);
downAnimationSet.addAnimation(downDowntranslateAnimation);
downAnimationSet.addAnimation(downUptranslateAnimation);
downAnimationSet.setDuration(800);
downAnimationSet.setStartOffset(200); } private void initView() {
imageUp = (ImageView) findViewById(R.id.image_up);
imageDown = (ImageView) findViewById(R.id.image_down);
} @Override
protected void onDestroy() {
sensorManager.unregisterListener(sensorEventListener);
super.onDestroy(); }
}
效果:

摇一摇:

利用传感器(sensor)实现微信摇一摇动画的更多相关文章
- HTML5实现摇一摇的功能(实测后)--转
eviceMotionEvent(设备运动事件)返回设备有关于加速度和旋转的相关信息.加速度的数据将包含三个轴:x,y和z(示意如下图所 示,x轴横向贯穿手机屏幕或者笔记本键盘,y轴纵向贯穿手机屏幕或 ...
- 玩转Android之加速度传感器的使用,模仿微信摇一摇
Android系统带的传感器有很多种,最常见的莫过于微信的摇一摇了,那么今天我们就来看看Anroid中传感器的使用,做一个类似于微信摇一摇的效果. OK ,废话不多说,我们就先来看看效果图吧: 当我摇 ...
- 安卓高级5 传感器和震动 模仿微信摇一摇Ui效果
效果图: 所用的Ui就三张图: 案例代码: 结构 MainActivity.java package com.example.myapp; import android.content.Intent; ...
- Sensor传感器(摇一摇)
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content ...
- Android仿iPhone晃动撤销输入功能(微信摇一摇功能)
重力传感器微信摇一摇SensorMannager自定义alertdialogSensorEventListener 很多程序中我们可能会输入长文本内容,比如短信,写便笺等,如果想一次性撤销所有的键入内 ...
- android摇一摇实现(仿微信)
这个demo模仿的是微信的摇一摇,是一个完整的demo,下载地址在最下面.下面是demo截图: 步驟: 1.手机摇动监听,首先要实现传感器接口SensorEventLi ...
- iOS开发 传感器(加速计、摇一摇、计步器)
一.传感器 1.什么是传感器传感器是一种感应\检测周围环境的一种装置, 目前已经广泛应用于智能手机上 传感器的作用用于感应\检测设备周边的信息不同类型的传感器, 检测的信息也不一样 iPhone中的下 ...
- iOS开发——高级篇——传感器(加速计、摇一摇、计步器)
一.传感器 1.什么是传感器传感器是一种感应\检测周围环境的一种装置, 目前已经广泛应用于智能手机上 传感器的作用用于感应\检测设备周边的信息不同类型的传感器, 检测的信息也不一样 iPhone中的下 ...
- Android 摇一摇 之 传感器片
要监视原始的传感器数据,你需要实现两个通过SensorEventListener接口暴露的回调方法:onAccuracyChanged()和onSensorChanged(). 传感器数据的速度值,这 ...
随机推荐
- 使用matplotlib绘制带图例的图表
#coding=utf8 from pylab import * plt.figure(figsize=(8,10), dpi=50) plt.plot(do_tow2[28:508],do_prn2 ...
- JAVA NIO 类库的异步通信框架netty和mina
Netty 和 Mina 我究竟该选择哪个? 根据我的经验,无论选择哪个,都是个正确的选择.两者各有千秋,Netty 在内存管理方面更胜一筹,综合性能也更优.但是,API 变更的管理和兼容性做的不是太 ...
- Failed to allocate the network(s), not rescheduling
Failed to allocate the network(s), not rescheduling 在计算节点的/etc/nova/nova.conf中添加下面两句 #Fail instance ...
- Merge Cells for DataGrid 合并单元格
只适合不分页的固定行列的表格 <script type="text/javascript"> function onLoadSuccess(data){ var mer ...
- Android实例-手机震动(XE8+小米2)
相关资料:http://blog.csdn.net/laorenshen/article/details/41148843 结果: 1.打开Vibrate权限为True. 2.规律震动我没感觉出来,有 ...
- mybatis中为sql中传值#{}和${}的区别
在mybatis中,配置文件中sql的值,用#{}表示,例如: <select id="getTeacher" resultType="Teacher"& ...
- Serializable 序列化为字符串 base64
工具类 Base64.java import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import ja ...
- 山东理工大学ACM平台题答案关于C语言 1181 C语言实验——最小公倍数和最大公约数
C语言实验——最小公倍数和最大公约数 Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 从键盘输入两个正整数,求这两个正整数的最小公 ...
- [置顶] 很荣幸被选为2013年度 CSDN博客之星评选,如果觉得我的文章可以,请投我一票!
亲爱的小伙伴们,很荣幸我被选为<2013年度CSDN博客之星候选人>,希望大家多多支持,geekguy会继续努力,为大家奉献更好的文章. 投票地址:http://vote.blog.csd ...
- 剑指OFFER之跳台阶(九度OJ1388)
题目描述: 一只青蛙一次可以跳上1级台阶,也可以跳上2级.求该青蛙跳上一个n级的台阶总共有多少种跳法. 输入: 输入可能包含多个测试样例,对于每个测试案例, 输入包括一个整数n(1<=n< ...