先上代码再说话!
清单文件加权限:
<uses-permission android:name="android.permission.VIBRATE"/>
布局文件:
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:id="@+id/container" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 tools:context="com.example.shake.MainActivity" 7 tools:ignore="MergeRootFrame" 8 android:orientation="vertical"> 9 10 <ImageView 11 android:id="@+id/iv" 12 android:layout_width="match_parent" 13 android:layout_height="match_parent" 14 android:scaleType="fitXY" 15 android:src="@drawable/yaoyao" 16 /> 17 </LinearLayout>
代码:
1 package com.example.shake; 2 3 import java.io.IOException; 4 5 import android.R.id; 6 import android.app.Activity; 7 import android.app.ActionBar; 8 import android.app.Fragment; 9 import android.graphics.Bitmap; 10 import android.graphics.BitmapFactory; 11 import android.hardware.Sensor; 12 import android.hardware.SensorEvent; 13 import android.hardware.SensorEventListener; 14 import android.hardware.SensorManager; 15 import android.os.Bundle; 16 import android.os.Vibrator; 17 import android.util.Log; 18 import android.view.LayoutInflater; 19 import android.view.Menu; 20 import android.view.MenuItem; 21 import android.view.View; 22 import android.view.ViewGroup; 23 import android.widget.ImageView; 24 import android.os.Build; 25 26 public class MainActivity extends Activity { 27 28 private SensorManager sm; 29 private Sensor sensor; 30 private MyListener listener; 31 32 private ImageView iv; 33 34 private int flag = 1; //用于切图,打个标记,根据不同的flag调用不同图片 35 36 @Override 37 protected void onCreate(Bundle savedInstanceState) { 38 super.onCreate(savedInstanceState); 39 setContentView(R.layout.activity_main); 40 41 iv = (ImageView) findViewById(R.id.iv); 42 43 //创建传感器管理者 44 sm = (SensorManager) getSystemService(SENSOR_SERVICE); 45 //创建传感器 46 sensor = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); 47 listener = new MyListener(); 48 sm.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_GAME); //SensorManager.SENSOR_DELAY_GAME表示传感器灵敏度设置 49 50 51 } 52 53 private class MyListener implements SensorEventListener{ 54 55 @Override 56 public void onSensorChanged(SensorEvent event) { 57 float [] value = event.values; 58 Log.v("1", value[0]+""); //打印log看下传感器值 59 //value[0] 60 if(value[0] > 17){ 61 System.out.println(flag); 62 f(); 63 BitmapFactory.Options options = new BitmapFactory.Options(); 64 Bitmap bitmap; 65 if(flag == 1){ 66 try { 67 bitmap = BitmapFactory.decodeStream(getAssets().open("1.jpg"), null, options); 68 iv.setImageBitmap(bitmap); 69 flag++; 70 71 return; 72 } catch (IOException e) { 73 // TODO Auto-generated catch block 74 e.printStackTrace(); 75 } 76 }else if(flag == 2){ 77 try { 78 bitmap = BitmapFactory.decodeStream(getAssets().open("22.jpg"), null, options); 79 iv.setImageBitmap(bitmap); 80 flag++; 81 return; 82 } catch (IOException e) { 83 // TODO Auto-generated catch block 84 e.printStackTrace(); 85 } 86 87 }else if(flag == 3){ 88 try { 89 //getAssets().open("33.jpg")调用assets中的图片文件 90 //把文件流转化成bitmap位图 91 bitmap = BitmapFactory.decodeStream(getAssets().open("33.jpg"), null, options); 92 iv.setImageBitmap(bitmap); 93 flag = 1; 94 return; 95 } catch (IOException e) { 96 // TODO Auto-generated catch block 97 e.printStackTrace(); 98 } 99 } 100 101 102 103 104 } 105 106 } 107 108 @Override 109 public void onAccuracyChanged(Sensor sensor, int accuracy) { 110 // TODO Auto-generated method stub 111 112 } 113 114 } 115 116 //震动 117 private void f(){ 118 Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE); 119 long[] pattern = {100, 1000, 50, 30}; // OFF/ON/OFF/ON... 120 vibrator.vibrate(pattern, -1);//-1不重复,非-1为从pattern的指定下标开始重复 121 } 122 123 @Override 124 protected void onDestroy() { 125 sm.unregisterListener(listener); 126 super.onDestroy(); 127 } 128 129 130 131 } 132
目前的问题是,在摇动终端的时候,震动没问题,但切图有时过快,摇一下切了两张图。待排查。
时间: 2024-10-21 12:08:38