안 드 로 이 드 중력 감지 개발 의 위 챗 흔 들 기 기능

이 인 스 턴 스 는 주로 안 드 로 이 드 의 중력 감지 기능 을 사용 하고 애니메이션 효과 와 결합 하여 이 루어 집 니 다.주요 코드 는 다음 과 같 습 니 다.
첫째:Shake Activity 주 클래스:

package com.android.shake; 
 
import java.io.IOException; 
import java.util.HashMap; 
import android.app.Activity; 
import android.media.AudioManager; 
import android.media.SoundPool; 
import android.os.Bundle; 
import android.os.Handler; 
import android.os.Vibrator; 
import android.view.View; 
import android.view.animation.Animation; 
import android.view.animation.AnimationSet; 
import android.view.animation.TranslateAnimation; 
import android.widget.Button; 
import android.widget.RelativeLayout; 
import android.widget.SlidingDrawer; 
import android.widget.SlidingDrawer.OnDrawerCloseListener; 
import android.widget.SlidingDrawer.OnDrawerOpenListener; 
import android.widget.Toast; 
 
import com.android.shake.ShakeListener.OnShakeListener; 
 
public class ShakeActivity extends Activity{ 
 
 ShakeListener mShakeListener = null; 
 Vibrator mVibrator; 
 private RelativeLayout mImgUp; 
 private RelativeLayout mImgDn; 
 private RelativeLayout mTitle; 
 
 private SlidingDrawer mDrawer; 
 private Button mDrawerBtn; 
 private SoundPool sndPool; 
 private HashMap<Integer, Integer> soundPoolMap = new HashMap<Integer, Integer>(); 
 
