📜  Android中的ViewSwitcher示例

📅  最后修改于: 2021-05-13 15:02:05             🧑  作者: Mango

所有android应用都将具有切换不同视图的功能,以说明/推广其网站或产品。通过显示不同的视图来直观表示产品,将轻松打动客户。在本文中,让我们看看如何将“ ViewSwitcher”引入Android。 ViewSwitcher是ViewAnimator的子类,用于在视图之间切换,从而为客户带来不同的视图。它是过渡小部件的一个元素,可帮助我们在视图上添加过渡。我们可以用它来动画屏幕上的视图。 ViewSwitcher甚至在两个不同的视图(即TextView,ImageView或任何布局)之间也可以平滑切换,并且由于此功能,可以通过在需要时交替显示TextView,Imageview来打动客户。在我们的示例中,让我们看一下ImageView本身。

ViewSwitcher初始化

Java
// We can intialize ViewSwitcher in the below way , where
// simpleViewSwitcher1
// is the id of ViewSwitcher in xml file. Usually xml file
// name will be
// activity_main but we can have meaningful name.
// let our xml file name be activity_viewswitcher
ViewSwitcher simpleViewSwitcher1 = (ViewSwitcher)findViewById(R.id.simpleViewSwitcher1);


XML
 

  

    
  
        
  
        
  
            
  
        
  
        
  
            
  
        
  
    
  
  
    


Java
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ViewSwitcher;
import androidx.appcompat.app.AppCompatActivity;
  
public class MainActivity extends AppCompatActivity {
  
    private ViewSwitcher simpleViewSwitcher;
    Button btnNext, btnPrev;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
          
        // get The references of Button and ViewSwitcher
        btnNext = (Button) findViewById(R.id.buttonNext);
        btnPrev = (Button) findViewById(R.id.buttonPrevious);
  
        // get the reference of ViewSwitcher
        simpleViewSwitcher = (ViewSwitcher) findViewById(R.id.simpleViewSwitcher); 
          
        // Declare in and out animations and load them using AnimationUtils class
        Animation in = AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left);
        Animation out = AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right);
  
        // set the animation type to ViewSwitcher
        simpleViewSwitcher.setInAnimation(in);
        simpleViewSwitcher.setOutAnimation(out);
          
        // ClickListener for NEXT button
        // When clicked on Button ViewSwitcher will switch between views
        // The current view will go out and next view will come in with specified animation
        btnNext.setOnClickListener(new View.OnClickListener() {
  
            public void onClick(View v) {
                // show the next view of ViewSwitcher
                simpleViewSwitcher.showNext();
            }
        });
  
        btnPrev.setOnClickListener(new View.OnClickListener() {
  
            public void onClick(View v) {
                // show the previous view of ViewSwitcher
                simpleViewSwitcher.showPrevious();
            }
        });
    }
}


让我们看看ViewSwitcher的重要方法,其功能以及其代码实现

重要方法

通用方法及其说明:

General Methods

Description

getNextView()

In order to return to the next view to be displayed in ViewSwitcher, 

the getNextView() method is used and it returns the view id of the 

next view to be displayed.

reset()

There may be a requirement to reset the ViewSwitcher on a click event 

and hence it behaves like the first-time animation has not yet played. 

reset() method is used for that.

showNext()

ViewSwitcher can have only 2 views. Only one view is shown at a time.

To show the next view, the required method is showNext()

showPrevious()

ViewSwitcher can have only 2 views. Only one view is shown at a time. 

To show the previous view, the required method is showPrevious()

动画相关方法:

Animation Related Methods

Description

loadAnimation(Context context, int id)

This method is used whenever we need to define an object of 

Animation class through AnimationUtilities class by calling a 

static method loadAnimation.

setInAnimation(loadIn) In order to set the animation of the appearance of the object on the screen
setOutAnimation(out)

When we show the next view, first view has to be removed and 

that is done by using setOutAnimation()

setFactory(ViewFactory factory)

It is used to create a new view for ViewSwitcher. The old one is 

replaced and a new view is created by using this method.

ViewSwitcher的属性

通过查看属性以及对项目有帮助的方法。

Attributes

Description

id To uniquely identify a ViewSwitcher.

padding

  • paddingRight
  • paddingLeft
  • paddingTop
  • paddingBottom
  • padding

This attribute is used to set the padding from the left, right, the top or bottom side of a ViewSwitcher

  • set the padding from the right side of a ViewSwitcher.
  •  set the padding from the left side of a ViewSwitcher.
  • set the padding from the top side of a ViewSwitcher
  • set the padding from the bottom side of a ViewSwitcher.
  • set the padding from all the sides of a ViewSwitcher. 
background

Set the background of a ViewSwitcher by this method. 

To make a pleasing appeal, the background has to be set.

我们可以在背景的背景中设置颜色或可绘制对象:

XML格式

 

  

    
  
        
  
        
  
            
  
        
  
        
  
            
  
        
  
    
  
  
    

步骤3:使用MainActivity。 Java文件

转到MainActivity。 Java文件并参考以下代码。下面是MainActivity的代码。 Java文件。在代码内部添加了注释,以更详细地了解代码。

Java

import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ViewSwitcher;
import androidx.appcompat.app.AppCompatActivity;
  
public class MainActivity extends AppCompatActivity {
  
    private ViewSwitcher simpleViewSwitcher;
    Button btnNext, btnPrev;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
          
        // get The references of Button and ViewSwitcher
        btnNext = (Button) findViewById(R.id.buttonNext);
        btnPrev = (Button) findViewById(R.id.buttonPrevious);
  
        // get the reference of ViewSwitcher
        simpleViewSwitcher = (ViewSwitcher) findViewById(R.id.simpleViewSwitcher); 
          
        // Declare in and out animations and load them using AnimationUtils class
        Animation in = AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left);
        Animation out = AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right);
  
        // set the animation type to ViewSwitcher
        simpleViewSwitcher.setInAnimation(in);
        simpleViewSwitcher.setOutAnimation(out);
          
        // ClickListener for NEXT button
        // When clicked on Button ViewSwitcher will switch between views
        // The current view will go out and next view will come in with specified animation
        btnNext.setOnClickListener(new View.OnClickListener() {
  
            public void onClick(View v) {
                // show the next view of ViewSwitcher
                simpleViewSwitcher.showNext();
            }
        });
  
        btnPrev.setOnClickListener(new View.OnClickListener() {
  
            public void onClick(View v) {
                // show the previous view of ViewSwitcher
                simpleViewSwitcher.showPrevious();
            }
        });
    }
}

输出

在android studio中运行android代码时,我们可以获取输出,如附件视频所示。这是一个有用的功能,可在许多android应用程序中使用。

想要一个节奏更快,更具竞争性的环境来学习Android的基础知识吗?
单击此处,前往由我们的专家精心策划的指南,以使您立即做好行业准备!