继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

高仿微信摇一摇,动画效果为左右晃动

明月笑刀无情
关注TA
已关注
手记 104
粉丝 16
获赞 56

原文地址;http://blog.csdn.net/catoop/article/details/8051835,原博主实现了摇一摇

在这几天的学习中,添加了状态栏和摇晃完以后的动画,如图

                                               5baccea500015ec203460596.jpg

学习到东西,发出来帮助更多的人,做一个有奉献精神的人


思路;

1.getSystemService(SENSOR_SERVICE)得到SensorManager ,有了它你就可以管理传感器了

2.getSystemService(VEBRATOR_SERVICE)得到手机振动的服务

3.initActionBar 设置手机状态栏的返回键

4.重力感应监听

5handler发送消息

6监听到以后 执行动画

 

代码如下

布局文件

[代码]xml代码:

?

01

02

03

04

05

06

07

08

09

10

11

12

13

14

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout

    xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context="com.example.dong.shake.MainActivity">

    <ImageView

        android:id="@+id/iv_shake"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_centerInParent="true"

        android:src="@mipmap/shake_logo"/>

</RelativeLayout>

activity中的代码

[代码]xml代码:

?

001

002

003

004

005

006

007

008

009

010

011

012

013

014

015

016

017

018

019

020

021

022

023

024

025

026

027

028

029

030

031

032

033

034

035

036

037

038

039

040

041

042

043

044

045

046

047

048

049

050

051

052

053

054

055

056

057

058

059

060

061

062

063

064

065

066

067

068

069

070

071

072

073

074

075

076

077

078

079

080

081

082

083

084

085

086

087

088

089

090

091

092

093

094

095

096

097

098

099

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

public   class MainActivity extends AppCompatActivity {

    @Bind(R.id.iv_shake)

    ImageView   ivShake;

    private   static final String TAG = "TestSensorActivity";

    private   static final int SENSOR_SHAKE = 10;

    private   SensorManager sensorManager;

    private   Vibrator vibrator;

    private   boolean isShaking = false;

    @Override

    protected   void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        ButterKnife.bind(this);

        sensorManager   = (SensorManager) getSystemService(SENSOR_SERVICE);

        vibrator   = (Vibrator) getSystemService(VIBRATOR_SERVICE);

        initActionBar();//可以设置状态栏的按钮和内容(返回键)

    }

    private   void initActionBar() {

        ActionBar   actionBar = getSupportActionBar();

        actionBar.setDisplayHomeAsUpEnabled(true);

        actionBar.setTitle("摇一摇");

    }

    @Override

    protected   void onResume() {

        super.onResume();

        if   (sensorManager != null) {

            //   第一个参数是Listener,第二个参数是所得传感器类型,第三个参数值获取传感器信息的频率

            sensorManager.registerListener(sensorEventListener,   sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),   SensorManager.SENSOR_DELAY_NORMAL);

        }

    }

    @Override

    protected   void onPause() {

        super.onPause();

        if   (sensorManager != null) {

            //   取消监听器

            sensorManager.unregisterListener(sensorEventListener);

        }

    }

    /**

     *   重力感应监听

     */

    private   SensorEventListener sensorEventListener = new SensorEventListener() {

        @Override

        public   void onSensorChanged(SensorEvent event) {

            //   传感器信息改变时执行该方法

            float[]   values = event.values;

            float   x = values[0]; // x轴方向的重力加速度,向右为正

            float   y = values[1]; // y轴方向的重力加速度,向前为正

            float   z = values[2]; // z轴方向的重力加速度,向上为正

            Log.i(TAG,   "x轴方向的重力加速度" + x + ";y轴方向的重力加速度" + y + ";z轴方向的重力加速度" + z);

            //   一般在这三个方向的重力加速度达到40就达到了摇晃手机的状态。

            int   medumValue = 19;// 三星 i9250怎么晃都不会超过20,没办法,只设置19了

            if   (Math.abs(x) > medumValue || Math.abs(y) > medumValue || Math.abs(z)   > medumValue) {

                vibrator.vibrate(200);

                Message   msg = new Message();

                msg.what   = SENSOR_SHAKE;

                handler.sendMessage(msg);

            }

        }

        @Override

        public   void onAccuracyChanged(Sensor sensor, int accuracy) {

        }

    };

    /**

     *   动作执行

     */

    Handler   handler = new Handler() {

        @Override

        public   void handleMessage(Message msg) {

            super.handleMessage(msg);

            switch   (msg.what) {

                case   SENSOR_SHAKE:

                    if   (!isShaking) {

                        startAnim();

                        isShaking   = true;

                    }

                    break;

            }

        }

    };

    //定义摇一摇动画动画

    private   void startAnim() {

        AnimationSet   animup = new AnimationSet(true);

        TranslateAnimation   animation0 = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0f,

                Animation.RELATIVE_TO_SELF,   -0.5f,Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF,0.f);

        animation0.setDuration(300);

        TranslateAnimation   animation1 = new TranslateAnimation(Animation.RELATIVE_TO_SELF,-0.5f,

                Animation.RELATIVE_TO_SELF,   0.5f,Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF,0.f);

        animation1.setDuration(300);

        animation1.setStartOffset(300);

        TranslateAnimation   animation2 = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0.5f,

                Animation.RELATIVE_TO_SELF,   0f,Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF,0.f);

        animation2.setDuration(300);

        animation2.setStartOffset(600);

        animup.addAnimation(animation0);

        animup.addAnimation(animation1);

        animup.addAnimation(animation2);

        animup.setRepeatCount(Animation.RESTART);

        ivShake.startAnimation(animup);

        ivShake.getAnimation().setAnimationListener(new   Animation.AnimationListener() {

            @Override

            public   void onAnimationStart(Animation animation) {

            }

            @Override

            public   void onAnimationEnd(Animation animation) {

                isShaking   = false;

            }

            @Override

            public   void onAnimationRepeat(Animation animation) {

            }

        });

    }

    @Override

    public   boolean onOptionsItemSelected(MenuItem item) {

        switch   (item.getItemId()) {

            case   android.R.id.home:

                //回退

                finish();

                break;

        }

        return   super.onOptionsItemSelected(item);

    }

}

原文链接:http://www.apkbus.com/blog-862846-62509.html

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP