📜  如何在Android中添加ViewFlipper?

📅  最后修改于: 2021-05-13 16:35:54             🧑  作者: Mango

本文介绍了ViewFipper在android中的实现。它是ViewAnimator类的扩展,有助于在添加到它的视图之间建立动画。 ViewFlipper使切换视图变得容易。为了控制视图之间的翻转,ViewFlipper提供了两种方法startFlipping()stopFlipping() 。到的视图之间自动切换,添加AUTOSTART标签和其值设置为true.To给予更多的控制,以用户,动态地在ViewFlipper添加视图。可以在图库应用程序中使用ViewFlipper在图像或视频之间导航。

方法:

  1. 创建一个新的Android资源目录。右键单击res文件夹,然后选择“ Android资源目录”。确保选择资源类型作为anim
  2. 现在,在anim目录中创建一个新的slide_left.xml文件,并添加以下代码。这是将在切换视图时使用的动画。
    slide_left.xml
    
    
        
        
    


    slide_right.xml
    
    
        
        
    


    activity_main.xml
    
    
      
        
      
            
      
            
      
            


    MainActivity.java
    package org.geeksforgeeks.gfgviewflipper;
      
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.ViewFlipper;
    import androidx.appcompat.app.AppCompatActivity;
      
    public class MainActivity extends AppCompatActivity {
        ViewFlipper flipper;
        Button prev_Button, next_Button;
      
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
      
            flipper = findViewById(R.id.view_flipper);
            prev_Button = findViewById(R.id.prev_button);
            next_Button = findViewById(R.id.next_button);
      
            prev_Button.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v)
                    {
                        // It is used to set the in and out
                        // animation of View flipper.
                        flipper.setInAnimation(MainActivity.this,
                                               R.anim.slide_right);
                        flipper.setOutAnimation(MainActivity.this,
                                                R.anim.slide_left);
      
                        // It shows previous item.
                        flipper.showPrevious();
                    }
                });
      
            next_Button.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v)
                    {
                        // It is used to set the in and out
                        // animation of View flipper.
                        flipper.setInAnimation(MainActivity.this,
                                               android.R.anim.slide_left);
                        flipper.setOutAnimation(MainActivity.this,
                                                android.R.anim.slide_right);
      
                        // It shows next item.
                        flipper.showNext();
                    }
                });
        }
    }


  3. 现在,在anim目录中创建一个新的slide_right.xml文件,并添加以下代码。这是将用于切换视图的动画。

    slide_right.xml

    
    
        
        
    
    
  4. activity_main.xml文件中添加以下代码。在此文件中, ViewFlipper被添加到布局中。添加到视图翻转器中的所有小部件都将充当不同的视图。还添加了下一个一个两个按钮。

    activity_main.xml

    
    
      
        
      
            
      
            
      
            
  5. 现在,在MainActivity中添加以下代码。 Java文件。上一个下一个按钮将帮助我们在视图之间切换。在以前的按钮,用于动画slide_right被使用并且为使用和副动画slide_left反之亦然为下一个按钮。

    主要活动。Java

    package org.geeksforgeeks.gfgviewflipper;
      
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.ViewFlipper;
    import androidx.appcompat.app.AppCompatActivity;
      
    public class MainActivity extends AppCompatActivity {
        ViewFlipper flipper;
        Button prev_Button, next_Button;
      
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
      
            flipper = findViewById(R.id.view_flipper);
            prev_Button = findViewById(R.id.prev_button);
            next_Button = findViewById(R.id.next_button);
      
            prev_Button.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v)
                    {
                        // It is used to set the in and out
                        // animation of View flipper.
                        flipper.setInAnimation(MainActivity.this,
                                               R.anim.slide_right);
                        flipper.setOutAnimation(MainActivity.this,
                                                R.anim.slide_left);
      
                        // It shows previous item.
                        flipper.showPrevious();
                    }
                });
      
            next_Button.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v)
                    {
                        // It is used to set the in and out
                        // animation of View flipper.
                        flipper.setInAnimation(MainActivity.this,
                                               android.R.anim.slide_left);
                        flipper.setOutAnimation(MainActivity.this,
                                                android.R.anim.slide_right);
      
                        // It shows next item.
                        flipper.showNext();
                    }
                });
        }
    }
    

输出:

在ViewFlipper中添加autoStart标记,并将其值设置为true。然后它将像这样工作。