[Android实例] 仿微信摇一摇功能实现

来源:互联网 发布:抓取数据 编辑:程序博客网 时间:2024/04/30 18:04

该篇文章从eoeAndroid搬迁过来的,原文地址:[Android实例] 仿微信摇一摇功能实现

前段时间,由于换了新工作,参与到公司新项目里,很忙,也没时间去弄一些好玩的东西。这些天不算忙,突然想到我在项目里仿照微信做了摇一摇功能,也就吧该功能提取出来,共享给大家。 摇一摇功能其实很简单,主要用到了加速度传感器,因此需要加入权限

<uses-permission android:name="android.permission.VIBRATE"/>

主要代码如下:
1.监听传感器加速变化并进行处理

@Override    public void onSensorChanged(SensorEvent event) {        // TODO Auto-generated method stub        int sensorType = event.sensor.getType();        // values[0]:X轴,values[1]:Y轴,values[2]:Z轴        float[] values = event.values;        if (sensorType == Sensor.TYPE_ACCELEROMETER) {            if ((Math.abs(values[0]) > 17 || Math.abs(values[1]) > 17 || Math                    .abs(values[2]) > 17) && !isShake) {                isShake = true;                new Thread() {                    public void run() {                        try {                            runOnUiThread(new Runnable() {                                public void run() {                                    // 摇动手机后,再伴随震动提示~~                                    vibrator.vibrate(300);                                    topLineIv.setVisibility(View.VISIBLE);                                    bottomLineIv.setVisibility(View.VISIBLE);                                    startAnimation(false);                                }                            });                            Thread.sleep(500);                            runOnUiThread(new Runnable() {                                public void run() {                                    // 摇动手机后,再伴随震动提示~~                                    vibrator.vibrate(300);                                }                            });                            Thread.sleep(500);                            runOnUiThread(new Runnable() {                                @Override                                public void run() {                                    // TODO Auto-generated method stub                                    isShake = false;                                    startAnimation(true);                                }                            });                        } catch (InterruptedException e) {                            // TODO Auto-generated catch block                            e.printStackTrace();                        }                    };                }.start();            }        }    }

2.动画效果

private void startAnimation(boolean isBack) {        int type = TranslateAnimation.RELATIVE_TO_SELF;        float topFromYValue;        float topToYValue;        float bottomFromYValue;        float bottomToYValue;        if (isBack) {            topFromYValue = -0.5f;            topToYValue = 0;            bottomFromYValue = 0.5f;            bottomToYValue = 0;        } else {            topFromYValue = 0;            topToYValue = -0.5f;            bottomFromYValue = 0;            bottomToYValue = 0.5f;        }        TranslateAnimation topAnimation = new TranslateAnimation(type, 0, type,                0, type, topFromYValue, type, topToYValue);        topAnimation.setDuration(200);        topAnimation.setFillAfter(true);        TranslateAnimation bottomAnimation = new TranslateAnimation(type, 0,                type, 0, type, bottomFromYValue, type, bottomToYValue);        bottomAnimation.setDuration(200);        bottomAnimation.setFillAfter(true);        if (isBack) {            bottomAnimation                    .setAnimationListener(new TranslateAnimation.AnimationListener() {                        @Override                        public void onAnimationStart(Animation animation) {                            // TODO Auto-generated method stub                        }                        @Override                        public void onAnimationRepeat(Animation animation) {                            // TODO Auto-generated method stub                        }                        @Override                        public void onAnimationEnd(Animation animation) {                            // TODO Auto-generated method stub                            topLineIv.setVisibility(View.GONE);                            bottomLineIv.setVisibility(View.GONE);                        }                    });        }        bottomLayout.startAnimation(bottomAnimation);        topLayout.startAnimation(topAnimation);    }

这里写图片描述 这里写图片描述

下载地址:项目代码

0 0
原创粉丝点击