Implementing a shake event

半世苍凉 提交于 2019-12-04 10:36:29

I think this might makes things easier for you.

Create a new Class called Shaker and add this.

public class Shaker {
private SensorManager mgr = null;
private long lastShakeTimestamp = 0;
private double threshold = 1.0d;
private long gap = 0;
private Shaker.Callback cb = null;

public Shaker(Context ctxt, double threshold, long gap, Shaker.Callback cb) {
    this.threshold = threshold * threshold;
    this.threshold = this.threshold * SensorManager.GRAVITY_EARTH
            * SensorManager.GRAVITY_EARTH;
    this.gap = gap;
    this.cb = cb;

    mgr = (SensorManager) ctxt.getSystemService(Context.SENSOR_SERVICE);
    mgr.registerListener(listener,
            mgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
            SensorManager.SENSOR_DELAY_UI);
}

public void close() {
    mgr.unregisterListener(listener);
}

private void isShaking() {
    long now = SystemClock.uptimeMillis();
    try {
        if (lastShakeTimestamp == 0) {
            lastShakeTimestamp = now;

            if (cb != null) {
                cb.shakingStarted();
            }
        } else {
            lastShakeTimestamp = now;
        }
    } catch (NullPointerException e) {

    }
}

private void isNotShaking() {
    long now = SystemClock.uptimeMillis();

    if (lastShakeTimestamp > 0) {
        if (now - lastShakeTimestamp > gap) {
            lastShakeTimestamp = 0;

            if (cb != null) {
                cb.shakingStopped();
            }
        }
    }
}

public interface Callback {
    void shakingStarted();

    void shakingStopped();
}

private final SensorEventListener listener = new SensorEventListener() {
    @Override
    public void onSensorChanged(SensorEvent e) {
        if (e.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
            double netForce = e.values[0] * e.values[0];

            netForce += e.values[1] * e.values[1];
            netForce += e.values[2] * e.values[2];

            if (threshold < netForce) {
                isShaking();
            } else {
                isNotShaking();
            }
        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // unused
        }
    };
}

In your Activity implement Shaker.Callback and use the methods shakingStarted() and shakingStopped() to actually do something.

Hi simple use the following code for shake event: I am adding shake event for linear layout

Note 1:create a folder anim and create shake.xml and add the below code

<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromXDelta="0"
android:interpolator="@anim/test"
android:toXDelta="30" />

create test.xml and add the below code:

<cycleInterpolator xmlns:android="http://schemas.android.com/apk/res/android"
android:cycles="7" />

Note 2:use the following code in class.

 private Animation shake;
    private LinearLayout linearLayout;

//After onCreate paste the below code

linearLayout = (LinearLayout) findViewById(R.id.test);
shake = AnimationUtils.loadAnimation(this, R.anim.shake);

//we can use the below code on button press or as per your requirement

linearLayout.startAnimation(shake);
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!