Android练习-----图像切换器(ImageSwitcher)

来源:互联网 发布:linux xorg 安装 编辑:程序博客网 时间:2024/06/05 00:45

图像切换器 ( ImageSwitcher ) 用于实现图片的切换。在使用 ImageSwitcher 控件时, 必须实现 View.Switcher.ViewFactory 接口,并通过 makeView()方法来创建用于显示图片的 ImageView。 makeView() 方法将返回一个显示图片的 ImageView。在使用图像切换器时使用 setImageResource() 方法来指定要在 ImageSwitcher 中显示的图片资源。


在 Android Stdio 中创建一个 Android 项目,名称为 Ch06_06,实现一个使用图像切换器控件的实例。
(1)首先准备一些图片文件,然后放在项目 res 目录下的 drawable 文件夹中,作为图片切换器显示的图片资源。
(2)在项目的 res/layout 目录下修改 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">    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/text"/></LinearLayout>

(3)添加一个线性布局,并在其中添加两个 Button 控件和一个 ImageSwitcher 控件,其代码如下所示:

<LinearLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="horizontal"        android:gravity="center">        <Button            android:layout_width="60dp"            android:layout_height="wrap_content"            android:id="@+id/pre"            android:text="pre"/>        <ImageSwitcher            android:layout_width="220dp"            android:layout_height="300dp"            android:id="@+id/imageSwitch"            android:layout_gravity="center"            android:background="#666666">        </ImageSwitcher>        <Button            android:layout_width="60dp"            android:layout_height="wrap_content"            android:id="@+id/next"            android:text="next"/>    </LinearLayout>

(4)在 MainActivity.java 文件中,首先获取到按钮、文本框和图像切换器控件对象,为获取到的 ImageSwitcher 控件添加显示效果,然后为其设置一个 ViewFactory ,并重写 makeView() 方法。其主要代码如下:

import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.view.animation.AnimationUtils;import android.widget.Button;import android.widget.ImageSwitcher;import android.widget.ImageView;import android.widget.TextView;import android.widget.ViewSwitcher;public class MainActivity extends AppCompatActivity {    private Button pre = null;    private Button next = null;    private ImageSwitcher imageSwitch = null;    private int index = 0;    private TextView text = null;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        text = (TextView) findViewById(R.id.text);        pre = (Button) findViewById(R.id.pre);        next = (Button) findViewById(R.id.next);        imageSwitch = (ImageSwitcher) findViewById(R.id.imageSwitch);        final int[] images = new int[] {R.drawable.background3,R.drawable.logo2,                                                R.drawable.logo3,R.drawable.logo4,R.drawable.logo5};        imageSwitch.setInAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_in));        imageSwitch.setInAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_out));        imageSwitch.setFactory(new ViewSwitcher.ViewFactory() {            @Override            public View makeView() {                return new ImageView(MainActivity.this);            }        });        imageSwitch.setImageResource(images[index]);        text.setText("一共有" + images.length + "图片,当前是第" + (index +1 ) + "张图片");        pre.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                if (index > 0 ) {                    index --;                }else{                    index = images.length - 1 ;                }                imageSwitch.setImageResource(images[index]);                text.setText("一共有" + images.length + "图片,当前是第" + (index +1 ) + "张图片");            }        });        next.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                if ( index < images.length -1 ){                    index ++ ;                }else {                    index = 0;                }                imageSwitch.setImageResource(images[index]);                text.setText("一共有" + images.length + "图片,当前是第" + (index +1 ) + "张图片");            }        });    }}

在上述代码中,使用 ImageSwitcher 类的父类 ViewAnimator 的 setInAnimation() 方法和 setOutAnimation() 方法为图像切换器设置动画效果。调用父类 ViewSwitcher 的 setFactory() 方法指定视图切换工程,其参数为 ViewSwitcher.ViewFactory 类型的对象。


运行该项目:

这里写图片描述 这里写图片描述

0 0
原创粉丝点击