android自动画廊,Android:使画廊无限循环的图像

android自动画廊,Android:使画廊无限循环的图像我在我的项目中使用了一个画廊,其中我添加了四个图像,我希望它从右侧和左侧都是无限的.我该如何做到这一点?解决方法:主要思想是在你的getView方法中,你必须使用position=position%imagesArray.length;if(position<0)position=position+imagesArray.length;imagesArray是将图像保存…

大家好,又见面了,我是你们的朋友全栈君。

我在我的项目中使用了一个画廊,其中我添加了四个图像,我希望它从右侧和左侧都是无限的.我该如何做到这一点?

解决方法:

主要思想是在你的getView方法中,你必须使用

position = position % imagesArray.length;

if (position < 0)

position = position + imagesArray.length;

imagesArray是将图像保存在res文件夹中的数组.例如:

public class CircularGallery extends Activity {

/** Called when the activity is first created. */

private Integer[] imagesArray = { R.drawable.picture1, R.drawable.picture2, R.drawable.picture3, R.drawable.picture4, R.drawable.picture5, R.drawable.picture6 , R.drawable.picture7 };

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

Gallery g = (Gallery) findViewById(R.id.gallery);

g.setAdapter(new ImageAdapter(this));

g.setOnItemClickListener(new OnItemClickListener() {

public void onItemClick(AdapterView parent, View v, int position, long id) {

if (position >= imagesArray.length) {

position = position % imagesArray.length;

}

Toast.makeText(CircularGallery.this, “” + position, Toast.LENGTH_SHORT).show();

}

});

}

public class ImageAdapter extends BaseAdapter {

int mGalleryItemBackground;

private Context mContext;

public ImageAdapter(Context c) {

mContext = c;

TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);

mGalleryItemBackground = a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);

a.recycle();

}

public int getCount() {

return Integer.MAX_VALUE;

}

public Object getItem(int position) {

if (position >= imagesArraylength) {

position = position % mImageIds.length;

}

return position;

}

public long getItemId(int position) {

if (position >= imagesArray.length) {

position = position % imagesArray.length;

}

return position;

}

public View getView(int position, View convertView, ViewGroup parent) {

ImageView i = new ImageView(mContext);

if (position >= imagesArray.length) {

position = position % imagesArray.length;

}

i.setImageResource(imagesArray[position]);

i.setLayoutParams(new Gallery.LayoutParams(80, 80));

i.setScaleType(ImageView.ScaleType.FIT_XY);

i.setBackgroundResource(mGalleryItemBackground);

return i;

}

public int checkPosition(int position) {

if (position >= imagesArray.length) {

position = position % imagesArray.length;

}

return position;

}

}}

此外,一些开发人员已经完成了这样的功能,您可以在他们的博客上找到来源

标签:android-gallery,android

来源: https://codeday.me/bug/20191007/1867746.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/130233.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 电商接口测试用例_连连电商跨境

    电商接口测试用例_连连电商跨境按照两种模式进行划分总结:1.按照测试类型2.按照电子商务网站的系统架构1.按照测试类型来划分1.兼容性1.1主要是在浏览器兼容(360浏览器IE6IE8浏览器)12.操作系统,主要体现在操作系统兼容(xpwin2003win2007)2.UI测试2.1检查连接是否正确2.2是否有文字错误信息2.2产品价格是否有显示错误。3.用户体验测试UE3.1首页产品的展示与分类3.2搜索结果页,搜索…

    2022年9月28日
    3
  • Nginx报错:Sorry, the page you are looking for is currently unavailable. Please try again later.

    Nginx报错:Sorry, the page you are looking for is currently unavailable. Please try again later.

    2021年10月12日
    126
  • 智慧职教云Java题库_云课堂智慧职教java职业证书题库答案

    智慧职教云Java题库_云课堂智慧职教java职业证书题库答案云课堂智慧职教java职业证书题库答案更多相关问题老子说“我有一颗愚人之心”,“若婴儿未孩”。下面哪一个选项最近此义:“大多数人都认为X是真的,所以X是真的。”属于()的论证方式。“大学语文”课程的前身是“大一国文”课程。()权益法下核算的长期股权投资,会导致投资企业投资收益发生增减变动的是( )“大众创业,万众创新”号召是在哪一年提出的?“大弦嘈嘈如急雨,小弦切切如私语。嘈嘈切切错杂弹,大珠小…

    2022年6月12日
    22
  • centos nginx安装配置_centos7服务器

    centos nginx安装配置_centos7服务器安装编译工具及库文件关闭防火墙确认防火墙状态关闭selinux确认selinux状态创建目录文件夹确认目录情况配置nginx的yum源安装nginx安装成功,查看版本参考:http

    2022年8月16日
    6
  • c语言入门教程–-8循环控制语句

    c语言入门教程–-8循环控制语句

    2021年3月12日
    226
  • springsecurity官网_log4j.properties配置

    springsecurity官网_log4j.properties配置由于项目框架古老spring3.0springsecurity2.0.4,但功能齐全,所以个人想要升级各个jar包版本,以减少不必要的已知bug目标更换为springsecurity4.2springdata换为最新问题1:spring版本不匹配,jar包冲突这个是最好解决的,先把springsecurity做最基础配置,换几个理论上兼容的spring版本试试就行,最终选定…

    2022年5月3日
    56

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号