 @Override 
 public void onCreate(Bundle savedInstanceState) { 
 // TODO Auto-generated method stub 
 super.onCreate(savedInstanceState); 
 setContentView(R.layout.shake_activity); 
 //drawerSet ();//   drawer            
 
 mVibrator = (Vibrator)getApplication().getSystemService(VIBRATOR_SERVICE); 
 
 mImgUp = (RelativeLayout) findViewById(R.id.shakeImgUp); 
 mImgDn = (RelativeLayout) findViewById(R.id.shakeImgDown); 
 mTitle = (RelativeLayout) findViewById(R.id.shake_title_bar); 
 
 mDrawer = (SlidingDrawer) findViewById(R.id.slidingDrawer1); 
 mDrawerBtn = (Button) findViewById(R.id.handle); 
 mDrawer.setOnDrawerOpenListener(new OnDrawerOpenListener() 
 { public void onDrawerOpened() 
 { 
 mDrawerBtn.setBackgroundDrawable(getResources().getDrawable(R.drawable.shake_report_dragger_down)); 
 TranslateAnimation titleup = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,-1.0f); 
 titleup.setDuration(200); 
 titleup.setFillAfter(true); 
 mTitle.startAnimation(titleup); 
 } 
 }); 
 /*   SlidingDrawer         */ 
 mDrawer.setOnDrawerCloseListener(new OnDrawerCloseListener() 
 { public void onDrawerClosed() 
 { 
 mDrawerBtn.setBackgroundDrawable(getResources().getDrawable(R.drawable.shake_report_dragger_up)); 
 TranslateAnimation titledn = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,-1.0f,Animation.RELATIVE_TO_SELF,0f); 
 titledn.setDuration(200); 
 titledn.setFillAfter(false); 
 mTitle.startAnimation(titledn); 
 } 
 }); 
 loadSound() ; 
 mShakeListener = new ShakeListener(this); 
 mShakeListener.setOnShakeListener(new OnShakeListener() { 
 public void onShake() { 
 //Toast.makeText(getApplicationContext(), "  ,                。
!", Toast.LENGTH_SHORT).show(); startAnim(); // mShakeListener.stop(); sndPool.play(soundPoolMap.get(0), (float) 1, (float) 1, 0, 0,(float) 1.2); new Handler().postDelayed(new Runnable(){ public void run(){ //Toast.makeText(getApplicationContext(), " ,

!", 500).setGravity(Gravity.CENTER,0,0).show(); sndPool.play(soundPoolMap.get(1), (float) 1, (float) 1, 0, 0,(float) 1.0); Toast mtoast; mtoast = Toast.makeText(getApplicationContext(), " ,

!", 10); //mtoast.setGravity(Gravity.CENTER, 0, 0); mtoast.show(); mVibrator.cancel(); mShakeListener.start(); } }, 2000); } }); } private void loadSound() { sndPool = new SoundPool(2, AudioManager.STREAM_SYSTEM, 5); new Thread() { public void run() { try { soundPoolMap.put( 0, sndPool.load(getAssets().openFd( "sound/shake_sound_male.mp3"), 1)); soundPoolMap.put( 1, sndPool.load(getAssets().openFd( "sound/shake_match.mp3"), 1)); } catch (IOException e) { e.printStackTrace(); } } }.start(); } public void startAnim () { // AnimationSet animup = new AnimationSet(true); TranslateAnimation mytranslateanimup0 = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,-0.5f); mytranslateanimup0.setDuration(1000); TranslateAnimation mytranslateanimup1 = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,+0.5f); mytranslateanimup1.setDuration(1000); mytranslateanimup1.setStartOffset(1000); animup.addAnimation(mytranslateanimup0); animup.addAnimation(mytranslateanimup1); mImgUp.startAnimation(animup); AnimationSet animdn = new AnimationSet(true); TranslateAnimation mytranslateanimdn0 = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,+0.5f); mytranslateanimdn0.setDuration(1000); TranslateAnimation mytranslateanimdn1 = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,-0.5f); mytranslateanimdn1.setDuration(1000); mytranslateanimdn1.setStartOffset(1000); animdn.addAnimation(mytranslateanimdn0); animdn.addAnimation(mytranslateanimdn1); mImgDn.startAnimation(animdn); } public void startVibrato(){ // mVibrator.vibrate( new long[]{500,200,500,200}, -1); // {} , ,-1 , -1 pattern } public void shake_activity_back(View v) { // this.finish(); } public void linshi(View v) { // startAnim(); } @Override protected void onDestroy() { super.onDestroy(); if (mShakeListener != null) { mShakeListener.stop(); } } }
코드:

package com.android.shake; 
 
import java.io.IOException; 
import java.util.HashMap; 
import android.app.Activity; 
import android.media.AudioManager; 
import android.media.SoundPool; 
import android.os.Bundle; 
import android.os.Handler; 
import android.os.Vibrator; 
import android.view.View; 
import android.view.animation.Animation; 
import android.view.animation.AnimationSet; 
import android.view.animation.TranslateAnimation; 
import android.widget.Button; 
import android.widget.RelativeLayout; 
import android.widget.SlidingDrawer; 
import android.widget.SlidingDrawer.OnDrawerCloseListener; 
import android.widget.SlidingDrawer.OnDrawerOpenListener; 
import android.widget.Toast; 
 
import com.android.shake.ShakeListener.OnShakeListener; 
 
public class ShakeActivity extends Activity{ 
 
 ShakeListener mShakeListener = null; 
 Vibrator mVibrator; 
 private RelativeLayout mImgUp; 
 private RelativeLayout mImgDn; 
 private RelativeLayout mTitle; 
 
 private SlidingDrawer mDrawer; 
 private Button mDrawerBtn; 
 private SoundPool sndPool; 
 private HashMap<Integer, Integer> soundPoolMap = new HashMap<Integer, Integer>(); 
 
 @Override 
 public void onCreate(Bundle savedInstanceState) { 
 // TODO Auto-generated method stub 
 super.onCreate(savedInstanceState); 
 setContentView(R.layout.shake_activity); 
 //drawerSet ();//   drawer            
 
 mVibrator = (Vibrator)getApplication().getSystemService(VIBRATOR_SERVICE); 
 
 mImgUp = (RelativeLayout) findViewById(R.id.shakeImgUp); 
 mImgDn = (RelativeLayout) findViewById(R.id.shakeImgDown); 
 mTitle = (RelativeLayout) findViewById(R.id.shake_title_bar); 
 
 mDrawer = (SlidingDrawer) findViewById(R.id.slidingDrawer1); 
 mDrawerBtn = (Button) findViewById(R.id.handle); 
 mDrawer.setOnDrawerOpenListener(new OnDrawerOpenListener() 
 { public void onDrawerOpened() 
 { 
 mDrawerBtn.setBackgroundDrawable(getResources().getDrawable(R.drawable.shake_report_dragger_down)); 
 TranslateAnimation titleup = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,-1.0f); 
 titleup.setDuration(200); 
 titleup.setFillAfter(true); 
 mTitle.startAnimation(titleup); 
 } 
 }); 
 /*   SlidingDrawer         */ 
 mDrawer.setOnDrawerCloseListener(new OnDrawerCloseListener() 
 { public void onDrawerClosed() 
 { 
 mDrawerBtn.setBackgroundDrawable(getResources().getDrawable(R.drawable.shake_report_dragger_up)); 
 TranslateAnimation titledn = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,-1.0f,Animation.RELATIVE_TO_SELF,0f); 
 titledn.setDuration(200); 
 titledn.setFillAfter(false); 
 mTitle.startAnimation(titledn); 
 } 
 }); 
 loadSound() ; 
 mShakeListener = new ShakeListener(this); 
 mShakeListener.setOnShakeListener(new OnShakeListener() { 
 public void onShake() { 
 //Toast.makeText(getApplicationContext(), "  ,                。
!", Toast.LENGTH_SHORT).show(); startAnim(); // mShakeListener.stop(); sndPool.play(soundPoolMap.get(0), (float) 1, (float) 1, 0, 0,(float) 1.2); new Handler().postDelayed(new Runnable(){ public void run(){ //Toast.makeText(getApplicationContext(), " ,

!", 500).setGravity(Gravity.CENTER,0,0).show(); sndPool.play(soundPoolMap.get(1), (float) 1, (float) 1, 0, 0,(float) 1.0); Toast mtoast; mtoast = Toast.makeText(getApplicationContext(), " ,

!", 10); //mtoast.setGravity(Gravity.CENTER, 0, 0); mtoast.show(); mVibrator.cancel(); mShakeListener.start(); } }, 2000); } }); } private void loadSound() { sndPool = new SoundPool(2, AudioManager.STREAM_SYSTEM, 5); new Thread() { public void run() { try { soundPoolMap.put( 0, sndPool.load(getAssets().openFd( "sound/shake_sound_male.mp3"), 1)); soundPoolMap.put( 1, sndPool.load(getAssets().openFd( "sound/shake_match.mp3"), 1)); } catch (IOException e) { e.printStackTrace(); } } }.start(); } public void startAnim () { // AnimationSet animup = new AnimationSet(true); TranslateAnimation mytranslateanimup0 = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,-0.5f); mytranslateanimup0.setDuration(1000); TranslateAnimation mytranslateanimup1 = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,+0.5f); mytranslateanimup1.setDuration(1000); mytranslateanimup1.setStartOffset(1000); animup.addAnimation(mytranslateanimup0); animup.addAnimation(mytranslateanimup1); mImgUp.startAnimation(animup); AnimationSet animdn = new AnimationSet(true); TranslateAnimation mytranslateanimdn0 = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,+0.5f); mytranslateanimdn0.setDuration(1000); TranslateAnimation mytranslateanimdn1 = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,-0.5f); mytranslateanimdn1.setDuration(1000); mytranslateanimdn1.setStartOffset(1000); animdn.addAnimation(mytranslateanimdn0); animdn.addAnimation(mytranslateanimdn1); mImgDn.startAnimation(animdn); } public void startVibrato(){ // mVibrator.vibrate( new long[]{500,200,500,200}, -1); // {} , ,-1 , -1 pattern } public void shake_activity_back(View v) { // this.finish(); } public void linshi(View v) { // startAnim(); } @Override protected void onDestroy() { super.onDestroy(); if (mShakeListener != null) { mShakeListener.stop(); } } }
 두 번 째:핸드폰 의 흔 들 림 을 감지 하 는 모니터 류 Shake Listener,코드 는 다음 과 같 습 니 다.

package com.android.shake; 
 
import android.content.Context; 
import android.hardware.Sensor; 
import android.hardware.SensorEvent; 
import android.hardware.SensorEventListener; 
import android.hardware.SensorManager; 
import android.util.Log; 
 
/** 
 *              
 */ 
public class ShakeListener implements SensorEventListener { 
 //     ,               
 private static final int SPEED_SHRESHOLD = 2000; 
 //           
 private static final int UPTATE_INTERVAL_TIME = 70; 
 //        
 private SensorManager sensorManager; 
 //     
 private Sensor sensor; 
 //         
 private OnShakeListener onShakeListener; 
 //     
 private Context mContext; 
 //                
 private float lastX; 
 private float lastY; 
 private float lastZ; 
 //        
 private long lastUpdateTime; 
 
 //     
 public ShakeListener(Context c) { 
 //        
 mContext = c; 
 start(); 
 } 
 
 //    
 public void start() { 
 //          
 sensorManager = (SensorManager) mContext 
 .getSystemService(Context.SENSOR_SERVICE); 
 if (sensorManager != null) { 
 //         
 sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); 
 } 
 //    
 if (sensor != null) { 
 sensorManager.registerListener(this, sensor, 
  SensorManager.SENSOR_DELAY_GAME); 
 } 
 
 } 
 
 //      
 public void stop() { 
 sensorManager.unregisterListener(this); 
 } 
 
 //           
 public void setOnShakeListener(OnShakeListener listener) { 
 onShakeListener = listener; 
 } 
 
 //               
 public void onSensorChanged(SensorEvent event) { 
 //        
 long currentUpdateTime = System.currentTimeMillis(); 
 //           
 long timeInterval = currentUpdateTime - lastUpdateTime; 
 //               
 if (timeInterval < UPTATE_INTERVAL_TIME) 
 return; 
 //        last   
 lastUpdateTime = currentUpdateTime; 
 
 //   x,y,z   
 float x = event.values[0]; 
 float y = event.values[1]; 
 float z = event.values[2]; 
 
 //   x,y,z     
 float deltaX = x - lastX; 
 float deltaY = y - lastY; 
 float deltaZ = z - lastZ; 
 
 //         last   
 lastX = x; 
 lastY = y; 
 lastZ = z; 
 
 double speed = Math.sqrt(deltaX * deltaX + deltaY * deltaY + deltaZ 
 * deltaZ) 
 / timeInterval * 10000; 
 //       ,     
 if (speed >= SPEED_SHRESHOLD) { 
 onShakeListener.onShake(); 
 } 
 } 
 
 public void onAccuracyChanged(Sensor sensor, int accuracy) { 
 
 } 
 
 //        
 public interface OnShakeListener { 
 public void onShake(); 
 } 
 
} 
코드:

package com.android.shake; 
 
import android.content.Context; 
import android.hardware.Sensor; 
import android.hardware.SensorEvent; 
import android.hardware.SensorEventListener; 
import android.hardware.SensorManager; 
import android.util.Log; 
 
/** 
 *              
 */ 
public class ShakeListener implements SensorEventListener { 
 //     ,               
 private static final int SPEED_SHRESHOLD = 2000; 
 //           
 private static final int UPTATE_INTERVAL_TIME = 70; 
 //        
 private SensorManager sensorManager; 
 //     
 private Sensor sensor; 
 //         
 private OnShakeListener onShakeListener; 
 //     
 private Context mContext; 
 //                
 private float lastX; 
 private float lastY; 
 private float lastZ; 
 //        
 private long lastUpdateTime; 
 
 //     
 public ShakeListener(Context c) { 
 //        
 mContext = c; 
 start(); 
 } 
 
 //    
 public void start() { 
 //          
 sensorManager = (SensorManager) mContext 
 .getSystemService(Context.SENSOR_SERVICE); 
 if (sensorManager != null) { 
 //         
 sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); 
 } 
 //    
 if (sensor != null) { 
 sensorManager.registerListener(this, sensor, 
  SensorManager.SENSOR_DELAY_GAME); 
 } 
 
 } 
 
 //      
 public void stop() { 
 sensorManager.unregisterListener(this); 
 } 
 
 //           
 public void setOnShakeListener(OnShakeListener listener) { 
 onShakeListener = listener; 
 } 
 
 //               
 public void onSensorChanged(SensorEvent event) { 
 //        
 long currentUpdateTime = System.currentTimeMillis(); 
 //           
 long timeInterval = currentUpdateTime - lastUpdateTime; 
 //               
 if (timeInterval < UPTATE_INTERVAL_TIME) 
 return; 
 //        last   
 lastUpdateTime = currentUpdateTime; 
 
 //   x,y,z   
 float x = event.values[0]; 
 float y = event.values[1]; 
 float z = event.values[2]; 
 
 //   x,y,z     
 float deltaX = x - lastX; 
 float deltaY = y - lastY; 
 float deltaZ = z - lastZ; 
 
 //         last   
 lastX = x; 
 lastY = y; 
 lastZ = z; 
 
 double speed = Math.sqrt(deltaX * deltaX + deltaY * deltaY + deltaZ 
 * deltaZ) 
 / timeInterval * 10000; 
 //       ,     
 if (speed >= SPEED_SHRESHOLD) { 
 onShakeListener.onShake(); 
 } 
 } 
 
 public void onAccuracyChanged(Sensor sensor, int accuracy) { 
 
 } 
 
 //        
 public interface OnShakeListener { 
 public void onShake(); 
 } 
 
} 
 셋째:AndroidManifest.xml 에 권한 을 추가 해 야 합 니 다.옆으로 흔 들 리 지 않 을 때 감청 할 수 없습니다.

<uses-permission android:name="android.hardware.sensor.accelerometer"/>
제4:효과 도:

이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기