欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android入门之Gallery+ImageSwitcher用法实例解析

程序员文章站 2022-05-16 12:44:08
继上一篇介绍了如何使用gallery控件之后,本文就来讲一下gallery 与imageswitcher的结合使用。本文所述实例代码将实现一个简单的浏览图片的功能。 先贴...

继上一篇介绍了如何使用gallery控件之后,本文就来讲一下gallery 与imageswitcher的结合使用。本文所述实例代码将实现一个简单的浏览图片的功能。

先贴出程序运行截图如下:

Android入门之Gallery+ImageSwitcher用法实例解析

除了gallery可以拖拉切换图片,我在imageswitcher控件加入了setontouchlistener事件实现,使得imageswitcher也可以在拖拉中切换图片。本例子依然使用java的反射机制来自动读取资源中的图片。

main.xml的源码如下:

<?xml version="1.0" encoding="utf-8"?>
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent"> 
  
  <imageswitcher android:id="@+id/switcher"
    android:layout_width="match_parent" android:layout_height="match_parent"/>
  
  <gallery android:id="@+id/gallery"
    android:background="#55000000"
    android:layout_width="match_parent"
    android:layout_alignparentbottom="true"
    android:layout_alignparentleft="true"
    
    android:gravity="center_vertical"
    android:spacing="16dp" android:layout_height="100dp"/>
</relativelayout>

java程序的源码如下:

package com.testimageview;
import java.lang.reflect.field;
import java.util.arraylist;
import android.app.activity;
import android.content.context;
import android.os.bundle;
import android.view.motionevent;
import android.view.view;
import android.view.view.ontouchlistener;
import android.view.viewgroup;
import android.view.animation.animationutils;
import android.widget.adapterview;
import android.widget.baseadapter;
import android.widget.gallery;
import android.widget.imageswitcher;
import android.widget.imageview;
import android.widget.adapterview.onitemselectedlistener;
import android.widget.gallery.layoutparams;
import android.widget.viewswitcher.viewfactory;
public class testimageview extends activity implements viewfactory {
 private imageswitcher is;
 private gallery gallery;
 private int downx,upx;
  private arraylist<integer> imglist=new arraylist<integer>();//图像id
  
 @override
 protected void oncreate(bundle savedinstancestate) {
 // todo auto-generated method stub
 super.oncreate(savedinstancestate);
 setcontentview(r.layout.main);
    //用反射机制来获取资源中的图片id
   field[] fields = r.drawable.class.getdeclaredfields();
   for (field field : fields)
   {
   if (!"icon".equals(field.getname()))//除了icon之外的图片
   { 
    int index = 0;
  try {
   index = field.getint(r.drawable.class);
  } catch (illegalargumentexception e) {
   // todo auto-generated catch block
   e.printstacktrace();
  } catch (illegalaccessexception e) {
   // todo auto-generated catch block
   e.printstacktrace();
  }
    //保存图片id
    imglist.add(index);
   }
   }
   
   //设置imageswitcher控件
 is = (imageswitcher) findviewbyid(r.id.switcher);
 is.setfactory(this);
 is.setinanimation(animationutils.loadanimation(this,
  android.r.anim.fade_in));
 is.setoutanimation(animationutils.loadanimation(this,
  android.r.anim.fade_out));
 is.setontouchlistener(new ontouchlistener(){
  /*
  * 在imageswitcher控件上滑动可以切换图片
  */
  @override
  public boolean ontouch(view v, motionevent event) {
  if(event.getaction()==motionevent.action_down)
  {
   downx=(int) event.getx();//取得按下时的坐标
   return true;
  }
  else if(event.getaction()==motionevent.action_up)
  {
   upx=(int) event.getx();//取得松开时的坐标
   int index=0;
   if(upx-downx>100)//从左拖到右,即看前一张
   {
   //如果是第一,则去到尾部
   if(gallery.getselecteditemposition()==0)
     index=gallery.getcount()-1;
   else
    index=gallery.getselecteditemposition()-1;
   }
   else if(downx-upx>100)//从右拖到左,即看后一张
   {
   //如果是最后,则去到第一
   if(gallery.getselecteditemposition()==(gallery.getcount()-1))
    index=0;
   else
    index=gallery.getselecteditemposition()+1;
   }
   //改变gallery图片所选,自动触发imageswitcher的setonitemselectedlistener
   gallery.setselection(index, true);
   return true;
  }
  return false;
  }
  
 });
 
 //设置gallery控件
 gallery = (gallery) findviewbyid(r.id.gallery);
 gallery.setadapter(new imageadapter(this));
 gallery.setonitemselectedlistener(new onitemselectedlistener(){
  @override
  public void onitemselected(adapterview<?> arg0, view arg1,
   int position, long arg3) {
  is.setimageresource(imglist.get(position));
  }
  @override
  public void onnothingselected(adapterview<?> arg0) {
  // todo auto-generated method stub
  }
  
 });
 }
 //设置imgaeswitcher
 @override
 public view makeview() {
 imageview i = new imageview(this);
 i.setbackgroundcolor(0xff000000);
 i.setscaletype(imageview.scaletype.center);//居中
 i.setlayoutparams(new imageswitcher.layoutparams(//自适应图片大小
  layoutparams.fill_parent, layoutparams.fill_parent));
 return i;
 }
 public class imageadapter extends baseadapter {
 public imageadapter(context c) {
  mcontext = c;
 }
 public int getcount() {
  return imglist.size();
 }
 public object getitem(int position) {
  return position;
 }
 public long getitemid(int position) {
  return position;
 }
 public view getview(int position, view convertview, viewgroup parent) {
  imageview i = new imageview(mcontext);
  i.setimageresource(imglist.get(position));
  i.setadjustviewbounds(true);
  i.setlayoutparams(new gallery.layoutparams(
   layoutparams.wrap_content, layoutparams.wrap_content));
  return i;
 }
 private context mcontext;
 }
}