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

Android使用gallery和imageSwitch制作可左右循环滑动的图片浏览器

程序员文章站 2023-08-21 22:29:04
效果图: 为了使图片浏览器左右无限循环滑动 我们要自定义gallery的adapter 如果要想自定义adapter首先要了解这几个方法 @overrid...

效果图:

Android使用gallery和imageSwitch制作可左右循环滑动的图片浏览器

为了使图片浏览器左右无限循环滑动 我们要自定义gallery的adapter

如果要想自定义adapter首先要了解这几个方法

@override 
 public int getcount() { 
  // todo auto-generated method stub 
  return 0; 
 } 

 @override 
 public object getitem(int position) { 
  // todo auto-generated method stub 
  return null; 
 } 

 @override 
 public long getitemid(int position) { 
  // todo auto-generated method stub 
  return 0; 
 } 
 @override 
 public view getview(int position, view convertview, viewgroup parent) { 
  // todo auto-generated method stub 
  return null; 
 } 

其中getcount方法 是返回数据源的数量

getitem方法 返回的是一个object对象 也就是返回目前容器中数据id position所对应的对象

getitemid 返回目前容器中的数据id

getview取得目前要显示的view

如果要实现左右循环滑动 首先我们要返回数据源的数量为最大值 然后把所有数据的id对原本数据源的数量取余  最后设置gallery初始的位置在0-最大值的中间即可

更改后的adapter就是这样

package com.example.imageswitcher; 
import android.content.context; 
import android.view.view; 
import android.view.viewgroup; 
import android.widget.baseadapter; 
import android.widget.gallery; 
import android.widget.imageview; 
import android.widget.imageview.scaletype; 

public class myadapter extends baseadapter{ 

 private int id_image[]; 
 private context contex; 
 public myadapter(context contex,int id_image[]) { 
  this.contex=contex; 
  this.id_image=id_image; 
 } 

 @override 
 public int getcount() { 
  // todo auto-generated method stub 
  return integer.max_value; 
 } 

 @override 
 public object getitem(int position) { 
  // todo auto-generated method stub 
  return id_image[position%id_image.length]; 
 } 

 @override 
 public long getitemid(int position) { 
  // todo auto-generated method stub 
  return position%id_image.length; 
 } 

 @override 
 public view getview(int position, view convertview, viewgroup parent) { 
  // todo auto-generated method stub 
  imageview imageview=new imageview(contex); 
  imageview.setbackgroundresource(id_image[position%id_image.length]); 
  imageview.setlayoutparams(new gallery.layoutparams(250, 200)); 
  imageview.setscaletype(scaletype.fit_xy); 
  return imageview; 
 } 
} 

mainactivity

package com.example.imageswitcher; 
import android.os.bundle; 
import android.app.activity; 
import android.view.layoutinflater.factory; 
import android.view.menu; 
import android.view.view; 
import android.view.window; 
import android.view.animation.animationutils; 
import android.widget.adapterview; 
import android.widget.adapterview.onitemselectedlistener; 
import android.widget.gallery; 
import android.widget.imageswitcher; 
import android.widget.imageview; 
import android.widget.imageview.scaletype; 
import android.widget.viewswitcher.viewfactory; 
public class mainactivity extends activity implements onitemselectedlistener,viewfactory{ 
 private imageswitcher imageswitcher; 
 private gallery gallery; 
 private int id_image[] = { r.drawable.beauty1, r.drawable.beauty2, 
   r.drawable.beauty3, r.drawable.beauty4, r.drawable.beauty5, 
   r.drawable.beauty6, r.drawable.beauty7, r.drawable.beauty8, 
   r.drawable.beauty9}; 
 private myadapter myadapter; 
 @override 
 protected void oncreate(bundle savedinstancestate) { 
  super.oncreate(savedinstancestate); 
  requestwindowfeature(window.feature_no_title); 
  setcontentview(r.layout.activity_main); 
  gallery = (gallery) findviewbyid(r.id.id_gallery); 
  imageswitcher = (imageswitcher) findviewbyid(r.id.id_imageswitcher); 
  myadapter=new myadapter(this, id_image); 
  imageswitcher.setfactory(this); 
  gallery.setonitemselectedlistener(this); 
  //设置淡入淡出效果 
  imageswitcher.setinanimation(animationutils.loadanimation(this, android.r.anim.fade_in)); 
  imageswitcher.setoutanimation(animationutils.loadanimation(this, android.r.anim.fade_out)); 
  gallery.setadapter(myadapter); 
  //一定不要忘记 设置gallery的初始位置为中间即可 
  gallery.setselection(id_image.length*100); 
 } 
 @override 
 public void onitemselected(adapterview<?> parent, view view, int position, 
   long id) { 
  // todo auto-generated method stub 
  imageswitcher.setbackgroundresource(id_image[position%id_image.length]); 
 } 
 @override 
 public void onnothingselected(adapterview<?> parent) { 
  // todo auto-generated method stub 
 } 
 @override 
 public view makeview() { 
  // todo auto-generated method stub 
  imageview image=new imageview(this); 
  image.setscaletype(scaletype.fit_center); 
  return image; 
 } 
} 

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!