Android Studio学习路程(13)

愿君学长松,慎勿作桃李。这篇文章主要讲述Android Studio学习路程(13)相关的知识,希望能为你提供帮助。
今天在网上学习了几个新的知识点,一个是传感器,一个是添加音效池,一个是实现震动的效果。
首先,安卓有很多自带的传感器,有加速度传感器等等,

private SensorManager sensorManager;
private Sensor sensor;
...
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
sensor = sensorManager.getDefaultSensor(Sensor.TYPE_GAME_ROTATION_VECTOR);
这个是安卓官方文件给出的一个例子,添加音效池和添加震动的方法是差不多的;下面是一个实现微信摇一摇功能的APP的代码:

Android Studio学习路程(13)

文章图片
Android Studio学习路程(13)

文章图片
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 11private Button mButton; 12 13@Override 14protected void onCreate(Bundle savedInstanceState) { 15super.onCreate(savedInstanceState); 16setContentView(R.layout.activity_main); 17 18//初始化控件 19initUI(); 20 21} 22private void initUI() { 23mButton = (Button) findViewById(R.id.btn_shake); 24findViewById(R.id.btn_shake).setOnClickListener(this); 25} 26 27@Override 28public void onClick(View view) { 29Intent intent = new Intent(); 30switch(view.getId()){ 31case R.id.btn_shake: 32intent.setClass(this,ShakeActivity.class); 33break; 34} 35startActivity(intent); 36} 37 }

MainActivity
Android Studio学习路程(13)

文章图片
Android Studio学习路程(13)

文章图片
1 < ?xml version="1.0" encoding="utf-8"?> 2 < RelativeLayout 3xmlns:android="http://schemas.android.com/apk/res/android" 4xmlns:tools="http://schemas.android.com/tools" 5android:layout_width="match_parent" 6android:layout_height="match_parent" 7tools:context="com.example.hp.wechat_shake.MainActivity"> 8 9< Button 10android:id="@+id/btn_shake" 11android:text="微信摇一摇" 12android:textSize="30dp" 13android:layout_width="match_parent" 14android:layout_height="65dp"/> 15 < /RelativeLayout>

MainActivity_XML
Android Studio学习路程(13)

文章图片
Android Studio学习路程(13)

文章图片
1 < ?xml version="1.0" encoding="utf-8"?> 2 < RelativeLayout 3xmlns:android="http://schemas.android.com/apk/res/android" 4xmlns:tools="http://schemas.android.com/tools" 5android:layout_width="match_parent" 6android:layout_height="match_parent" 7android:background="#000" 8tools:context="com.example.hp.wechat_shake.ShakeActivity"> 9 10< ImageView 11android:id="@+id/iv_shake_img" 12android:layout_width="wrap_content" 13android:layout_height="wrap_content" 14android:src="https://www.songbingjia.com/android/@drawable/shakehideimg_man2" 15android:layout_centerInParent="true"/> 16 17< LinearLayout 18android:id="@+id/id_shake_layout" 19android:layout_width="match_parent" 20android:layout_height="match_parent" 21android:orientation="vertical" 22android:gravity="center"> 23 24< ImageView 25android:src="https://www.songbingjia.com/android/@drawable/shake_logo_up" 26android:id="@+id/iv_shake_up" 27android:layout_width="wrap_content" 28android:layout_height="wrap_content"/> 29 30< ImageView 31android:src="https://www.songbingjia.com/android/@drawable/shake_logo_down" 32android:id="@+id/iv_shake_down" 33android:layout_width="wrap_content" 34android:layout_height="wrap_content"/> 35 36< /LinearLayout> 37 38 < /RelativeLayout>

ShakeActivity_XML
Android Studio学习路程(13)

文章图片
Android Studio学习路程(13)

文章图片
1 < ?xml version="1.0" encoding="utf-8"?> 2 < manifest package="com.example.hp.wechat_shake" 3xmlns:android="http://schemas.android.com/apk/res/android"> 4/*******振动器使用权限**********/ 5< uses-permission android:name="android.permission.VIBRATE"/> 6 7< application 8android:allowBackup="true" 9android:icon="@mipmap/ic_launcher" 10android:label="@string/app_name" 11android:supportsRtl="true" 12android: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>

manifest_XML
Android Studio学习路程(13)

文章图片
Android Studio学习路程(13)

文章图片
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 20private ImageView mShakeImg; 21private ImageView mShakeUp; 22private ImageView mShakeDown; 23private SensorManager mSensorManager; 24private Sensor mSensor; 25private AnimationSet mSetUp; 26private AnimationSet mSetDown; 27private SoundPool mSoundPool; 28private int loadId; 29private Vibrator mVibrator; 30 31@Override 32protected void onCreate(Bundle savedInstanceState) { 33super.onCreate(savedInstanceState); 34setContentView(R.layout.activity_shake); 35//初始化控件 36initUI(); 37//初始化动画 38initAnimation(); 39//初始化音效池 40initSoundPool(); 41//初始化振动器 42initVibrator(); 43 44} 45 46private void initVibrator() { 47//获取振动器管理者对象 48mVibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); 49} 50 51private void initSoundPool() { 52//获取音效池对象 53mSoundPool = new SoundPool(2, AudioManager.STREAM_MUSIC,0); //2代表音效池最多下载两个播放器,中间的参数代表播放器的类型,最后一个0代表源文件的一个优先级 54//加载音效到音效池当中 55loadId=mSoundPool.load(this,R.raw.awe,1); 56 57} 58 59private void initUI() { 60mShakeImg = (ImageView) findViewById(R.id.iv_shake_img); 61mShakeUp = (ImageView) findViewById(R.id.iv_shake_up); 62mShakeDown = (ImageView) findViewById(R.id.iv_shake_down); 63//1.获取传感器管理者对象 64mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); 65//2.获取指定的传感器对象---加速度传感器 66mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); 67//3.注册传感器对象 68mSensorManager.registerListener(this,mSensor,SensorManager.SENSOR_DELAY_NORMAL); 69 70} 71 72@Override 73protected void onDestroy() { 74//4.注销传感器对象 75super.onDestroy(); 76mSensorManager.unregisterListener(this); 77} 78 79@Override 80public void onSensorChanged(SensorEvent sensorEvent) { 81/*当传感器的数值发生改变时会调用的方法(函数)*/ 82float[] values = sensorEvent.values; 83float x = values[0]; 84float y = values[1]; 85float z = values[3]; 86int minValue = https://www.songbingjia.com/android/12; 87if(Math.abs(x)> minValue||Math.abs(y)> minValue||Math.abs(z)> minValue){ 88//开始震动 89long[]patten = {300,500}; 90mVibrator.vibrate(patten,-1); 91 92//开始播放音效 93mSoundPool.play(loadId,1,1,1,0,1); 94 95 96//开始动画效果 97mShakeUp.startAnimation(mSetUp); 98mShakeDown.startAnimation(mSetDown); 99 100} 101} 102 103@Override 104public void onAccuracyChanged(Sensor sensor, int i) { 105/*当传感器的精度发生改变时会调用的方法*/ 106} 107 108private void initAnimation(){ 109//上面图片对应的动画效果 110TranslateAnimation mAnimationUp = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,1); 111mAnimationUp.setDuration(500); //设置上面的图片向上的时间为5秒 112TranslateAnimation mAnimationUpDown = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,1,Animation.RELATIVE_TO_SELF,0); 113mAnimationUpDown.setDuration(500); 114mSetUp = new AnimationSet(true); 115//将上面的一张图片的上下移动的动画效果放到一个集合里 116mSetUp.addAnimation(mAnimationUp); 117mSetUp.addAnimation(mAnimationUpDown); 118//设置动画之间的执行时间差 119mSetUp.setStartOffset(500); 120 121TranslateAnimation mAnimationDown = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,-1); 122mAnimationDown.setDuration(500); //设置上面的图片向上的时间为5秒 123TranslateAnimation mAnimationDownUp = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,-1,Animation.RELATIVE_TO_SELF,0); 124mAnimationDownUp.setDuration(500); 125mSetDown = new AnimationSet(true); 126//将下面的一张图片的上下移动的动画效果放到一个集合里 127mSetDown.addAnimation(mAnimationDown); 128mSetDown.addAnimation(mAnimationDownUp); 129//设置动画之间的执行时间差 130mSetDown.setStartOffset(500); 131 132} 133 }

ShakeActivity【Android Studio学习路程(13)】 






    推荐阅读