简体   繁体   中英

How to implement slide to reveal animation in android?

I have created 5 activities, each has a ListView containing only images. I have used a simple swipe animation to change activities.

Here is the relevant code:

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (gestureDetector.onTouchEvent(event)) {
        return true;
    }
    return super.onTouchEvent(event);
}

private void onLeftSwipe() {
    Intent intent = new Intent(HimachalActivity.this, IndianWildlifeActivity.class);
    startActivity(intent);
    overridePendingTransition(R.anim.slide_left_in, R.anim.slide_left_out);
}

private void onRightSwipe() {
    Intent intent = new Intent(HimachalActivity.this, BaseActivity.class);
    startActivity(intent);
    overridePendingTransition(R.anim.slide_right_in, R.anim.slide_right_out);
}

private class SwipeGestureDetector extends GestureDetector.SimpleOnGestureListener {
    // Swipe properties, you can change it to make the swipe
    // longer or shorter and speed
    private static final int SWIPE_MIN_DISTANCE = 120;
    private static final int SWIPE_MAX_OFF_PATH = 200;
    private static final int SWIPE_THRESHOLD_VELOCITY = 200;

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2,
                           float velocityX, float velocityY) {
        try {
            float diffAbs = Math.abs(e1.getY() - e2.getY());
            float diff = e1.getX() - e2.getX();

            if (diffAbs > SWIPE_MAX_OFF_PATH)
                return false;

            // Left swipe
            if (diff > SWIPE_MIN_DISTANCE
                    && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                HimachalActivity.this.onLeftSwipe();

                // Right swipe
            } else if (-diff > SWIPE_MIN_DISTANCE
                    && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                HimachalActivity.this.onRightSwipe();
            }
        } catch (Exception e) {
            Log.e("BaseActivity", "Error on gestures");
        }
        return false;
    }
}

The problem is that the next activity or previous activity opens only after full swipe and the animation does not feel that smooth.

I want an animation in which the next activity or previous activity starts revealing itself as soon as I start swiping. Is there any built-in animation that can be applied. If not, please point me to any relevant resources for creating one myself, possibly, one which does not require me to modify the existing code much. Thanks.

据我所知,这个功能在Lollipop材料设计模块中很容易获得。

I reckon, it would be easier in case you follow @Whitney 's advice. Put your fragments in view pager as described here: http://developer.android.com/training/animation/screen-slide.html

The reason it is not working with your current implementation is that the device makes sense of fling gesture only after you have flinged . Thereafter, the animation takes place. Fling is not detected in real time. In case you want to implement real time fling, you might have to override touch events as described here: http://developer.android.com/training/custom-views/making-interactive.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM