Android使用gallery和imageSwitch制作可左右循环滑动的图片浏览器
作者:甄情 时间:2021-08-31 22:49:45
效果图:
为了使图片浏览器左右无限循环滑动 我们要自定义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;
}
}
来源:http://blog.csdn.net/su20145104009/article/details/50739822
标签:android,gallery,imageSwitch
0
投稿
猜你喜欢
SpringCloud Finchley+Spring Boot 2.0 集成Consul的方法示例(1.2版本)
2023-09-02 13:07:36
MyBatis入门学习教程-MyBatis快速入门
2023-10-25 05:06:07
Unity 如何获取鼠标停留位置下的物体
2023-09-05 17:17:14
WinForm开发中屏蔽WebBrowser脚本错误提示的方法
2021-06-23 08:49:40
详解C#中三个关键字params,Ref,out
2021-09-25 18:54:02
MyBatis if test 判断字符串相等不生效问题
2021-10-11 22:42:01
vc提示unexpected end of file found的原因分析
2022-01-19 12:58:00
关于Java反编译字节码文件
2021-10-07 01:41:32
SpringMVC接收多个对象的4种方法
2023-11-23 06:24:18
C#使用NPOI上传excel
2022-05-20 14:46:22
Java中将File转化为MultipartFile的操作
2021-07-05 21:25:32
C#检测上传文件真正类型的方法
2021-10-28 11:32:59
Java定义泛型方法实例分析
2023-11-20 20:28:22
SpringBoot整合TomCat实现本地图片服务器代码解析
2021-07-30 16:34:19
安卓自定义流程进度图控件实例代码
2023-10-30 00:07:23
c#使用xamarin编写拨打电话程序
2023-09-04 18:09:20
Android带清除功能的输入框控件EditTextWithDel
2022-02-13 16:53:27
response对象的使用(实例讲解)
2023-11-30 12:23:22
jar命令的用法详解
2022-08-14 13:37:28
Bean实例化之前修改BeanDefinition示例详解
2023-10-26 23:31:09