android-viewflipper的简单淡出和淡入动画

android-viewflipper的简单淡出和淡入动画,android,android-animation,android-viewflipper,Android,Android Animation,Android Viewflipper,我是android新手,对android动画知之甚少。 我有一个viewflipper,我想在其中的图像之间设置动画。 代码如下: runnable = new Runnable() { public void run() { handler.postDelayed(runnable, 3000); imageViewFlipper.setInAnimation(fadeIn); imageViewF

我是android新手,对android动画知之甚少。 我有一个viewflipper,我想在其中的图像之间设置动画。 代码如下:

 runnable = new Runnable() {
           public void run() {
            handler.postDelayed(runnable, 3000);
            imageViewFlipper.setInAnimation(fadeIn);
            imageViewFlipper.setOutAnimation(fadeOut);
            imageViewFlipper.showNext();
           }
          };
          handler = new Handler();
          handler.postDelayed(runnable, 500);
    }
2个动画文件不好,它们的动画效果很差。 我只需要一个代码淡出前一幅图像和淡入下一幅图像,并对其中的所有图像执行相同的操作

有人能帮我吗


谢谢您参考这些链接,它们还有动画xml:

&

示例类:

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;
    }
}
动画Xml:

淡入:

<?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>
<?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>

使用Kotlin很简单

首先,设置动画:

private fun setAnimations() {
    // in anim
    val inAnim = AlphaAnimation(0f, 1f)
    inAnim.duration = 600
    flipperView.inAnimation = inAnim

    // out anim
    val outAnim = AlphaAnimation(1f, 0f)
    outAnim.duration = 600
    flipperView.outAnimation = outAnim

}
要(在两个视图之间)翻转,只需调用此函数:

fun onSendClick(view: View) {
    viewFlipper.displayedChild = if(flipperView.displayedChild == 0) 1 else 0
}

尝试本教程:非常方便,可以将它们全部放在这里:)我认为值得一提的是,您还可以在
ViewFlipper
xml
android:inaimation=“…”
android:outAnimation=“…”
<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>
<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>
<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>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ViewFlipper
        android:id="@+id/view_flipper"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <!--  The child Views/Layout to flip -->
        <ImageView
            android:layout_marginTop="15dp"
            android:id="@+id/imageView1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/image1" />
        <ImageView
            android:layout_marginTop="15dp"
            android:id="@id/imageView1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/image2" />
    </ViewFlipper>
</LinearLayout>
private fun setAnimations() {
    // in anim
    val inAnim = AlphaAnimation(0f, 1f)
    inAnim.duration = 600
    flipperView.inAnimation = inAnim

    // out anim
    val outAnim = AlphaAnimation(1f, 0f)
    outAnim.duration = 600
    flipperView.outAnimation = outAnim

}
fun onSendClick(view: View) {
    viewFlipper.displayedChild = if(flipperView.displayedChild == 0) 1 else 0
}