Start with the code and talk!
Manifest file Plus permissions:
<uses-permission android:name= "Android.permission.VIBRATE"/>
Layout file:
1<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"2Xmlns:tools= "Http://schemas.android.com/tools"3Android:id= "@+id/container"4Android:layout_width= "Match_parent"5android:layout_height= "Match_parent"6tools:context= "Com.example.shake.MainActivity"7Tools:ignore= "Mergerootframe"8android:orientation= "Vertical" >9 Ten<ImageView OneAndroid:id= "@+id/iv" AAndroid:layout_width= "Match_parent" -android:layout_height= "Match_parent" -Android:scaletype= "Fitxy" theandroid:src= "@drawable/yaoyao" -/> -</LinearLayout>
Code:
1 PackageCom.example.shake;2 3 Importjava.io.IOException;4 5 ImportAndroid. R.id;6 Importandroid.app.Activity;7 ImportAndroid.app.ActionBar;8 Importandroid.app.Fragment;9 ImportAndroid.graphics.Bitmap;Ten Importandroid.graphics.BitmapFactory; One ImportAndroid.hardware.Sensor; A Importandroid.hardware.SensorEvent; - ImportAndroid.hardware.SensorEventListener; - ImportAndroid.hardware.SensorManager; the ImportAndroid.os.Bundle; - ImportAndroid.os.Vibrator; - ImportAndroid.util.Log; - ImportAndroid.view.LayoutInflater; + ImportAndroid.view.Menu; - ImportAndroid.view.MenuItem; + ImportAndroid.view.View; A ImportAndroid.view.ViewGroup; at ImportAndroid.widget.ImageView; - ImportAndroid.os.Build; - - Public classMainactivityextendsActivity { - - PrivateSensormanager sm; in Privatesensor sensor; - PrivateMyListener Listener; to + PrivateImageView IV; - the Private intflag = 1;//used to cut graphs, make a mark, call different pictures according to different flags * $ @OverridePanax Notoginseng protected voidonCreate (Bundle savedinstancestate) { - Super. OnCreate (savedinstancestate); the Setcontentview (r.layout.activity_main); + AIV =(ImageView) Findviewbyid (R.ID.IV); the + //Create a sensor manager -SM =(Sensormanager) Getsystemservice (sensor_service); $ //Create a sensor $Sensor =sm.getdefaultsensor (sensor.type_accelerometer); -Listener =NewMyListener (); -Sm.registerlistener (listener, sensor, sensormanager.sensor_delay_game);//Sensormanager.sensor_delay_game indicates sensor sensitivity setting the - Wuyi } the - Private classMyListenerImplementssensoreventlistener{ Wu - @Override About Public voidonsensorchanged (Sensorevent event) { $ float[] Value =event.values; -LOG.V ("1", value[0]+ "");//Print log to see the sensor value - //Value[0] - if(Value[0] > 17){ A System.out.println (flag); + f (); theBitmapfactory.options Options =Newbitmapfactory.options (); - Bitmap Bitmap; $ if(Flag = = 1){ the Try { theBitmap = Bitmapfactory.decodestream (Getassets (). Open ("1.jpg"),NULL, options); the Iv.setimagebitmap (bitmap); theflag++; - in return; the}Catch(IOException e) { the //TODO auto-generated Catch block About e.printstacktrace (); the } the}Else if(Flag = = 2){ the Try { +Bitmap = Bitmapfactory.decodestream (Getassets (). Open ("22.jpg"),NULL, options); - Iv.setimagebitmap (bitmap); theflag++; Bayi return; the}Catch(IOException e) { the //TODO auto-generated Catch block - e.printstacktrace (); - } the the}Else if(Flag = = 3){ the Try { the //getassets (). Open ("33.jpg") calls the picture file in assets - //Convert file stream to bitmap bitmap theBitmap = Bitmapfactory.decodestream (Getassets (). Open ("33.jpg"),NULL, options); the Iv.setimagebitmap (bitmap); theFlag = 1; 94 return; the}Catch(IOException e) { the //TODO auto-generated Catch block the e.printstacktrace ();98 } About } - 101 102 103 104 } the 106 }107 108 @Override109 Public voidOnaccuracychanged (sensor sensor,intaccuracy) { the //TODO auto-generated Method Stub111 the }113 the } the the //Vibration117 Private voidf () {118Vibrator Vibrator =(Vibrator) Getsystemservice (vibrator_service);119 Long[] pattern = {100, 1000, 50, 30};//off/on/off/on ... -Vibrator.vibrate (Pattern,-1);//-1 not repeat, non-1 to start repeating the specified subscript from pattern121 }122 123 @Override124 protected voidOnDestroy () { the Sm.unregisterlistener (listener);126 Super. OnDestroy ();127 } - 129 the 131 } the
The current problem is that when shaking the terminal, the vibration is not a problem, but transduction sometimes too fast, shake cut two pictures. Pending troubleshooting.
Shake a roll and cut the picture. apk