• 安卓高级5 传感器和震动 模仿微信摇一摇Ui效果


    效果图:
    这里写图片描述

    所用的Ui就三张图:
    这里写图片描述
    这里写图片描述
    这里写图片描述


    案例代码:

    结构
    这里写图片描述

    MainActivity.java

    package com.example.myapp;
    
    
    import android.content.Intent;
    import android.hardware.Sensor;
    import android.hardware.SensorManager;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.widget.TextView;
    
    import java.util.List;
    
    public class MainActivity extends AppCompatActivity {
    
        private SensorManager sensorManager;
        private TextView textView;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            getSupportActionBar().hide();
            setContentView(R.layout.activity_main);
            sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
            List<Sensor> sensorList = sensorManager.getSensorList(Sensor.TYPE_ALL);
            StringBuilder sb = new StringBuilder();
            sb.append("您手机的传感器个数 :" + sensorList.size() + "
    ");
            sb.append("列表如下:
    ");
            for (Sensor sensor : sensorList) {
                sb.append(sensor.getName() + "
    ");
            }
    
            textView = ((TextView) findViewById(R.id.tv));
            textView.setText(sb);
    
    
        }
    
        public void click(View view) {
            startActivity(new Intent(this, ShowActivity.class));
        }
    }
    

    ShowActivity.java

    package com.example.myapp;
    
    import android.animation.ObjectAnimator;
    import android.hardware.Sensor;
    import android.hardware.SensorEvent;
    import android.hardware.SensorEventListener;
    import android.hardware.SensorManager;
    import android.os.Bundle;
    import android.os.Vibrator;
    import android.support.v7.app.AppCompatActivity;
    import android.util.Log;
    import android.view.animation.Animation;
    import android.view.animation.AnimationSet;
    import android.view.animation.TranslateAnimation;
    import android.widget.ImageView;
    
    import static android.hardware.SensorManager.SENSOR_DELAY_NORMAL;
    
    public class ShowActivity extends AppCompatActivity implements SensorEventListener {
    
        private SensorManager sensorManage;
        private Sensor sensor;
    
        private float xFlaot;
        private float yFlaot;
        private float zFlaot;
        private ImageView up;
        private ImageView down;
        private ObjectAnimator upAnmation;
        private ObjectAnimator downAnamtion;
        private AnimationSet setUp;
        private TranslateAnimation up1;
        private Vibrator vs;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_show);
            //获取传感器管理器
            sensorManage = ((SensorManager) getSystemService(SENSOR_SERVICE));
            //获取指定传感器
            sensor = sensorManage.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
            //获取震动对象
            vs = ((Vibrator) getSystemService(VIBRATOR_SERVICE));
            //Ui图片手上部分
            up = (ImageView) findViewById(R.id.Myup);
            //Ui图手下部分
            down = (ImageView) findViewById(R.id.Mydwon);
            //初始化动画
            initAnimation();
        }
    
        private void initAnimation() {
          /*  upAnmation = ObjectAnimator.ofFloat(up, "translationY", 0, -200, 0);
            upAnmation.setDuration(4000);*/
            downAnamtion = ObjectAnimator.ofFloat(down,"translationY",0,200,0);
            downAnamtion.setDuration(4000);
            up1 = new TranslateAnimation(0,0,0,-200);
            up1.setDuration(2000);
            final TranslateAnimation up2= new TranslateAnimation(0,0,-200,0);
            up2.setDuration(2000);
    
            setUp = new AnimationSet(false);
            setUp.addAnimation(up1);
    
            up1.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
    
                }
    
                @Override
                public void onAnimationEnd(Animation animation) {
                    up.clearAnimation();
                    up.startAnimation(up2);
                }
    
                @Override
                public void onAnimationRepeat(Animation animation) {
    
                }
            });
            /*upAnmation.addListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animation) {
    
                }
    
                @Override
                public void onAnimationEnd(Animator animation) {
    
                }
    
                @Override
                public void onAnimationCancel(Animator animation) {
    
                }
    
                @Override
                public void onAnimationRepeat(Animator animation) {
    
                }
            });*/
        }
    
    
        @Override
        protected void onResume() {
            super.onResume();
            //注册监听
            sensorManage.registerListener(this, sensor, SENSOR_DELAY_NORMAL);
    
        }
    
        @Override
        public void onSensorChanged(SensorEvent event) {
            float[] values = event.values;
            xFlaot = values[0]; //X轴加速度 左右
            yFlaot = values[1]; //Y轴加速度 前后
            zFlaot = values[2]; //Z轴加速 重力
            int val = 7;
    
            if (xFlaot>val||yFlaot>val||zFlaot>13){
                up.startAnimation(up1);
                //自定义一个震动模式 两个为一组 前一个参数为播放延迟时间 第二个播放时长
                long [] patter =new long[]{100l,2000l,300l,4000l};
                //第一个震动模式,第二个参数从振动模式选取下标中某个重复震动
                //假设第二个参数是1 那么将从 2000l,300l 作为一组数组一直重复
                // 如果第二个参数为震动模式的最后一位 那么将不震动(因为只有一个延迟播放时间,没有播放时间)
                //需要权限<uses-permission android:name="android.permission.VIBRATE"/>
                vs.vibrate(patter,3);
                downAnamtion.start();
                Log.e("fmy","启动");
    
            }
        }
    
        @Override
        protected void onPause() {
            super.onPause();
            //解除注册
            sensorManage.unregisterListener(this);
        }
    
        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
    
        }
    }
    

    activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    
    <ScrollView 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.myapp.MainActivity">
    
    
    
        <LinearLayout
            android:orientation="vertical"
            android:id="@+id/activity_main"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="click"
                android:text="查看实时重力指数" />
            <TextView
                android:id="@+id/tv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Hello World!" />
        </LinearLayout>
    
    
    </ScrollView>
    

    activity_show.xml

    <?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:id="@+id/activity_show"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#1d1d1d"
        android:orientation="vertical"
        tools:context="com.example.myapp.ShowActivity">
    
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:src="@mipmap/shakehideimg_man2" />
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:orientation="vertical">
    
            <ImageView
                android:id="@+id/Myup"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:layout_gravity="center"
                android:src="@mipmap/shake_logo_up" />
    
            <ImageView
                android:id="@+id/Mydwon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:layout_gravity="center"
                android:src="@mipmap/shake_logo_down" />
        </LinearLayout>
    </RelativeLayout>
    
  • 相关阅读:
    viewmodel
    jQuery.each的function中有哪些参数
    JS,Jquery获取各种屏幕的宽度和高度
    解决点击空<a>标签返回页面顶部的问题
    JS使用getComputedStyle()方法获取CSS属性值
    html5中画布和SVG的比较
    HTML中的SVG
    Canvas绘制时钟
    用Canvas实现动画效果
    绘制文字
  • 原文地址:https://www.cnblogs.com/muyuge/p/6152139.html
Copyright © 2020-2023  润新知