标签:ati button 控件 orm prot case 管理 XML public
今天在网上学习了几个新的知识点,一个是传感器,一个是添加音效池,一个是实现震动的效果。
首先,安卓有很多自带的传感器,有加速度传感器等等,
private SensorManager sensorManager;
private Sensor sensor;
...
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
sensor = sensorManager.getDefaultSensor(Sensor.TYPE_GAME_ROTATION_VECTOR);
这个是安卓官方文件给出的一个例子,添加音效池和添加震动的方法是差不多的;下面是一个实现微信摇一摇功能的APP的代码:
1 package com.example.hp.wechat_shake; 2 3 import android.content.Intent; 4 import android.os.Bundle; 5 import android.support.v7.app.ActionBarActivity; 6 import android.view.View; 7 import android.widget.Button; 8 9 public class MainActivity extends ActionBarActivity implements View.OnClickListener { 10 11 private Button mButton; 12 13 @Override 14 protected void onCreate(Bundle savedInstanceState) { 15 super.onCreate(savedInstanceState); 16 setContentView(R.layout.activity_main); 17 18 //初始化控件 19 initUI(); 20 21 } 22 private void initUI() { 23 mButton = (Button) findViewById(R.id.btn_shake); 24 findViewById(R.id.btn_shake).setOnClickListener(this); 25 } 26 27 @Override 28 public void onClick(View view) { 29 Intent intent = new Intent(); 30 switch(view.getId()){ 31 case R.id.btn_shake: 32 intent.setClass(this,ShakeActivity.class); 33 break; 34 } 35 startActivity(intent); 36 } 37 }
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 tools:context="com.example.hp.wechat_shake.MainActivity"> 8 9 <Button 10 android:id="@+id/btn_shake" 11 android:text="微信摇一摇" 12 android:textSize="30dp" 13 android:layout_width="match_parent" 14 android:layout_height="65dp"/> 15 </RelativeLayout>
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 android:background="#000" 8 tools:context="com.example.hp.wechat_shake.ShakeActivity"> 9 10 <ImageView 11 android:id="@+id/iv_shake_img" 12 android:layout_width="wrap_content" 13 android:layout_height="wrap_content" 14 android:src="@drawable/shakehideimg_man2" 15 android:layout_centerInParent="true"/> 16 17 <LinearLayout 18 android:id="@+id/id_shake_layout" 19 android:layout_width="match_parent" 20 android:layout_height="match_parent" 21 android:orientation="vertical" 22 android:gravity="center"> 23 24 <ImageView 25 android:src="@drawable/shake_logo_up" 26 android:id="@+id/iv_shake_up" 27 android:layout_width="wrap_content" 28 android:layout_height="wrap_content"/> 29 30 <ImageView 31 android:src="@drawable/shake_logo_down" 32 android:id="@+id/iv_shake_down" 33 android:layout_width="wrap_content" 34 android:layout_height="wrap_content"/> 35 36 </LinearLayout> 37 38 </RelativeLayout>
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest package="com.example.hp.wechat_shake" 3 xmlns:android="http://schemas.android.com/apk/res/android"> 4 /*******振动器使用权限**********/ 5 <uses-permission android:name="android.permission.VIBRATE"/> 6 7 <application 8 android:allowBackup="true" 9 android:icon="@mipmap/ic_launcher" 10 android:label="@string/app_name" 11 android:supportsRtl="true" 12 android:theme="@style/AppTheme"> 13 <activity android:name=".MainActivity"> 14 <intent-filter> 15 <action android:name="android.intent.action.MAIN"/> 16 17 <category android:name="android.intent.category.LAUNCHER"/> 18 </intent-filter> 19 </activity> 20 <activity android:name=".ShakeActivity"> 21 </activity> 22 </application> 23 24 </manifest>
1 package com.example.hp.wechat_shake; 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 import android.media.AudioManager; 9 import android.media.SoundPool; 10 import android.os.Bundle; 11 import android.os.Vibrator; 12 import android.support.v7.app.ActionBarActivity; 13 import android.view.animation.Animation; 14 import android.view.animation.AnimationSet; 15 import android.view.animation.TranslateAnimation; 16 import android.widget.ImageView; 17 18 public class ShakeActivity extends ActionBarActivity implements SensorEventListener{ 19 20 private ImageView mShakeImg; 21 private ImageView mShakeUp; 22 private ImageView mShakeDown; 23 private SensorManager mSensorManager; 24 private Sensor mSensor; 25 private AnimationSet mSetUp; 26 private AnimationSet mSetDown; 27 private SoundPool mSoundPool; 28 private int loadId; 29 private Vibrator mVibrator; 30 31 @Override 32 protected void onCreate(Bundle savedInstanceState) { 33 super.onCreate(savedInstanceState); 34 setContentView(R.layout.activity_shake); 35 //初始化控件 36 initUI(); 37 //初始化动画 38 initAnimation(); 39 //初始化音效池 40 initSoundPool(); 41 //初始化振动器 42 initVibrator(); 43 44 } 45 46 private void initVibrator() { 47 //获取振动器管理者对象 48 mVibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); 49 } 50 51 private void initSoundPool() { 52 //获取音效池对象 53 mSoundPool = new SoundPool(2, AudioManager.STREAM_MUSIC,0);//2代表音效池最多下载两个播放器,中间的参数代表播放器的类型,最后一个0代表源文件的一个优先级 54 //加载音效到音效池当中 55 loadId=mSoundPool.load(this,R.raw.awe,1); 56 57 } 58 59 private void initUI() { 60 mShakeImg = (ImageView) findViewById(R.id.iv_shake_img); 61 mShakeUp = (ImageView) findViewById(R.id.iv_shake_up); 62 mShakeDown = (ImageView) findViewById(R.id.iv_shake_down); 63 //1.获取传感器管理者对象 64 mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); 65 //2.获取指定的传感器对象---加速度传感器 66 mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); 67 //3.注册传感器对象 68 mSensorManager.registerListener(this,mSensor,SensorManager.SENSOR_DELAY_NORMAL); 69 70 } 71 72 @Override 73 protected void onDestroy() { 74 //4.注销传感器对象 75 super.onDestroy(); 76 mSensorManager.unregisterListener(this); 77 } 78 79 @Override 80 public void onSensorChanged(SensorEvent sensorEvent) { 81 /*当传感器的数值发生改变时会调用的方法(函数)*/ 82 float[] values = sensorEvent.values; 83 float x = values[0]; 84 float y = values[1]; 85 float z = values[3]; 86 int minValue = 12; 87 if(Math.abs(x)>minValue||Math.abs(y)>minValue||Math.abs(z)>minValue){ 88 //开始震动 89 long[]patten = {300,500}; 90 mVibrator.vibrate(patten,-1); 91 92 //开始播放音效 93 mSoundPool.play(loadId,1,1,1,0,1); 94 95 96 //开始动画效果 97 mShakeUp.startAnimation(mSetUp); 98 mShakeDown.startAnimation(mSetDown); 99 100 } 101 } 102 103 @Override 104 public void onAccuracyChanged(Sensor sensor, int i) { 105 /*当传感器的精度发生改变时会调用的方法*/ 106 } 107 108 private void initAnimation(){ 109 //上面图片对应的动画效果 110 TranslateAnimation mAnimationUp = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,1); 111 mAnimationUp.setDuration(500);//设置上面的图片向上的时间为5秒 112 TranslateAnimation mAnimationUpDown = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,1,Animation.RELATIVE_TO_SELF,0); 113 mAnimationUpDown.setDuration(500); 114 mSetUp = new AnimationSet(true); 115 //将上面的一张图片的上下移动的动画效果放到一个集合里 116 mSetUp.addAnimation(mAnimationUp); 117 mSetUp.addAnimation(mAnimationUpDown); 118 //设置动画之间的执行时间差 119 mSetUp.setStartOffset(500); 120 121 TranslateAnimation mAnimationDown = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,-1); 122 mAnimationDown.setDuration(500);//设置上面的图片向上的时间为5秒 123 TranslateAnimation mAnimationDownUp = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,-1,Animation.RELATIVE_TO_SELF,0); 124 mAnimationDownUp.setDuration(500); 125 mSetDown = new AnimationSet(true); 126 //将下面的一张图片的上下移动的动画效果放到一个集合里 127 mSetDown.addAnimation(mAnimationDown); 128 mSetDown.addAnimation(mAnimationDownUp); 129 //设置动画之间的执行时间差 130 mSetDown.setStartOffset(500); 131 132 } 133 }
标签:ati button 控件 orm prot case 管理 XML public
原文地址:https://www.cnblogs.com/mxk123456/p/12398998.html