软件编程
位置:首页>> 软件编程>> Android编程>> ImageSwitcher图像切换器的使用实例

ImageSwitcher图像切换器的使用实例

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

标签:ImageSwitcher,图像切换器

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

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com