ImageSwitcher图像切换器的使用实例

作者:小龙 时间:2022-10-29 13:16:02 

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

描述

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

目标效果图如下所示:

ImageSwitcher图像切换器的使用实例

ImageSwitcher图像切换器的使用实例

ImageSwitcher图像切换器的使用实例

页面布局


<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;
 }

}

来源:https://blog.csdn.net/qq_36631076/article/details/78270066

标签:ImageSwitcher,图像切换器
0
投稿

猜你喜欢

  • Mybatis SqlSessionFactory与SqlSession详细讲解

    2021-12-24 22:42:56
  • SpringCloud Feign 服务调用的实现

    2023-09-18 11:07:35
  • java 算法之快速排序实现代码

    2023-01-30 01:44:59
  • EditText监听方法,实时的判断输入多少字符

    2021-08-16 11:32:36
  • C#最简单的字符串加密解密方法

    2022-10-30 12:46:36
  • C++编程中用put输出单个字符和cin输入流的用法

    2023-05-28 06:14:49
  • Android 使用SharePerference判断是否为第一次登陆的实现代码

    2021-07-31 18:51:32
  • GateWay动态路由与负载均衡详细介绍

    2022-02-10 15:12:29
  • Java计时器StopWatch实现方法代码实例

    2021-07-25 13:43:42
  • Spring boot如何配置请求的入参和出参json数据格式

    2023-10-23 17:27:48
  • springboot连接sqllite遇到的坑及解决

    2023-04-27 04:37:38
  • Spring的事务管理你了解吗

    2023-02-05 19:44:16
  • Java实现导入导出Excel文件的方法(poi,jxl)

    2021-09-13 21:12:35
  • 模仿美团点评的Android应用中价格和购买栏悬浮固定的效果

    2021-11-12 12:00:42
  • Java Shutdown Hook场景使用及源码分析

    2023-05-19 06:01:30
  • Android SurfaceView预览变形完美解决方法

    2021-11-14 10:29:22
  • idea 如何查找类中的某个方法

    2022-03-17 17:17:42
  • Android集成腾讯X5实现文档浏览功能

    2023-10-03 23:44:06
  • Android编程实现VideoView循环播放功能的方法

    2021-12-05 15:31:42
  • Android手势识别器GestureDetector使用详解

    2022-01-16 14:25:17
  • asp之家 软件编程 m.aspxhome.com