ImageSwitcher图像切换器的使用实例

2022-07-29,,

本文实例为大家分享了imageswitcher图像切换器的实现代码,供大家参考,具体内容如下

描述

在该实例中,提供一个图片切换器和两个点击按钮,用于切换图片,并用一个textview显示图片信息。其中,当前图片若为最后一张,点击下一张,则跳转到第一张;同理,第一张图片点击上一张,则显示最后一张图片,循环查看当前图片。

目标效果图如下所示:

页面布局

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/linearlayout1"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="@drawable/bg67"
  android:orientation="vertical"
  android:paddingbottom="@dimen/activity_vertical_margin"
  android:paddingleft="@dimen/activity_horizontal_margin"
  android:paddingright="@dimen/activity_horizontal_margin"
  android:paddingtop="@dimen/activity_vertical_margin"
  tools:context=".mainactivity" >

  <textview
    android:id="@+id/show"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginleft="20dp"
    android:layout_margintop="20dp"
    android:text="我是当前图片的信息~"
    android:textsize="24dp" />


  <linearlayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <imageswitcher 
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/image"
      android:layout_gravity="center"
      android:background="#666666">
    </imageswitcher>

    <linearlayout 
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="horizontal"
      android:gravity="center">

      <button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="上一张"
        android:layout_marginleft="20dp"
        android:textsize="24dp"
        android:id="@+id/up" />

      <button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="下一张"
        android:layout_marginleft="20dp"
        android:textsize="24dp"
        android:id="@+id/down" />

    </linearlayout>

  </linearlayout>
</linearlayout>

事件响应

package com.example.imageswitchdemo;

import android.os.bundle;
import android.app.activity;
import android.view.menu;
import android.view.view;
import android.view.view.onclicklistener;
import android.view.animation.animation;
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.viewfactory;

public class mainactivity extends activity
{
  textview show=null;
  button up,dowm=null;
  imageswitcher image=null;
  private int[] images=new int[]{r.drawable.a001,r.drawable.a002,r.drawable.a003,
                  r.drawable.a004,r.drawable.a005,r.drawable.a006,
                  r.drawable.a007,r.drawable.a008,r.drawable.a009};
  private int index=0;

  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_main);

    //获取控件
    show=(textview) findviewbyid(r.id.show);
    up=(button) findviewbyid(r.id.up);
    dowm=(button) findviewbyid(r.id.down);
    image=(imageswitcher) findviewbyid(r.id.image);

    //为获取到的控件添加显示效果:淡入动画和淡出动画
    image.setinanimation(animationutils.loadanimation(this, android.r.anim.fade_in));
    image.setoutanimation(animationutils.loadanimation(this, android.r.anim.fade_out));

    //为图像切换器设置一个viewfactory,并重写makeview方法
    image.setfactory(new viewfactory()
    {

      @override
      public view makeview()
      {
        //指定视图切换工程
        return new imageview(mainactivity.this);
      }
    });
    image.setimageresource(images[index]);
    show.settext("一共有"+images.length+"张图片,当前是第"+(index+1)+"张图片");

    //当点击按钮时,图像切换并显示相应的信息
    up.setonclicklistener(new onclicklistener()
    {

      @override
      public void onclick(view arg0)
      {
        if(index>0)
          index--;
        else
          index=images.length-1;

        image.setimageresource(images[index]);
        show.settext("一共有"+images.length+"张图片,当前是第"+(index+1)+"张图片");
      }
    });

    //同理,当点击按钮时,图像切换并显示相应的信息
    dowm.setonclicklistener(new onclicklistener()
    {
      public void onclick(view arg0)
      {
        if(index<images.length-1)
          index++;
        else
          index=0;

        image.setimageresource(images[index]);
        show.settext("一共有"+images.length+"张图片,当前是第"+(index+1)+"张图片");
      }
    });
  }

  @override
  public boolean oncreateoptionsmenu(menu menu) {
    // inflate the menu; this adds items to the action bar if it is present.
    getmenuinflater().inflate(r.menu.main, menu);
    return true;
  }

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

《ImageSwitcher图像切换器的使用实例.doc》

下载本文的Word格式文档,以方便收藏与打印。