`
helpbs
  • 浏览: 1162738 次
文章分类
社区版块
存档分类
最新评论

android Gallery可循环显示图片

 
阅读更多
循环显示图像的原理

循环显示有些类似于循环链表,最后一个结点的下一个结点又是第1个结点。循环显示图像也可以模拟这一点。

也许细心的读者从上一节实现的ImageAdapter类中会发现些什么。对!就是getView方法中的position参数和getCount方法的关系。position参数的值是不可能超过getCount方法返回的值的,也就是说,position参数值的范围是0getCount() - 1

如果这时Gallery组件正好显示到最后一个图像,position参数值正好为getCount() - 1。那么我们如何再让Gallery显示下一个图像呢?也就是说让position参数值再增1,对!将getCount()方法的返回值也增1

那么这里还有一个问题,如果position参数值无限地增加,就意味着resIds数组要不断地增大,这样会大大消耗系统的资源。想到这,就需要解决两个问题:既要position不断地增加,又让resIds数组中保存的图像资源ID是有限的,该怎么做呢?对于getCount()方法非常好解决,可以让getCount方法返回一个很大的数,例如,Integer.MAX_VALUE。这时position参数值就可以随着Gallery组件的图像不断向前移动而增大。现在resIds数组只有15个元素,如果position的值超过数组边界,要想继续循环取得数组中的元素(也就是说,当position的值是15时,取resIds数组的第0个元素,是16时取第1个元素),最简单的方法就是取余,代码如下:

resIds[position % resIds.length]

在本节对ImageAdapter类做了如下两个改进:

1.使getCount方法返回一个很大的值。建议返回Integer.MAX_VALUE

2.getView方法中通过取余来循环取得resIds数组中的图像资源ID

通过上面两点改进,可以使图像列表在向右移动时会循环显示图像。当然,这种方法从本质上说只是伪循环,也就是说,如果真把图像移动到getCount方法返回的值那里,那也就显示到最后一个图像的。不过在这里getCount方法返回的是Integer.MAX_VALUE,这个值超过了20亿,除非有人真想把图像移动到第20亿的位置,否则Gallery组件看着就是一个循环显示图像的组件。

实现循环显示图像的Gallery组件

在本节将组出与循环显示图像相关的ImageAdapter类的完整代码。读者可以从中看到上一节介绍的两点改进。为了使界面看上去更丰满,本例还在单击某一个Gallery组件中的图像时在下方显示一个放大的图像(使用ImageSwitcher组件)。本例的显示效果如图3所示。当不断向后移动图像时,图像可不断显示,读者可以自己运行本例来体验一下。

直接上码

<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:orientation
="vertical"android:layout_width="fill_parent"
android:layout_height
="fill_parent">
<Galleryandroid:id="@+id/gallery"android:layout_width="fill_parent"
android:layout_height
="wrap_content"android:layout_marginTop="30dp"/>
</LinearLayout>

packagenet.blogjava.mobile;

importandroid.app.Activity;
importandroid.content.Context;
importandroid.content.res.TypedArray;
importandroid.os.Bundle;
importandroid.view.View;
importandroid.view.ViewGroup;
importandroid.view.animation.AnimationUtils;
importandroid.widget.AdapterView;
importandroid.widget.BaseAdapter;
importandroid.widget.Gallery;
importandroid.widget.ImageSwitcher;
importandroid.widget.ImageView;
importandroid.widget.AdapterView.OnItemSelectedListener;
importandroid.widget.Gallery.LayoutParams;
importandroid.widget.ViewSwitcher.ViewFactory;

publicclassMainextendsActivityimplementsOnItemSelectedListener,
ViewFactory
{
privateGallerygallery;
privateImageAdapterimageAdapter;

privateint[]resIds=newint[]
{R.drawable.item1,R.drawable.item2,R.drawable.item3,R.drawable.item4,
R.drawable.item5,R.drawable.item6,R.drawable.item7,
R.drawable.item8,R.drawable.item9,R.drawable.item10,
R.drawable.item11,R.drawable.item12,R.drawable.item13,
R.drawable.item14,R.drawable.item15};

publicclassImageAdapterextendsBaseAdapter
{
intmGalleryItemBackground;
privateContextmContext;

publicImageAdapter(Contextcontext)
{
mContext
=context;
TypedArraytypedArray
=obtainStyledAttributes(R.styleable.Gallery);
mGalleryItemBackground
=typedArray.getResourceId(
R.styleable.Gallery_android_galleryItemBackground,
0);
}
//第1点改进,返回一个很大的值,例如,Integer.MAX_VALUE
publicintgetCount()
{
returnInteger.MAX_VALUE;
}

publicObjectgetItem(intposition)
{
returnposition;
}

publiclonggetItemId(intposition)
{
returnposition;
}

publicViewgetView(intposition,ViewconvertView,ViewGroupparent)
{
ImageViewimageView
=newImageView(mContext);
//第2点改进,通过取余来循环取得resIds数组中的图像资源ID
imageView.setImageResource(resIds[position%resIds.length]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(
newGallery.LayoutParams(163,106));
imageView.setBackgroundResource(mGalleryItemBackground);
returnimageView;
}
}

@Override
publicvoidonCreate(BundlesavedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gallery
=(Gallery)findViewById(R.id.gallery);
imageAdapter
=newImageAdapter(this);
gallery.setAdapter(imageAdapter);
gallery.setOnItemSelectedListener(
this);
}

总结
在本文介绍了如何实现可循环显示的Gallery组件。实际上,这个循环显示只是一个伪循环,不过由于getCount方法返回的图像总数很大(超过20亿),这就意味着已经非常接近无限循环了。实现循环显示图像的关键点有如下两个:
1. getCount方法返回一个很大的整数值(例如,Integer.MAX_VALUE)。
2. 在getView方法中通过取余的方法来循环获得图像的资源ID。

此文是转载而来的,和大家分享。

注明:QQ技术交流群:108614806 感兴趣的加一下。




分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics