Android常用控件ImageSwitcher使用方法详解

Android常用控件ImageSwitcher使用方法详解,第1张

概述图像切换器使用ImageSwitcher表示,用于实现类似于Windows *** 作系统下的“Windows照片查看器”中的上一张、下一张切换图片的功能。在使用ImageSwitcher时,必须实现ViewSwitcher.ViewFactory接口,并通过makeView()方

图像切换器使用ImageSwitcher表示,用于实现类似于windows *** 作系统下的“windows照片查看器”中的上一张、下一张切换图片的功能。在使用ImageSwitcher时,必须实现VIEwSwitcher.VIEwFactory接口,并通过makeVIEw()方法创建用于显示图片的ImageVIEw对象。makeVIEw()方法将返回一个显示图片的ImageVIEw。在使用ImageSwitcher组件时,还有一个非常重要的方法,那就是setimageResource()方法,改方法用于指定在ImageSwitcher中显示的图片资源。

第一步:XML布局文件的代码如下:

<ImageSwitcher  androID:ID="@+ID/im"  androID:layout_wIDth="wrap_content"  androID:layout_height="wrap_content"  androID:layout_below="@+ID/button1"  androID:layout_centerHorizontal="true"  androID:layout_margintop="99dp" ></ImageSwitcher><button  androID:ID="@+ID/button1"  androID:layout_wIDth="wrap_content"  androID:layout_height="wrap_content"  androID:layout_alignParenttop="true"  androID:layout_centerHorizontal="true"  androID:layout_margintop="77dp"  androID:text="上一张" /><button  androID:ID="@+ID/button2"  androID:layout_wIDth="wrap_content"  androID:layout_height="wrap_content"  androID:layout_alignleft="@+ID/button1"  androID:layout_below="@+ID/im"  androID:layout_margintop="51dp"  androID:text="下一张" />

第二步:在Java中编写逻辑代码,详细代码如下所示:

package com.example.imageswitcher;import androID.app.Activity;import androID.app.Actionbar;import androID.app.Fragment;import androID.os.Bundle;import androID.vIEw.LayoutInflater;import androID.vIEw.Menu;import androID.vIEw.MenuItem;import androID.vIEw.VIEw;import androID.vIEw.VIEw.OnClickListener;import androID.vIEw.VIEwGroup;import androID.Widget.button;import androID.Widget.ImageSwitcher;import androID.Widget.ImageVIEw;import androID.Widget.VIEwSwitcher.VIEwFactory;import androID.os.Build;public class MainActivity extends Activity {  private int[] imageID=new int[]{      R.drawable.bj,R.drawable.bj1,R.drawable.bj11,R.drawable.bj12  };  private int index=0;  private ImageSwitcher imageSwitcher;  @OverrIDe  protected voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.activity_main);   imageSwitcher=(ImageSwitcher) findVIEwByID(R.ID.im);     imageSwitcher.setInAnimation(this,androID.R.anim.fade_in);//设置淡入的动画   imageSwitcher.setoutAnimation(this,androID.R.anim.fade_out);//设置淡出的动画   imageSwitcher.setFactory(new VIEwFactory() {    @OverrIDe    public VIEw makeVIEw() {      ImageVIEw imageVIEw=new ImageVIEw(MainActivity.this);//创建一个Iv 的类      imageVIEw.setAdjustVIEwBounds(true);      imageVIEw.setScaleType(ImageVIEw.ScaleType.FIT_CENTER);//设置保持横纵比居中缩放图片      imageVIEw.setLayoutParams(new ImageSwitcher.LayoutParams(240,180));      return imageVIEw;    }  });  imageSwitcher.setimageResource(imageID[index]);//显示默认的图片  //获取两个按钮的ID  button up=(button) findVIEwByID(R.ID.button1);  button down=(button) findVIEwByID(R.ID.button2);  up.setonClickListener(new OnClickListener() {    @OverrIDe    public voID onClick(VIEw v) {    if(index>0){      index--;    }else{      index =imageID.length-1;    }    imageSwitcher.setimageResource(imageID[index]);//显示当前的图片    }  });  down.setonClickListener(new OnClickListener() {    @OverrIDe    public voID onClick(VIEw v) {    if(index < imageID.length-1){      index++;    }else{      index=0;    }    imageSwitcher.setimageResource(imageID[index]);//显示当前的图片    }  });  }}

第三步:用手机运行的结果如下所示:

感谢大家的阅读,如有错误和不足请指出,谢谢大家。

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

总结

以上是内存溢出为你收集整理的Android常用控件ImageSwitcher使用方法详解全部内容,希望文章能够帮你解决Android常用控件ImageSwitcher使用方法详解所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1144682.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-31
下一篇2022-05-31

发表评论

登录后才能评论

评论列表(0条)

    保存