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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • spring dubbo集成(@autowired和构造器注入)

    一:运行spring容器的方式有三种1:使用tomcat、jetty等servlet容器运行2:自己写一个Main方法运行3:使用dubbo框架提供的Main方法运行前面两种有一定的局限性,很多缺点,具体就不一一列举,有兴趣的同学可以研究一下,今天就给大家讲如何实现第三种方式二:Main配置详讲spring路径存放路径 按照dubbo官网解说,要使得Main能正确的启

    2022年4月14日
    117
  • IIS 下利用UrlRewriter做图片防盗链

    IIS 下利用UrlRewriter做图片防盗链<?xmlversion=”1.0″encoding=”UTF-8″?><configuration><system.webServer><staticContent><clientCachecacheControlMode=”UseMaxAge”cacheC…

    2022年7月23日
    11
  • pycharm 2021.9激活码_最新在线免费激活

    (pycharm 2021.9激活码)好多小伙伴总是说激活码老是失效,太麻烦,关注/收藏全栈君太难教程,2021永久激活的方法等着你。IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.htmlS32PGH0SQB-eyJsaWNlbnNlSW…

    2022年3月26日
    47
  • intellijidea激活码2021【2021最新】

    (intellijidea激活码2021)JetBrains旗下有多款编译器工具(如:IntelliJ、WebStorm、PyCharm等)在各编程领域几乎都占据了垄断地位。建立在开源IntelliJ平台之上,过去15年以来,JetBrains一直在不断发展和完善这个平台。这个平台可以针对您的开发工作流进行微调并且能够提供…

    2022年3月21日
    67
  • 漫画:这个靠脸吃饭的男人,太可怕了!

    今天推荐画风唯美的漫画 《重生之星光璀璨》 作为歌坛巨星的阮熙冰被双胞胎妹妹 和未婚夫设计杀害,阴差阳错重生 到了一个小服务生的身上。 她靠着努力与演技重返娱乐圈, 誓要夺回曾属于…

    2021年6月22日
    115
  • 老生常谈–什么是装箱什么是拆箱「建议收藏」

    老生常谈–什么是装箱什么是拆箱「建议收藏」我们知道.NET具有两个数据类型:值类型和引用类型。因为值类型没有指针引用,不是分配在托管堆中,也不会被GC回收,因此它比引用类型更加高效。但有时我们需要将一种类型的变量转换为另一种类型,这时我们就可以使用装箱/拆箱。一、什么是装箱装箱就是将值类型的数据存储在引用类型的变量中。例如在方法中创建了int类型的变量,需要将这个值类型赋值给一个引用类型的变量,这就意味着对这个值进行了装箱操作,代码如下:voiddemo(){intnum=25;//这是装箱操作objectobjN

    2022年10月21日
    4

发表回复

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

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