android 实现微信摇一摇效果

其实微信摇一摇的效果不是特别难,主要就是一个加速感应器和动画效果的配合了。本篇代码实现了摇一摇的功能,但界面方面不是特别好看,同学们可以把本文的代码下载,无需导入任何图片就可使用了,因为ImageView 我直接设的背景色,没上图片,你懂的。下面就是代码了:

activity

package com.shake.activity;

import com.shake.activity.R;

import android.app.Activity;
import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Window;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class ShakeActivity extends Activity implements SensorEventListener,AnimationListener{

	private SensorManager  sensorManager ;
	private Sensor sensor ;
	private Animation out_top_Annotation = null;//上半部退出动画
	private Animation out_bottom_Annotation = null;//下半部退出动画
	private Animation in_top_Annotation = null;//上半部进入动画
	private Animation in_bottom_Annotation = null;下半部进入动画
	private int duration = 450;//动画效果时常,可以自己设置,此处为0.45秒
	private ImageView imageView_top = null;
	private ImageView imageView_bottom = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.main);
	    
		LayoutInflater inflater = LayoutInflater.from(this);
		LinearLayout ll = (LinearLayout) inflater.inflate(R.layout.main, null);
		
		sensorManager =  (SensorManager) getSystemService(SENSOR_SERVICE);
		sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
		sensorManager.registerListener(this,sensor, SensorManager.SENSOR_DELAY_GAME);

		/**
		 * 设置动画效果
		 */
		this.out_top_Annotation = new TranslateAnimation(0,0,0,-(float)getWindowManager().getDefaultDisplay().getHeight()/3);
		this.out_top_Annotation.setDuration(duration);
		
		
		this.out_bottom_Annotation = new TranslateAnimation(0,0,0,(float)getWindowManager().getDefaultDisplay().getHeight()/3);
		this.out_bottom_Annotation.setDuration(duration);
		
		this.in_top_Annotation = new TranslateAnimation(0,0,-(float)getWindowManager().getDefaultDisplay().getHeight()/3,0);
		this.in_top_Annotation.setDuration(duration);
		
		this.in_bottom_Annotation = new TranslateAnimation(0,0,(float)getWindowManager().getDefaultDisplay().getHeight()/3,0);
		this.in_bottom_Annotation.setDuration(duration);
		
		this.out_bottom_Annotation.setAnimationListener(this);
		this.in_bottom_Annotation.setAnimationListener(this);
		
		/**
		 * 新建两个ImageView用于展示摇的效果
		 */
		this.imageView_top = new ImageView(this);
		this.imageView_top.setLayoutParams(new LayoutParams(getWindowManager().getDefaultDisplay().getWidth(),
				getWindowManager().getDefaultDisplay().getHeight()/2));
		
		this.imageView_bottom = new ImageView(this);
		this.imageView_bottom.setLayoutParams(new LayoutParams(getWindowManager().getDefaultDisplay().getWidth(),
				getWindowManager().getDefaultDisplay().getHeight()/2));
		
		
		this.imageView_top.setBackgroundColor(Color.BLUE);//设置上半部分背景色,实际中用图来表示
		this.imageView_bottom.setBackgroundColor(Color.RED);//设置下班部分背景,实际中用图来表示
		//实际应用中上半部分和下半部分是一张图切成两部分的
		ll.addView(imageView_top);
		ll.addView(imageView_bottom);
		
		setContentView(ll);
		

	}


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

	}

	@Override
	public void onSensorChanged(SensorEvent event) {
		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){//判断加速度>14时,这个值是可以修改的。
			imageView_bottom.startAnimation(out_bottom_Annotation);//开始动画,记住是startAnimation,不是setAnimation
			imageView_top.startAnimation(out_top_Annotation);
			sensorManager.unregisterListener(this);//取消监听加速感应器,如果不取消的话会有问题,同学们可以自己去掉,狂摇试试
		}

	}


	@Override
	public void onAnimationEnd(Animation animation) {
		// TODO Auto-generated method stub
		if(animation == this.out_bottom_Annotation){
			imageView_bottom.startAnimation(in_bottom_Annotation);
			imageView_top.startAnimation(in_top_Annotation);
		}else if(animation.equals(this.in_bottom_Annotation)){
			sensorManager.registerListener(this,sensor, SensorManager.SENSOR_DELAY_GAME);//完成动画后再次监听加速感应器
		}
	}


	@Override
	public void onAnimationRepeat(Animation animation) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void onAnimationStart(Animation animation) {
		// TODO Auto-generated method stub
		
	}

}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >


</LinearLayout>

main.xml这个布局中没有控件,是因为我考虑到屏幕的适配,两个ImageView 都是直接在代码中调整的布局。具体在xml中怎么调整没研究过,估计可以使用layout_weight这个属性设置吧,具体没弄过。

刚刚玩微信的时候发现摇的时候有朵小红花。其实就是将布局设个背景就可以了。


还有,如果对代码有什么疑问或者有什么好的建议,可以留言,有时间看到一定会回复的。

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值