android 微信摇一摇 简单实现

燕雨石
2023-12-01
Java代码部分
//MainActivity
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.exple.zuoye6.shake.ShakeActivity;


public class MainActivity extends AppCompatActivity {
    private Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void onClick(View view) {
        Intent intent = new Intent();
        switch (view.getId()) {
            case R.id.btn_shake:
                intent.setClass(MainActivity.this, ShakeActivity.class);
                break;
        }

        startActivity(intent);
    }
}

//Main2Activity

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.exple.zuoye6.R;

public class Main2Activity extends AppCompatActivity {
    Button normalBtn;
    Button contentBtn;
    private View.OnClickListener onClickListener;
    private View.OnClickListener setOnClickListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        //初始化组件
        normalBtn = (Button) findViewById(R.id.normalBtn);
        //设置监听器对象
        normalBtn.setOnClickListener(onClickListener);
        contentBtn = (Button) findViewById(R.id.contentBtn);
        contentBtn.setOnClickListener(setOnClickListener);

    }
}

//ShakeActivity

import androidx.appcompat.app.AppCompatActivity;

import android.app.ActionBar;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.app.MediaRouteButton;
import android.content.Intent;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.Toast;

import com.exple.zuoye6.MainActivity;
import com.exple.zuoye6.R;

public class ShakeActivity<isBack> extends AppCompatActivity implements SensorEventListener {

    ImageView upImg,downImg,centerImg;
    SensorManager sensorManager;
    private Sensor sensor;
    private AnimationSet mSetUp;
    private AnimationSet mSetDown;
    SoundPool soundPool;
    private int loadId;
    Object vibrator;
    private AlertDialog.Builder dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shake);
        init();
        initAnimation();

    }

    private void init(){
        upImg = findViewById(R.id.id_shake_upimg);
        downImg = findViewById(R.id.id_shake_downimg);
        centerImg = findViewById(R.id.id_shake_centerimg);
        //获取传感器管理者对象
        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        sensorManager.registerListener(this,sensor,SensorManager.SENSOR_DELAY_NORMAL);

        soundPool = new SoundPool(2, AudioManager.STREAM_MUSIC,0);
        //2.加载音乐
        loadId = soundPool.load(this,R.raw.ave,1);
        //获取振动器管理者对象
        vibrator = getSystemService(VIBRATOR_SERVICE);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        sensorManager.unregisterListener(this);
    }

    @Override
    public void onSensorChanged(SensorEvent sensorEvent) {
        float[] values = sensorEvent.values;
        float x = values[0];
        float y = values[1];
        float z = values[2];
        int minValue = 12;
        if(Math.abs(x)>minValue||Math.abs(y)>minValue||Math.abs(z)>minValue) {
            //开始震动


            //播放音效
            soundPool.play(loadId,1,1,1,0,1);


            //开始动画效果

            upImg.startAnimation(mSetUp);
            downImg.startAnimation(mSetDown);


        }


    }

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

    }

    private void initAnimation(){
        //上面图片对应的图画效果
        TranslateAnimation mAnimationUp = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,-1);
        mAnimationUp.setDuration(500);
        TranslateAnimation mAnimationUpDown = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,-1,Animation.RELATIVE_TO_SELF,0);
        mAnimationUpDown.setDuration(500);
        mSetUp = new AnimationSet(true);
        //将上下移动的动画添加到集合当中
        mSetUp.addAnimation(mAnimationUp);
        mSetUp.addAnimation(mAnimationUpDown);
        //设置动画之间执行的时差
        mSetUp.setStartOffset(500);

        //下面图片的动画效果
        TranslateAnimation mAnimationDown = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,1);
        mAnimationDown.setDuration(500);
        TranslateAnimation mAnimationDownUp= new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,1,Animation.RELATIVE_TO_SELF,0);
        mAnimationDownUp.setDuration(500);

        mSetDown = new AnimationSet(true);
        mSetDown.addAnimation(mAnimationDown);
        mSetDown.addAnimation(mAnimationDownUp);
        mAnimationDown.setStartOffset(500);


    }
    private void startAnimation(boolean isBack) {
        //动画坐标移动的位置的类型是相对自己的
        int type = Animation.RELATIVE_TO_SELF;

        float topFromY;
        float topToY;
        float bottomFromY;
        float bottomToY;
        if (isBack) {
            topFromY = -0.5f;
            topToY = 0;
            bottomFromY = 0.5f;
            bottomToY = 0;
        } else {
            topFromY = 0;
            topToY = -0.5f;
            bottomFromY = 0;
            bottomToY = 0.5f;
        }

        //上面图片的动画效果
        TranslateAnimation topAnim = new TranslateAnimation(
                type, 0, type, 0, type, topFromY, type, topToY
        );
        topAnim.setDuration(200);
        //动画终止时停留在最后一帧~不然会回到没有执行之前的状态
        topAnim.setFillAfter(true);

        //底部的动画效果
        TranslateAnimation bottomAnim = new TranslateAnimation(
                type, 0, type, 0, type, bottomFromY, type, bottomToY
        );
        bottomAnim.setDuration(200);
        bottomAnim.setFillAfter(true);

        //大家一定不要忘记, 当要回来时, 我们中间的两根线需要GONE掉
        if (isBack) {
            
            bottomAnim.setAnimationListener(new Animation.AnimationListener() {

                private ActionBar builder;

                @Override
                public void onAnimationStart(Animation animation) {
                }

                @Override
                public void onAnimationRepeat(Animation animation) {
                }

                @Override
                public void onAnimationEnd(Animation animation) {

                    dialog = new AlertDialog.Builder(ShakeActivity.this);
                    dialog.setMessage("爱本无罪,你说对吗?");//为对话框设置内容
                    dialog.setTitle("你还爱我吗?"); //为对话框设置标题
                    dialog.setPositiveButton("爱", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) { //给对话框设置yes按钮
                            //获取String.xml里面的值
                            String mystring = getResources().getString(R.string.app_b);
                            Intent intent = new Intent(ShakeActivity.this, Main2Activity.class);
                            intent.putExtra("gy", mystring);
                            startActivity(intent);
                            Toast.makeText(ShakeActivity.this, "" + mystring, Toast.LENGTH_LONG).show();
                        }
                    });
                    dialog.setNegativeButton("不爱", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {  //给对话框添加no按钮
                            Toast.makeText(ShakeActivity.this, "狗子你是不是变了!", Toast.LENGTH_LONG).show();
                        }
                    });
                    builder.show();//显示对话框
                }
            });
        }

    }

}
//页面部分

//activity_main

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="微信摇一摇"
        android:id="@+id/btn_shake"
        android:onClick="onClick"/>


</LinearLayout>

//activity_main2

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".shake.Main2Activity">
    <Button
        android:id="@+id/normalBtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Dialog"
        />
    <Button
        android:id="@+id/contentBtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Dialog"
        />



</RelativeLayout>

//activity_shake

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:background="#000">

    <ImageView
        android:id="@+id/id_shake_centerimg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@mipmap/fiower" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"
        android:orientation="vertical"
        android:gravity="center">

        <ImageView
            android:id="@+id/id_shake_upimg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/up"
            />

        <ImageView
            android:id="@+id/id_shake_downimg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/down"/>


    </LinearLayout>

</RelativeLayout>

 类似资料: