Open top menu








Implement  Flip Animation In Android?

Step1. Create anim Folder inside res folder.

Step2.Create file inside anim/from_middle.xml and to_middle.xml folder.

from_middle.xml .
............................................................................................................
xml version="1.0" encoding="utf-8"?>
<scale
    xmlns:android="http://schemas.android.com/apk/res/android"
   android:fromXScale="0.0" 
   android:toXScale="1.0"
   android:pivotX="50%" 
   android:fromYScale="1.0" 
   android:toYScale="1.0"
   android:pivotY="50%"         
   android:duration="250" />
............................................................................................................



to_middle.xml.
............................................................................................................
xml version="1.0" encoding="utf-8"?>
<scale
    xmlns:android="http://schemas.android.com/apk/res/android"
   android:fromXScale="1.0" 
   android:toXScale="0.0"
   android:pivotX="50%" 
   android:fromYScale="1.0" 
   android:toYScale="1.0"
   android:pivotY="50%"        
   android:duration="250" />
............................................................................................................

activity_main.xml.
............................................................................................................
xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dip"
        android:text="Click On Chnage Buton"
        android:textSize="26sp"
        android:textStyle="bold" />

    <ImageView
        android:id="@+id/Img_back_And_front"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:src="@drawable/front" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dip"
        android:text="Change"
        />

</LinearLayout>
............................................................................................................


Main_Activity.java.
............................................................................................................
public class MainActivity extends AppCompatActivity implements 
        View.OnClickListener, Animation.AnimationListener {

    private Animation animation_Front;
    private Animation animation_Back;
    private boolean isBackOfCardShowing = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        animation_Front = AnimationUtils.loadAnimation(this, R.anim.to_middle);
        animation_Front.setAnimationListener(this);
        animation_Back = AnimationUtils.loadAnimation(this, R.anim.from_middle);
        animation_Back.setAnimationListener(this);
        findViewById(R.id.button1).setOnClickListener(this);

    }



    @Override

    public void onClick(View v) {

        v.setEnabled(false);
        ((ImageView)findViewById(R.id.Img_back_And_front)).clearAnimation();
        ((ImageView)findViewById(R.id.Img_back_And_front)).setAnimation(animation_Front);
        ((ImageView)findViewById(R.id.Img_back_And_front)).startAnimation(animation_Front);

    }





    @Override

    public void onAnimationEnd(Animation animation) {
        if (animation==animation_Front) {
            if (isBackOfCardShowing) {
                ((ImageView)findViewById(R.id.Img_back_And_front)).
                               setImageResource(R.drawable.front);
            } else {
                ((ImageView)findViewById(R.id.Img_back_And_front)).
                              setImageResource(R.drawable.back);
            }

            ((ImageView)findViewById(R.id.Img_back_And_front)).clearAnimation();
            ((ImageView)findViewById(R.id.Img_back_And_front)).setAnimation(animation_Back);
            ((ImageView)findViewById(R.id.Img_back_And_front)).startAnimation(animation_Back);

        } else {
            isBackOfCardShowing=!isBackOfCardShowing;
            findViewById(R.id.button1).setEnabled(true);
        }

    }





    @Override
    public void onAnimationRepeat(Animation animation) {
        // TODO Auto-generated method stub

        }



    @Override
    public void onAnimationStart(Animation animation) {
        // TODO Auto-generated method stub

    }

}
............................................................................................................





try this code...
Tagged

0 comments