仿微信摇一摇功能的实现

用最基本的android东西写了一个仿照微信摇一摇的功能。下面给出详细的代码。这里写代码片首先贴出布局文件。

最底层放一张红色的菊花图片,然后上层放一张手的图片,上层这张图片一分为二,要的时候,添加动画图片的上半部分向上,下办部分向下。ok还是看代码吧~~~。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#282e2c">
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:layout_margin="50dp"
            android:layout_gravity="center"
            android:src="@mipmap/flower" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:id="@+id/shake_topId"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="#101010">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:layout_gravity="bottom"
                android:src="@mipmap/shake_top" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/shake_botId"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="#101010">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:src="@mipmap/shake_bottom" />
        </LinearLayout>
    </LinearLayout>

    <TextView
        android:id="@+id/shakeTv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center_horizontal"
        android:text="欢迎使用微信摇一摇"
        android:textColor="@color/colorAccent"
        android:textSize="25sp" />
</FrameLayout>

主要布局使用FrameLayout布局来实现,其他都是最简单最傻x的实现。
下面贴出activity实现部分

/**
 * Created by sfbao1 on 2016/3/3.
 * 摇一摇的功能
 */
public class HaveAShake extends Activity implements SensorEventListener,Animation.AnimationListener {
    TextView tv = null;
    MediaPlayer mp;
    SensorManager sensorManager = null;
    LinearLayout topLv,botLv;
    TranslateAnimation atopup,atopdown,abotup,abotdown;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.shake);

        initPlayer();
        initWidget();
        initAnimation();
    }

    /**
     * 初始化各类控件
     */
    private void initWidget() {
        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        tv = ((TextView) findViewById(R.id.shakeTv));

        topLv= ((LinearLayout) findViewById(R.id.shake_topId));
        botLv= ((LinearLayout) findViewById(R.id.shake_botId));
    }

    /**
     * 初始化上下两张图片的动画
     */
    private void initAnimation() {
        atopup=new TranslateAnimation(0,0,0,-300.0f);
        abotup=new TranslateAnimation(0,0,0, 300.0f);
        atopdown=new TranslateAnimation(0,0,-300.0f,0);
        abotdown=new TranslateAnimation(0,0,300.0f,0);
        atopup.setDuration(1000);
        abotup.setDuration(1000);
        abotdown.setDuration(1000);
        atopdown.setDuration(1000);
        abotdown.setFillAfter(true);
        abotup.setFillAfter(true);
        atopdown.setFillAfter(true);
        atopup.setFillAfter(true);
        AccelerateDecelerateInterpolator as=new AccelerateDecelerateInterpolator();
        atopup.setInterpolator(as);
        atopdown.setInterpolator(as);
        abotup.setInterpolator(as);
        abotdown.setInterpolator(as);
        atopup.setAnimationListener(this);
        abotup.setAnimationListener(this);
    }

    /**
     * 音频文件相关
     */
    private void initPlayer(){
        mp=MediaPlayer.create(this,R.raw.shake_sound_male);
    }

    /**
     * 传感器感受到摇晃时候发生变化
     * @param event
     */
    @Override
    public void onSensorChanged(SensorEvent event) {
        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)) {
                tv.setText("摇一摇成功");
                topLv.startAnimation(atopup);
                botLv.startAnimation(abotup);
                mp.start();
            }
        }
    }

    /**
     * 传感器精度改变的时候回调这个方法
     *
     * @param sensor
     * @param accuracy
     */
    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
    }
    @Override
        protected void onPause() {
        super.onPause();
        sensorManager.unregisterListener(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mp.release();
    }

    @Override
    protected void onResume() {
        super.onResume();
        sensorManager.registerListener(this,
                sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
                SensorManager.SENSOR_DELAY_NORMAL);
    }

    @Override
    public void onAnimationStart(Animation animation) {

    }

    @Override
    public void onAnimationEnd(Animation animation) {
        botLv.startAnimation(abotdown);
        topLv.startAnimation(atopdown);
    }

    @Override
    public void onAnimationRepeat(Animation animation) {

    }
}

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值