模仿微信摇一摇功能

模仿微信摇一摇功能

模仿微信摇一摇功能

1,xml文件----shake_activity.xml
 
 
   


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#111"
android:orientation="vertical" >

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true" >

<ImageView
android:id="@+id/shakeBg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/buffer" />

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="vertical" >

<RelativeLayout
android:id="@+id/shakeImgUp"
android:layout_width="fill_parent"
android:layout_height="190dp"
android:background="#111" >

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:src="@drawable/shake_logo_up" />
</RelativeLayout>

<RelativeLayout
android:id="@+id/shakeImgDown"
android:layout_width="fill_parent"
android:layout_height="190dp"
android:background="#111" >

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:src="@drawable/shake_logo_down" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>

<RelativeLayout
android:id="@+id/shake_title_bar"
android:layout_width="fill_parent"
android:layout_height="45dp"
android:background="@drawable/title_bar"
android:gravity="center_vertical" >

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:onClick="onShake_back"
android:src="@android:drawable/ic_media_previous"
android:textColor="#fff"
android:textSize="14sp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="附近的驴友"
android:textColor="#ffffff"
android:textSize="20sp" />

<ImageButton
android:layout_width="67dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="5dp"
android:background="@drawable/title_btn_right"
android:onClick="linshi"
android:src="@drawable/find_more_friend_shake" />
</RelativeLayout>

<SlidingDrawer
android:id="@+id/slidingDrawer1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:content="@+id/content"
android:handle="@+id/handle" >

<Button
android:id="@+id/handle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/shake_report_dragger_up" />

<LinearLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f9f9f9" >

<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="@drawable/shake_line_up" />
</LinearLayout>
</SlidingDrawer>

</RelativeLayout>

       
   
 
 
 
 
 
2.Shake.java
package com.example.tourismproject;
 
import android.app.Activity;
import android.content.Intent;
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.example.tourismproject.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 ImageView shakeBg;//小狗的图片
private MediaPlayer mp = new MediaPlayer();//开启音乐
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.shake_activity);
//drawerSet ();//设置  drawer监听    切换 按钮的方向
shakeBg=(ImageView) findViewById(R.id.shakeBg);
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);
}
});*/
 
mShakeListener = new ShakeListener(this);
        mShakeListener.setOnShakeListener(new OnShakeListener() {
public void onShake() {
//Toast.makeText(getApplicationContext(), "抱歉,暂时没有找到在同一时刻摇一摇的人。\n再试一次吧!", Toast.LENGTH_SHORT).show();
startMusic();//开启音乐
startAnim();  //开始 摇一摇手掌动画
mShakeListener.stop();
startVibrato(); //开始 震动
new Handler().postDelayed(new Runnable(){
@Override
public void run(){
//Toast.makeText(getApplicationContext(), "抱歉,暂时没有找到\n在同一时刻摇一摇的人。\n再试一次吧!", 500).setGravity(Gravity.CENTER,0,0).show();
Toast mtoast;
mtoast = Toast.makeText(getApplicationContext(),
    "抱歉,暂时没有找到\n在同一时刻摇一摇的人。\n再试一次吧!", 10);
  //mtoast.setGravity(Gravity.CENTER, 0, 0);
  mtoast.show();
 mp.stop();//音乐结束
  mVibrator.cancel();//震动结束
  mShakeListener.start();
}
}, 2000);
}
});
   }
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);
/*
* 设置小狗的动画
*/
RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setDuration(1000);
//加速差值器
rotateAnimation.setInterpolator(new AccelerateInterpolator());
shakeBg.startAnimation(rotateAnimation);
}
/*
* 开启音乐
*/
public void startMusic(){
try {
mp.stop(); 
mp = MediaPlayer.create(getApplicationContext(), R.raw.two); 
bofang();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/*
* 播放音乐
*/
public void bofang() throws IllegalStateException, IOException{

if(mp.isPlaying()){
return;
}
else{
mp.setLooping(true); 

try {
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp.start();
}
}

public void startVibrato(){ //定义震动
//mVibrator.vibrate( new long[]{500,200,500,200}, -1); //第一个{}里面是节奏数组, 第二个参数是重复次数,-1为不重复,非-1俄日从pattern的指定下标开始重复
//}
public void onShake_back(View v) {     //标题栏 返回按钮
      Intent intent=new Intent(this,MainActivity.class);
      startActivity(intent);
      finish();
      overridePendingTransition(R.anim.tran_pre_in, R.anim.tran_pre_out);
      }  
public void linshi(View v) {     //标题栏
startAnim();
      }  
@Override
protected void onDestroy() {
super.onDestroy();
if (mShakeListener != null) {
mShakeListener.stop();
}
}
}
3,ShakeListener.java
package com.example.tourismproject;
 
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 = 3000;
// 两次检测的时间间隔
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;
Log.v("thelog", "===========log===================");
// 达到速度阀值,发出提示
if (speed >= SPEED_SHRESHOLD) {
onShakeListener.onShake();
}
}
 
public void onAccuracyChanged(Sensor sensor, int accuracy) {
 
}
 
// 摇晃监听接口
public interface OnShakeListener {
public void onShake();
}
 
}
 
4在AndroidManifest.xml设置权限
<uses-permission android:name="android.permission.VIBRATE" /><!-- 注意:震动权限 -->
 
本人菜鸟一个,刚学Android,这个是通过人家的源码自己再进行修改了一下,如有不足,欢迎高手指点
没有震动权限出现的错误:
模仿微信摇一摇功能

模仿微信摇一摇功能

上一篇:仿照微信的界面,即ViewPager+Fragment的结合使用


下一篇:微信网页静默授权(scope=snsapi_base),这种方式不需要用户手动同意