如何在布局中制作从右到左的动画。

14

我遇到了一个从右往左滑动的问题。我通过点击按钮时使用“hide”和“visible”布局来创建它。但是现在我想要一种动画效果,这样,当我点击按钮时,会发生“从右到左”的动画效果,并且当我再次点击按钮时,相同的布局将会以“从左到右”的动画效果工作,就像幻灯片一样。

代码:

final RelativeLayout rightlayout = (RelativeLayout) findViewById(R.id.relativelayout2);
rightlayout.setVisibility(View.GONE);

Button rightbtn = (Button) findViewById(R.id.righttoleft);

rightbtn.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        System.out.println("clickecd");
        if(keyright == 0) {
            //rightlayout.setVisibility(View.VISIBLE);
            keyright++;
            slideToLeft(rightlayout);

        } else
        {
            //rightlayout.setVisibility(View.GONE);
            TranslateAnimation animate = new TranslateAnimation(0,rightlayout.getWidth(),0,0);
            animate.setDuration(1000);
            animate.setFillAfter(true);
            rightlayout.startAnimation(animate);
            keyright--;
        }

    }
});

// To animate view slide out from left to right
public void slideToRight(View view){
TranslateAnimation animate = new TranslateAnimation(0,view.getWidth(),0,0);
animate.setDuration(500);
animate.setFillAfter(true);
view.startAnimation(animate);

}
// To animate view slide out from right to left
public void slideToLeft(View view){
TranslateAnimation animate = new TranslateAnimation(0,-view.getWidth(),0,0);
animate.setDuration(500);
animate.setFillAfter(true);
view.startAnimation(animate);

}

XML:

<RelativeLayout
    android:id="@+id/rightsidemenu"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/frameLayout1"
    android:gravity="right"
    android:orientation="vertical" >

    <Button
        android:id="@+id/righttoleft"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="100dp"
        android:background="@drawable/right_switcher_expanded" />

    <RelativeLayout
        android:id="@+id/relativelayout2"
        android:layout_width="50dp"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:layout_toRightOf="@+id/righttoleft"
        android:background="#EDEEF3"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:orientation="vertical"
            android:padding="5dp" >

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="10dp"
                android:background="#ffffff"
                android:gravity="right"
                android:orientation="vertical" >

                <FrameLayout
                    android:id="@+id/titleframe"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="#F4F4F4"
                    android:orientation="horizontal" >

                    <TextView
                        android:id="@+id/firsttitle"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical"
                        android:text="Job and Qualification"
                        android:textColor="#56A91E" />
                </FrameLayout>

                <FrameLayout
                    android:id="@+id/firstframe1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/titleframe"
                    android:layout_margin="10dp"
                    android:orientation="horizontal" >

                    <TextView
                        android:id="@+id/textView81"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Highest qualification" />

                    <TextView
                        android:id="@+id/higesttxt"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="right"
                        android:text="highest qualification" />
                </FrameLayout>

                <FrameLayout
                    android:id="@+id/thirdff"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/firstframe1"
                    android:layout_margin="10dp" >

                    <TextView
                        android:id="@+id/textView10"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Occupation" />

                    <TextView
                        android:id="@+id/occupationtxt"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="right"
                        android:text="occupation" />
                </FrameLayout>

                <FrameLayout
                    android:id="@+id/thirdfff"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/thirdff"
                    android:layout_marginBottom="20dp"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp"
                    android:layout_marginTop="10dp" >

                    <TextView
                        android:id="@+id/textView100"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="CTC (Income)" />

                    <TextView
                        android:id="@+id/incometxt"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="right"
                        android:text="income" />
                </FrameLayout>
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="10dp"
                android:background="#ffffff"
                android:gravity="right"
                android:orientation="vertical" >

                <FrameLayout
                    android:id="@+id/titleframe"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="#F4F4F4"
                    android:orientation="horizontal" >

                    <TextView
                        android:id="@+id/firsttitle"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical"
                        android:text="Family Info"
                        android:textColor="#56A91E" />
                </FrameLayout>

                <FrameLayout
                    android:id="@+id/firstframe11"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/titleframe"
                    android:layout_margin="10dp"
                    android:orientation="horizontal" >

                    <TextView
                        android:id="@+id/textView811"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Have children" />

                    <TextView
                        android:id="@+id/havechildrntxt"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="right"
                        android:text="have children" />
                </FrameLayout>

                <FrameLayout
                    android:id="@+id/secondfff"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/firstframe11"
                    android:layout_margin="10dp" >

                    <TextView
                        android:id="@+id/textView91"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Father Alive" />

                    <TextView
                        android:id="@+id/fathrtxt"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="right"
                        android:text="Father Alive" />
                </FrameLayout>

                <FrameLayout
                    android:id="@+id/thirdfff"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/secondfff"
                    android:layout_margin="10dp" >

                    <TextView
                        android:id="@+id/textView101"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Mother Alive" />

                    <TextView
                        android:id="@+id/mothertxt"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="right"
                        android:text="Mother alive" />
                </FrameLayout>

                <FrameLayout
                    android:id="@+id/thirdffff"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/thirdfff"
                    android:layout_marginBottom="10dp"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp"
                    android:layout_marginTop="10dp" >

                    <TextView
                        android:id="@+id/textView103"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Brother" />

                    <TextView
                        android:id="@+id/brothercounttxt"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="right"
                        android:text="Brother" />
                </FrameLayout>

                <FrameLayout
                    android:id="@+id/thirdffff3"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/thirdffff"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp" >

                    <TextView
                        android:id="@+id/textView104"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Sister" />

                    <TextView
                        android:id="@+id/sistercounttxt"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="right"
                        android:text="Sister" />
                </FrameLayout>
            </RelativeLayout>
        </LinearLayout>
    </RelativeLayout>

