android- How to animate between view flipper chang

2019-05-30 16:10发布

问题:

this is how I made my slidshow by using viewFlipper :

    private void gallery() {
    imageViewFlipper = (ViewFlipper) findViewById(R.id.main_flipper);
    File sdcardPath = new File(Environment.getExternalStorageDirectory().getPath() + "/Pictures/picFolder");
    String sdcard =  sdcardPath .getPath(); 
     setImagesToFlipper(imageViewFlipper, sdcardPath);

      runnable = new Runnable() {

       public void run() {

        handler.postDelayed(runnable, 3000);
        imageViewFlipper.showNext();

       }
      };
      handler = new Handler();
      handler.postDelayed(runnable, 500);
}
private void setImagesToFlipper(ViewFlipper flipper, File sdcardPath) {
      int imageCount = sdcardPath.listFiles().length;
      for (int count = 0; count < imageCount - 1; count++) {
       ImageView imageView = new ImageView(this);
       Bitmap bmp = BitmapFactory.decodeFile(sdcardPath.listFiles()[count].getAbsolutePath());
       imageView.setImageBitmap(bmp);
       flipper.addView(imageView);
      }
}

I need to animate between changing the images , Now ,it just changes the images suddenly and it doesn't look good.

How can I animate it ?

thanks you

回答1:

Refer these links, they also have the animation xml's:

Link 1 & Link 2

Example Class:

public class ViewFlipperMainActivity extends Activity
{
    private ViewFlipper viewFlipper;
    private float lastX;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
                 super.onCreate(savedInstanceState);
                 setContentView(R.layout.view_flipper_main);
                 viewFlipper = (ViewFlipper) findViewById(R.id.view_flipper);
    }



    // Method to handle touch event like left to right swap and right to left swap
    public boolean onTouchEvent(MotionEvent touchevent)
    {
     switch (touchevent.getAction())
     {
        // when user first touches the screen to swap
         case MotionEvent.ACTION_DOWN:
         {
             lastX = touchevent.getX();
             break;
        }
         case MotionEvent.ACTION_UP:
         {
         float currentX = touchevent.getX();

         // if left to right swipe on screen
         if (lastX < currentX)
         {
              // If no more View/Child to flip
             if (viewFlipper.getDisplayedChild() == 0)
                 break;

             // set the required Animation type to ViewFlipper
             // The Next screen will come in form Left and current Screen will go OUT from Right
             viewFlipper.setInAnimation(this, R.anim.in_from_left);
             viewFlipper.setOutAnimation(this, R.anim.out_to_right);
             // Show the next Screen
             viewFlipper.showNext();
         }

         // if right to left swipe on screen
         if (lastX > currentX)
         {
             if (viewFlipper.getDisplayedChild() == 1)
                 break;
             // set the required Animation type to ViewFlipper
             // The Next screen will come in form Right and current Screen will go OUT from Left
             viewFlipper.setInAnimation(this, R.anim.in_from_right);
             viewFlipper.setOutAnimation(this, R.anim.out_to_left);
             // Show The Previous Screen
             viewFlipper.showPrevious();
         }
         break;
         }
        }
     return false;
    }
}

Animation Xml:

Fade In:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
  android:interpolator="@android:anim/linear_interpolator">
  <alpha
      android:fromAlpha="0.1"
      android:toAlpha="1.0"
      android:duration="2000"
      />
</set>

Fade Out:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
  android:interpolator="@android:anim/linear_interpolator">
  <alpha
      android:fromAlpha="1.0"
      android:toAlpha="0.1"
      android:duration="2000"
      />
</set>

in_from_left.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
    <translate
        android:fromXDelta="-100%" android:toXDelta="0%"
           android:fromYDelta="0%" android:toYDelta="0%"
           android:duration="1400" />
</set>

in_from_right.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
    <translate
        android:fromXDelta="100%" android:toXDelta="0%"
           android:fromYDelta="0%" android:toYDelta="0%"
           android:duration="1400" />
</set>

out_to_left.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
      <translate android:fromXDelta="0%" android:toXDelta="-100%"
        android:fromYDelta="0%" android:toYDelta="0%"
        android:duration="1400"/>
</set>

out_to_right.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
      <translate android:fromXDelta="0%" android:toXDelta="100%"
        android:fromYDelta="0%" android:toYDelta="0%"
        android:duration="1400"/>
</set>