话不多说看代码
布局文件:
java:
package cn.com.bvin.shake;import java.io.IOException;import android.app.Activity;import android.content.Context;import android.content.res.AssetFileDescriptor;import android.content.res.AssetManager;import android.graphics.Color;import android.hardware.Sensor;import android.hardware.SensorEvent;import android.hardware.SensorEventListener;import android.hardware.SensorManager;import android.media.MediaPlayer;import android.os.Bundle;import android.util.Log;import android.view.Gravity;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup.LayoutParams;import android.view.animation.AccelerateInterpolator;import android.view.animation.Animation;import android.view.animation.Animation.AnimationListener;import android.view.animation.DecelerateInterpolator;import android.view.animation.TranslateAnimation;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.RelativeLayout;public class MainActivity extends Activity implements SensorEventListener,AnimationListener{ /** Called when the activity is first created. */ private SensorManager sMan; private Sensor sensor; private Animation topInAnim = null; private Animation topOutAnim = null; private Animation botInAnim = null; private Animation botOutAnim = null; private int duration =630; private ImageView ivTop; private ImageView ivBot; private ImageView shake_bg; private ImageView shake_line_up; private ImageView shake_line_down; private RelativeLayout shake_panel_up; private RelativeLayout shake_panel_down; private int scr_h,src_w; private MediaPlayer mPlayer; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); scr_h = getWindowManager().getDefaultDisplay().getHeight(); src_w = getWindowManager().getDefaultDisplay().getWidth(); initSensorManager(); initViews(); initAnim(); initPlayer(); } private void initPlayer(){ mPlayer = new MediaPlayer(); //mPlayer.setDataSource(context, uri); AssetManager aMan = this.getAssets(); try { AssetFileDescriptor fileDescriptor = aMan.openFd("shake_sound_male.mp3"); mPlayer.setDataSource(fileDescriptor.getFileDescriptor(),fileDescriptor.getStartOffset(),fileDescriptor.getLength()); mPlayer.prepare(); //mPlayer.start(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private void initSensorManager(){ sMan = (SensorManager)getSystemService(Context.SENSOR_SERVICE); sensor = sMan.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); sMan.registerListener(this, sensor,SensorManager.SENSOR_DELAY_NORMAL); } private void initAnim(){ topOutAnim = new TranslateAnimation(0,0,0,-(float)scr_h/4); topOutAnim.setDuration(duration); topOutAnim.setInterpolator(new DecelerateInterpolator()); botOutAnim = new TranslateAnimation(0,0,0,(float)scr_h/4); botOutAnim.setDuration(duration); botOutAnim.setInterpolator(new DecelerateInterpolator()); topInAnim = new TranslateAnimation(0,0,-(float)scr_h/4,0); topInAnim.setDuration(duration); topInAnim.setInterpolator(new AccelerateInterpolator()); botInAnim = new TranslateAnimation(0,0,(float)scr_h/4,0); botInAnim.setDuration(duration); botInAnim.setInterpolator(new AccelerateInterpolator()); botInAnim.setAnimationListener(this); botOutAnim.setAnimationListener(this); } private void initViews(){ shake_bg = (ImageView)findViewById(R.id.iv_hide); shake_panel_up = (RelativeLayout)findViewById(R.id.frameTop); shake_panel_down = (RelativeLayout)findViewById(R.id.frameBot); shake_line_up = (ImageView)findViewById(R.id.shake_line_up); shake_line_down = (ImageView)findViewById(R.id.shake_line_down); shake_line_up.setVisibility(View.INVISIBLE); shake_line_down.setVisibility(View.INVISIBLE); ivTop = (ImageView)findViewById(R.id.ivTop); ivBot = (ImageView)findViewById(R.id.ivBot); } @Override public void onAnimationEnd(Animation arg0) { // TODO Auto-generated method stub if(arg0 == botOutAnim){ //夹门打开 shake_panel_up.startAnimation(topInAnim); shake_panel_down.startAnimation(botInAnim); }else if(arg0.equals(botInAnim)){ //夹门关闭 Log.e("wn", "sdgsdg"); sMan.registerListener(this, sensor,SensorManager.SENSOR_DELAY_NORMAL); shake_line_up.setVisibility(View.INVISIBLE); shake_line_down.setVisibility(View.INVISIBLE); shake_bg.setVisibility(View.INVISIBLE); //完成动画后,再次监听加速器 } } @Override public void onAnimationRepeat(Animation animation) { // TODO Auto-generated method stub } @Override public void onAnimationStart(Animation animation) { // TODO Auto-generated method stub } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { // TODO Auto-generated method stub } @Override public void onSensorChanged(SensorEvent event) { // TODO Auto-generated method stub float x = event.values[SensorManager.DATA_X]; float y = event.values[SensorManager.DATA_Y]; float z = event.values[SensorManager.DATA_Z]; if(Math.abs(x)>=14||Math.abs(y)>=14||Math.abs(z)>=14){ shake_line_up.setVisibility(View.VISIBLE); shake_line_down.setVisibility(View.VISIBLE); shake_bg.setVisibility(View.VISIBLE); mPlayer.start(); shake_panel_up.startAnimation(topOutAnim); shake_panel_down.startAnimation(botOutAnim); sMan.unregisterListener(this); } } }
附件怎么传啊?怎么我没看到哪里传附件啊,我没权限么,源码可以共享啊