5个回答

44

这段代码可能对你有所帮助。

左侧滑动动画:

在 res/anim/ 目录下创建一个名为 anim_left 的文件。

    <translate xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="500"
        android:fromXDelta="100%"
        android:toXDelta="0%" >
    </translate>
    

右滑动画:

在res/anim/目录下创建一个名为anim_right的文件

    <translate xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="500"
        android:fromXDelta="-100%"
        android:toXDelta="0%" >
    </translate>
    

在Activity中使用:

Animation rightSwipe = AnimationUtils.loadAnimation(this, R.anim.anim_right);
<view>.startAnimation(rightSwipe);

感谢工作。但有一点问题。按钮附加在布局上。当我单击时,从右到左工作,现在我想再次单击按钮。布局应该回到(当前位置隐藏)。 - user1629977
请检查以下代码:-- TranslateAnimation animate = new TranslateAnimation(0,-0,0,0); animate.setDuration(600); animate.setFillAfter(true); leftlayout.startAnimation(animate); 我正在尝试通过动画隐藏布局,请帮忙。 - user1629977
我们可以将两个幻灯片混合成一个吗?这意味着它会播放第一张幻灯片,当第一张幻灯片的播放时间结束后,它会继续播放第二张幻灯片。 - Twitter khuong291
但我只想通过它的宽度滑动,我们该怎么做? - AbhayBohra
@AbhayBohra 这应该是一个单独的问题。 - Shailendra Madda

5
public void slidefromRightToLeft(View view) {

        TranslateAnimation animate;
        if (view.getHeight() == 0) {
            main_layout.getHeight(); // parent layout
            animate = new TranslateAnimation(main_layout.getWidth()/2,
                    0, 0, 0);
        } else {
            animate = new TranslateAnimation(view.getWidth(),0, 0, 0); // View for animation
        }

        animate.setDuration(500);
        animate.setFillAfter(true);
        view.startAnimation(animate);
        view.setVisibility(View.VISIBLE); // Change visibility VISIBLE or GONE
    }

4

针对那些正在寻找从右到左动画的解决方案:

TranslateAnimation animation1 = new TranslateAnimation(0.0f, 0.0f, 1500.0f, 0.0f); // new TranslateAnimation(xFrom,xTo, yFrom,yTo)
animation1.setDuration(1500); // animation duration
animation1.setRepeatCount(4); // animation repeat count if u want to repeat
animation1.setFillAfter(true);
your_view .startAnimation(animation);//your_view for mine is imageView     

针对那些寻求重复动画的人(例如从左到右和从右到左),这里提供解决方案。

TranslateAnimation animation = new TranslateAnimation(0.0f, 0.0f, 0.0f, 1500.0f); // new TranslateAnimation (float fromXDelta,float toXDelta, float fromYDelta, float toYDelta)

animation.setDuration(1500); // animation duration
animation.setRepeatCount(8); // animation repeat count
animation.setRepeatMode(2); // repeat animation (left to right, right to left)

animation.setFillAfter(true);
your_view.startAnimation(animation);//your_view for mine is imageView 

2
主布局和背景布局是两个线性布局,在点击按钮时调用startanimation方法。
boolean menuOut=false;
public void startAnimation()
    {
    Animation anim;
    int w = mainLayout.getMeasuredWidth();
                     int h = mainLayout.getMeasuredHeight();
                     int left = (int) (mainLayout.getMeasuredWidth() * 0.80);

    if(!menuOut)
    {
     anim = new TranslateAnimation(0, -left, 0, 0);
                         backLayout.setVisibility(View.VISIBLE);
                         animParams.init(-left, 0, -left + w, h);
    }
    else
    {
    anim = new TranslateAnimation(0, left, 0, 0);
                         animParams.init(0, 0, w, h);
    }

     anim.setDuration(400);
                     anim.setAnimationListener(Sample.this);
                     anim.setFillAfter(true);
                     mainLayout.startAnimation(anim);
}

AnimParams.java

public class AnimParams {

    int left, right, top, bottom;
    void init(int left, int top, int right, int bottom) {
        this.left = left;
        this.top = top;
        this.right = right;
        this.bottom = bottom;
    }

}

0

你也可以使用 animate().translationXBy([n_pixels]f).

其中,n_pixels] > 0 将对象向左移动,n_pixels] < 0 将对象向右移动。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接