三步走:第一步初始化gallery时设置较大的初始化位置 Gallery gallery = ((Gallery) findViewById(R.id.myGallery1)); gallery.setAdapter(new ImageAdapter(this)); gallery.setSelection(200); 第二步:重写 BaseAdapter方法中的getCount时返回一个较大的值: // 为了使资源循环使用 public int getCount() { return Integer.MAX_VALUE; } 第三步:重写BaseAdapter时使用用position对集合大小取余的值,如下: /* 取得目前欲显示的图像View,传入数组ID值使之读取与成像 */ public View getView(int position, View convertView, ViewGroup parent) { /* 创建一个ImageView对象 */ ImageView i = new ImageView(this.myContext); i.setPadding(10, 10, 10, 10); i.setAlpha(80); // i.setImageResource(this.myImageIds[position]); if(position<0){ position =position+myImageIds.length; } i.setImageResource(this.myImageIds[position% myImageIds.length]); i.setScaleType(ImageView.ScaleType.FIT_XY); i.setBackgroundResource(mGalleryItemBackground); /* 设置这个ImageView对象的宽高,单位为dip */ i.setLayoutParams(new Gallery.LayoutParams(85, 72)); return i; }
以下是该类的完整代码: /* 依据距离中央的位移量 利用getScale返回views的大小(0.0f to 1.0f) */
- package irdc.ex03_15;
-
- import android.app.Activity;
- import android.content.Context;
- import android.content.res.TypedArray;
- import android.os.Bundle;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.AdapterView;
- import android.widget.BaseAdapter;
- import android.widget.Gallery;
- import android.widget.ImageView;
- import android.widget.TextView;
- import android.widget.Toast;
- import android.widget.AdapterView.OnItemSelectedListener;
-
- public class EX03_15 extends Activity
- {
- private TextView mTextView01;
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
- Gallery gallery = ((Gallery) findViewById(R.id.myGallery1));
- gallery.setAdapter(new ImageAdapter(this));
- gallery.setSelection(200);
-
-
- gallery.setOnItemSelectedListener(new OnItemSelectedListener()
- {
-
- public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
- long arg3)
- {
- Toast.makeText(EX03_15.this, "当前位置:" + arg2, Toast.LENGTH_SHORT).show();
- }
-
- public void onNothingSelected(AdapterView<?> arg0)
- {
-
- }
- });
- }
-
- public class ImageAdapter extends BaseAdapter
- {
- /* 类成员 myContext为Context父类 */
- private Context myContext;
- /*声明GalleryItemBackground*/
- int mGalleryItemBackground;
-
- /* 使用android.R.drawable里的图片作为图库来源,类型为整数数组 */
- private int[] myImageIds =
- { R.drawable.a1, R.drawable.a2, R.drawable.a3, R.drawable.a4,
- R.drawable.a5, R.drawable.a27 };
-
- /* 构造器只有一个参数,即要存储的Context */
- public ImageAdapter(Context c)
- {
- myContext = c;
-
- /*
- * 使用在res/values/attrs.xml中的<declare-styleable>定义 的Gallery属性.
- */
- TypedArray a = obtainStyledAttributes(R.styleable.Gallery);
-
- /* 取得Gallery属性的Index id */
- mGalleryItemBackground = a.getResourceId(
- R.styleable.Gallery_android_galleryItemBackground, 0);
-
- /* 让对象的styleable属性能够反复使用 */
- a.recycle();
- }
-
- /* 返回所有已定义的图片总数量 */
- // public int getCount() { return this.myImageIds.length; }
- // 为了使资源循环使用
- public int getCount()
- {
- return Integer.MAX_VALUE;
- }
-
- /* 利用getItem方法,取得目前容器中图像的数组ID */
- public Object getItem(int position)
- {
- return position;
- }
-
- public long getItemId(int position)
- {
- return position;
- }
-
- /* 取得目前欲显示的图像View,传入数组ID值使之读取与成像 */
- public View getView(int position, View convertView, ViewGroup parent)
- {
- /* 创建一个ImageView对象 */
- ImageView i = new ImageView(this.myContext);
- i.setPadding(10, 10, 10, 10);
- i.setAlpha(80);
- // i.setImageResource(this.myImageIds[position]);
- if(position<0){
- position =position+myImageIds.length;
- }
- i.setImageResource(this.myImageIds[position% myImageIds.length]);
- i.setScaleType(ImageView.ScaleType.FIT_XY);
- i.setBackgroundResource(mGalleryItemBackground);
- /* 设置这个ImageView对象的宽高,单位为dip */
- i.setLayoutParams(new Gallery.LayoutParams(85, 72));
- return i;
- }
-
- /* 依据距离中央的位移量 利用getScale返回views的大小(0.0f to 1.0f) */
- public float getScale(boolean focused, int offset)
- {
- return Math.max(0, 1.0f / (float) Math.pow(2, Math.abs(offset)));
- }
- }
- }
复制代码
|
相关推荐
**Android Gellary3D 源码解析*...通过深入研究Android Gellary3D的源代码,开发者可以学习到如何在Android平台上实现高效的3D图形渲染、数据加载优化、UI设计以及多线程编程,这对于提升Android应用开发技能大有裨益。
标题中的"Android应用源码模拟立体翻转效果,非Gallery实现.zip" 提供了一个源码示例,它展示了如何在不使用内置的Gallery组件的情况下,实现一个立体翻转效果。Gallery组件在较旧版本的Android API中被用于展示图像...
本文将详细探讨如何实现一个"Android自定义带倒影和偏转的超炫Gallery",该Gallery组件能展示出极具视觉冲击力的效果。 首先,让我们了解一下Android中的Gallery组件。Gallery是Android提供的一个特殊的视图,它...
项目中的"ZKtest48_CirculateGallery"很可能包含了实现这个功能的所有源代码和资源文件。开发者可以深入研究这些代码,理解如何创建自定义的Gallery,以及如何实现无限循环和中心放大效果。这有助于提升Android UI...
在原生Android SDK中,并没有内置的CoverFlow控件,因此需要通过自定义ViewGroup,如Gallery或HorizontalScrollView来实现。这个改进版的控件可能是在此基础上进行了优化,解决了原始实现中可能存在的性能问题,并...
7. **界面设计**:Gallery3D的UI设计简洁明了,源码中包含了XML布局文件和自定义控件,展示了如何在Android中实现3D视图和动画效果。 总的来说,通过分析Android 2.3的3D系统图库源码,我们可以学习到如何利用...
【3D Gallery无限自动循环+倒影效果】是一款专为Android平台设计的图像展示应用,它结合了3D视觉效果和动态循环播放功能,同时增加了图片的倒影特效,为用户带来独特的视觉体验。这款应用在Android开发领域具有较高...
文件"mainbeijing.png"可能是3D场景中的一个纹理资源,而"android_effects-master"可能是一个包含3D效果实现的Android项目源码。通过分析这些文件,开发者可以学习到具体的实现细节,如纹理加载、3D模型构建以及动画...