Animating Relativelayout

好久不见. 提交于 2020-01-14 05:20:07

问题


I have got following XML structure of my screen. I want my Relative Layout with id journeyLandingView to get slide up upon click on some button and show view RelativeLayout with id journeyRootView which will slide up from bottom since journeyLandingView have Fill_Parent set as height and width so journeyRootView must be below journeyLandingView that is covering the whole screen. How can I do that?

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

    <RelativeLayout android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:id="@+id/journeyRootView"
        android:background="@drawable/root_layer">

    </RelativeLayout>
</LinearLayout>

回答1:


I belive the kind of animation you want can be achieved using a ViewFlipper.

You could put your RelativeLayouts into a ViewFlipper, and set the in and out animations on the ViewFlipper programatically.

Sample code:

xml (layout):

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical">
            <Button 
                android:layout_height="wrap_content"
                android:layout_width="fill_parent"
                android:text="click me"
                android:id="@+id/btn"
                />
            <ViewFlipper 
                android:layout_height="fill_parent"
                android:layout_width="fill_parent"
                android:id="@+id/viewFlipper"
                >
            <RelativeLayout 
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:id="@+id/journeyLandingView"
                android:background="#FFFFFF"
                >  
            </RelativeLayout>
            <RelativeLayout 
                android:layout_width="fill_parent" 
                android:layout_height="fill_parent"
                android:id="@+id/journeyRootView"
                android:background="#FFA500"
                >

            </RelativeLayout>
            </ViewFlipper>        
        </LinearLayout>

java (Activity):

    ViewFlipper vflipper;
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        vflipper = (ViewFlipper) findViewById(R.id.viewFlipper);
        vflipper.setInAnimation(this, android.R.anim.slide_in_left);
        vflipper.setOutAnimation(this, android.R.anim.slide_out_right);

        Button btn = (Button) findViewById(R.id.btn);
        btn.setOnClickListener(this);
    }

    public void onClick(View v) 
    {
        vflipper.showNext();
    }

You would be using a custom animation instead of the "android.R.anim.slide_in_left" and "android.R.anim.slide_out_right" i've used in the example code at lines:

        vflipper.setInAnimation(this, android.R.anim.slide_in_left);
        vflipper.setOutAnimation(this, android.R.anim.slide_out_right);

However, i'm not really good at making custom animations, so i'm leaving that to yourself. Really hope this helped :-)




回答2:


I ended up using RelativeLAYOUT instead of LINEARLAYOUT as a parent element and than used following code to animate.

TranslateAnimation slide = new TranslateAnimation(0, 0, 0, -1*Methods.screenHeight(this));   
             slide.setDuration(1000);   
             slide.setFillAfter(true);   
             journeyLandingView.startAnimation(slide); 
             slide = new TranslateAnimation(0, 0, Methods.screenHeight(this), 0);   
             slide.setDuration(1000);   
             slide.setFillAfter(true);   
             journeyRootView.startAnimation(slide);

public static int screenHeight(Context ctx) {
        DisplayMetrics displaymetrics = new DisplayMetrics();
        ((Activity) ctx).getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);                 
        return displaymetrics.heightPixels;
    }


来源:https://stackoverflow.com/questions/10293907/animating-relativelayout

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