Android摇一摇(仿微信),摇个大美女

转载请注明:http://blog.csdn.net/ganklun/article/details/43561241

微信的摇一摇相信大家肯定早就玩的不亦乐乎了吧,事实上也确实是,这东西在微信早期版本中就已经诞生了,当时还是微信标志性功能之一呢,被广大网友誉为“约炮神器”。

接下来就让俺带大家,一起去摇一摇,(*^__^*) 嘻嘻……

1、场景模拟

第一,我们会看到一个提示摇一摇的界面,然后用户会用手机摇一摇,接着我们会看到一个“摇一摇”动画,最终成功获取摇一摇的结果。

2、核心原理

摇一摇主要是利用了Android的加速度感应效果配合相关补间动画得以实现,当加速度到达某个值时,触发某个事件。这里要实现该功能,首先需要了解一下android传感器的使用。Android传感器的使用步骤大致可分为三步:1. 获取传感器管理服对象 SensorManager。2. 创建传感器事件监听类,该类必须实android.hardware.SensorEventListener接口。3. 使用SensorManager.registerListener方法注册指定的传感器。

     传感器事件接口

     SensorEventListener接口,该接口的onSensorChanged()和onAccuracyChanged()方法用于处理相应的传感器事件。

    

public interface SensorEventListener {
 
    /**
     * Called when sensor values have changed.
     * <p>See {@link android.hardware.SensorManager SensorManager}
     * for details on possible sensor types.
     * </p><p>See also {@link android.hardware.SensorEvent SensorEvent}.
     *
     * </p><p><b>NOTE:</b> The application doesn't own the
     * {@link android.hardware.SensorEvent event}
     * object passed as a parameter and therefore cannot hold on to it.
     * The object may be part of an internal pool and may be reused by
     * the framework.
     *
     * @param event the {@link android.hardware.SensorEvent SensorEvent}.
     */
    public void onSensorChanged(SensorEvent event);
 
    /**
     * Called when the accuracy of a sensor has changed.
     * </p><p>See {@link android.hardware.SensorManager SensorManager}
     * for details.
     *
     * @param accuracy The new accuracy of this sensor
     */
    public void onAccuracyChanged(Sensor sensor, int accuracy);   
}



3、核心代码

public class ShakeActivity extends BaseActivity implements SensorEventListener {

	private RelativeLayout shakeImgUp;

	private RelativeLayout shakeImgDown;

	private SensorManager sensorManager;

	private Vibrator vibrator;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.shake_activity);
		findViewById();
		init();
	}

	@Override
	public void init() {
		sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
		vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
	}

	@Override
	public void findViewById() {

		shakeImgUp = (RelativeLayout) findViewById(R.id.shakeImgUp);
		shakeImgDown = (RelativeLayout) findViewById(R.id.shakeImgDown);
	}

	@Override
	public void Message(android.os.Message msg) {

	}

	@Override
	protected void onPause() {
		super.onPause();
		if (sensorManager != null) {// 取消监听器
			sensorManager.unregisterListener(ShakeActivity.this);
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (sensorManager != null) {// 注册监听器
			sensorManager.registerListener(ShakeActivity.this,
					sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
					SensorManager.SENSOR_DELAY_NORMAL);
		}
	}

	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);
		shakeImgUp.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);
		shakeImgDown.startAnimation(animdn);
	}

	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {

	}

	@Override
	public void onSensorChanged(SensorEvent event) {
		int sensorType = event.sensor.getType();
		// values[0]:X轴,values[1]:Y轴,values[2]:Z轴
		float[] values = event.values;
		float x = values[0];
		float y = values[1];
		float z = values[2];
		int shakeValue = 17;
		if (sensorType == Sensor.TYPE_ACCELEROMETER) {
			if (Math.abs(x) > shakeValue || Math.abs(y) > shakeValue
					|| Math.abs(z) > shakeValue) {
				vibrator.vibrate(200);  
				startAnim();
				new Handler().postDelayed(new Runnable(){
					@Override
					public void run() {
						startActivity(new Intent(ShakeActivity.this, ShakeResultActivity.class));
					}
					
				},1000);
			}
		}
	}

}

4、代码下载

http://download.csdn.net/detail/ganklun/8427953


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值