web-dev-qa-db-fra.com

Comment créer une animation de droite à gauche dans un modèle

Je suis impliqué dans un problème de glissement de droite à gauche. Je l'ai créé par "masquer" et "visible" mise en page sur le bouton cliqué. Mais maintenant, je veux une animation de sorte que lorsque je clique sur le bouton, une animation "de droite à gauche" se produise et que lorsque je clique de nouveau sur le bouton, la même présentation soit "gauche à droite", une animation et fonctionne comme une présentation de diapositive.

code

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>
10
user1629977

Ce code peut-être vous aider.

Animation diapositive gauche:

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

Animation diapo droite:

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

Utilisez-le comme:

Animation RightSwipe = AnimationUtils.loadAnimation(Screen.this, R.anim.right_swipe);
ScreenAnimation.startAnimation(RightSwipe);
34
Shylendra Madda
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
    }
3
Vaibhav Jain

La disposition principale et la disposition arrière sont deux dispositions linéaires. Lorsque vous cliquez sur le bouton, appelez la méthode 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;
    }

}
2
Sravanya

Solution pour ceux qui recherchent une animation de droite à gauche:

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     

Solution pour ceux qui recherchent une animation répétée (par exemple, de gauche à droite et de droite à gauche)

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 
1
Andrew Coder

Vous pouvez également utiliser animate().translationXBy([n_pixels]f).

n_pixels] > 0 déplace l'objet LTR, n_pixels] < 0 déplace l'objet RTL.

0
Kobbi Gal