本文实例为大家分享了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;
  }

}

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

ImageSwitcher图像切换器的使用实例的更多相关文章

  1. Android开发之ImageSwitcher相册功能实例分析

    这篇文章主要介绍了Android开发之ImageSwitcher相册功能,结合实例形式分析了Android ImageSwitcher相册的原理、使用方法及相关操作注意事项,需要的朋友可以参考下

  2. ImageSwitcher图像切换器的使用实例

    这篇文章主要为大家详细介绍了ImageSwitcher图像切换器的使用实例,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  3. Android中Gallery和ImageSwitcher的使用实例

    今天小编就为大家分享一篇关于Android中Gallery和ImageSwitcher的使用实例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧

  4. Android图像切换器imageSwitcher的实例应用

    这篇文章主要为大家详细介绍了Android图像切换器imageSwitcher的实例应用,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  5. Android实现图像切换器

    这篇文章主要为大家详细介绍了Android实现图像切换器,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

随机推荐

  1. Flutter 网络请求框架封装详解

    这篇文章主要介绍了Flutter 网络请求框架封装详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  2. Android单选按钮RadioButton的使用详解

    今天小编就为大家分享一篇关于Android单选按钮RadioButton的使用详解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧

  3. 解决android studio 打包发现generate signed apk 消失不见问题

    这篇文章主要介绍了解决android studio 打包发现generate signed apk 消失不见问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

  4. Android 实现自定义圆形listview功能的实例代码

    这篇文章主要介绍了Android 实现自定义圆形listview功能的实例代码,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  5. 详解Android studio 动态fragment的用法

    这篇文章主要介绍了Android studio 动态fragment的用法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  6. Android用RecyclerView实现图标拖拽排序以及增删管理

    这篇文章主要介绍了Android用RecyclerView实现图标拖拽排序以及增删管理的方法,帮助大家更好的理解和学习使用Android,感兴趣的朋友可以了解下

  7. Android notifyDataSetChanged() 动态更新ListView案例详解

    这篇文章主要介绍了Android notifyDataSetChanged() 动态更新ListView案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下

  8. Android自定义View实现弹幕效果

    这篇文章主要为大家详细介绍了Android自定义View实现弹幕效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  9. Android自定义View实现跟随手指移动

    这篇文章主要为大家详细介绍了Android自定义View实现跟随手指移动,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  10. Android实现多点触摸操作

    这篇文章主要介绍了Android实现多点触摸操作,实现图片的放大、缩小和旋转等处理,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

返回
顶部