水平滑动以使动画可见布局

时间:2013-12-19 12:04:54

标签: android android-layout android-viewpager android-animation android-view

我在Android应用程序中工作,我希望在使用动画效果进行滑动时,使我的布局的一部分可见且不可见。

这是我的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="2" >

        <LinearLayout
            android:id="@+id/hiddenLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight=".3"
            android:background="@android:color/black"
            android:orientation="horizontal"
            android:visibility="gone" >
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1.7"
            android:background="@android:color/darker_gray" />
    </LinearLayout>

</LinearLayout>

默认情况下,名为“hiddenLayout”可见性的布局已消失。但是当我从左向右滑动时,它应该像ViewPager一样以动画效果显示。它的可见性应该是相反方向的滑动。

我该如何实现呢?

1 个答案:

答案 0 :(得分:0)

您的活动应该实现GestureDetector.OnGestureListener 您可以在此方法中处理隐藏布局的可见性 -

 @Override
public boolean onFling(MotionEvent event1, MotionEvent event2, 
        float velocityX, float velocityY) {

    boolean result = false;
        try {
            float diffY = e2.getY() - e1.getY();
            float diffX = e2.getX() - e1.getX();
            if (Math.abs(diffX) > Math.abs(diffY)) {
                if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffX > 0) {
                        onSwipeRight();
                    } else {
                        onSwipeLeft();
                    }
                }
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return result;
    }
}

最后为视图添加动画..