滑入视图动画

时间:2014-04-04 10:43:58

标签: android

我希望相对布局能够滑入屏幕。

目前我将布局设置为占据整个屏幕的布局

<FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    android:animateLayoutChanges="true"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">
        <ImageView
                android:id="@+id/ghostIv"
                android:src="@drawable/results_ghost"
                android:visibility="gone"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_centerHorizontal="true"/>
        <ImageView
                android:id="@+id/ghostShadowIv"
                android:visibility="gone"
                android:src="@drawable/results_ghost_shadow"
                android:layout_centerHorizontal="true"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/ghostIv"/>
        <TextView
                android:id="@+id/noFilterTv"
                android:textStyle="bold"
                android:gravity="center_horizontal"
                android:textColor="@color/LightTextGrey"
                android:visibility="gone"
                android:text="@string/no_filters"
                android:paddingLeft="50dp"
                android:paddingRight="50dp"
                android:layout_centerHorizontal="true"
                android:layout_below="@+id/ghostShadowIv"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"/>

        <com.allryder.android.ui.views.ScheduleView
                android:visibility="invisible"
                android:background="@color/White"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/scheduleView"
                android:layout_alignParentTop="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentRight="true"/>

        <View
                android:id="@+id/shadowV"
                android:visibility="gone"
                android:layout_above="@+id/ratingRl"
                android:layout_height="4dp"
                android:layout_width="match_parent"
                android:background="@drawable/bottom_shadow"/>

        <RelativeLayout
                android:id="@+id/ratingRl"
                android:layout_below="@+id/scheduleView"
                android:background="@color/White"
                android:clickable="true"
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >

            <TextView
                    android:background="@drawable/time_view_selector"
                    android:padding="10dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:id="@+id/ratePromptTv"
                    android:layout_toLeftOf="@+id/closeRatingIv"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentTop="true"
                    android:gravity="center|left"
                    android:text="@string/rate_prompt"/>
            <ImageView
                    android:background="@drawable/time_view_selector"
                    android:padding="10dp"
                    android:clickable="true"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignTop="@+id/ratePromptTv"
                    android:layout_alignBottom="@+id/ratePromptTv"
                    android:id="@+id/closeRatingIv"
                    android:layout_centerVertical="true"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentEnd="true"
                    android:src="@drawable/search_clear"/>
        </RelativeLayout>
    </RelativeLayout>
</FrameLayout>

<fragment android:id="@+id/filters_drawer"
          android:layout_width="wrap_content"
          android:layout_height="match_parent"
          android:layout_gravity="end"
          android:name="com.allryder.android.ui.fragments.FiltersDrawerFragment"/>

然后拨打电话

      ratingRl.animate().translationY(- ratingRl.getHeight());

但它并没有滑入屏幕。如果视图在屏幕中开始,则视图会生成动画。有什么问题?

2 个答案:

答案 0 :(得分:0)

 ratingRl.animate().translationY(- ratingRl.getHeight()).setDuration(300).start();

答案 1 :(得分:0)

我在最近的项目中完成了类似的任务。

background = (RelativeLayout) findViewById(R.id.slidingBackground);
positionOffScreen(background);

private void positionOffScreen(View v){
    View myView = v;
    int amountOffscreen = (int)(getScreenWidth());
    boolean offscreen = true;

    int xOffset = (offscreen) ? amountOffscreen : 0;
    RelativeLayout.LayoutParams rlParams = (RelativeLayout.LayoutParams)myView.getLayoutParams();
    rlParams.setMargins(-1*xOffset, 0, xOffset, 0);
    myView.setLayoutParams(rlParams);
}

protected float getScreenWidth() {
    DisplayMetrics displaymetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    return (float) displaymetrics.widthPixels;
}

当您想要开始动画时,请调用此方法

private void startAnimation(int duration){
    TranslateAnimation a = new TranslateAnimation(Animation.ABSOLUTE,0,Animation.ABSOLUTE,getScreenWidth(),Animation.ABSOLUTE,0,Animation.ABSOLUTE,0);
    a.setDuration(duration);
    a.setFillAfter(false);
    background.startAnimation(a);
}

当您想要中断动画

background.clearAnimation();

这是布局示例。滑动视图必须放在RelativeLayout

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background" >

    <RelativeLayout
        android:id="@+id/slidingBackground"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#64000000"
        android:orientation="vertical" >
    </RelativeLayout>
    ...

this是我使用此滑动视图的应用链接。