简体   繁体   English

如何实现幻灯片在android中显示动画?

[英]How to implement slide to reveal animation in android?

I have created 5 activities, each has a ListView containing only images. 我创建了5个活动,每个活动都有一个只包含图像的ListView。 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. 我想,如果你按照@Whitney的建议,会更容易。 Put your fragments in view pager as described here: http://developer.android.com/training/animation/screen-slide.html 将您的片段放入视图寻呼机中,如下所述: 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. 实时未检测到Fling。 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 如果您想实现实时销售,可能必须覆盖触摸事件,如下所述: http//developer.android.com/training/custom-views/making-interactive.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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