Chinaunix首页 | 论坛 | 博客
  • 博客访问: 497887
  • 博文数量: 59
  • 博客积分: 86
  • 博客等级: 民兵
  • 技术积分: 2141
  • 用 户 组: 普通用户
  • 注册时间: 2012-07-30 20:57
个人简介

为今天而努力的人很平凡,为昨天而努力的人叫失败,只有为美好明天而战斗不止才叫精彩!

文章分类

全部博文(59)

文章存档

2015年(1)

2014年(3)

2013年(53)

2012年(2)

我的朋友

分类: Android平台

2014-01-03 22:05:19

先上图·····

1.进入程序


2、向右滑动


3、点击图片


4、向右滑动


5、再次点击右边图片,出现循环效果



实现步骤:
一、main.layout布局文件


  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android=""
  3.     android:id="@+id/main"
  4.     android:layout_width="fill_parent"
  5.     android:layout_height="fill_parent"
  6.     android:orientation="vertical" >

  7. <Gallery
  8.     android:id="@+id/gallery"
  9.     android:layout_width="fill_parent"
  10.     android:layout_height="wrap_content" />
  11. <ImageView
  12.     android:id="@+id/image_view"
  13.     android:layout_width="wrap_content"
  14.     android:layout_height="wrap_content" />
  15. </LinearLayout>
二、启动代码里初始化Gallery,这里主要用到Gallery控件的 setImageResource()[显示内容] 和 setLayoutParams()[显示属性] 方法。最最关键的是定制适配器 ImageShowAdapter。而实现循环是通过对position求余实现,Gallery开始时会调用getCount()函数,确定有多少个View要绘制,当绘制到最后一个以后就结束了。为了实现循环我们设置getCount()的返回值为Integer的最大值,这样Gallery就可以绘制足够多的View,但是我们设定的资源图片数组大小为4,要想循环当然要用到取模运算啦。
  1. package com.adamhu.dianxiaoer;

  2. import java.lang.reflect.Field;
  3. import java.util.ArrayList;

  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. import android.widget.AdapterView;
  8. import android.widget.AdapterView.OnItemClickListener;
  9. import android.widget.BaseAdapter;
  10. import android.widget.Gallery;
  11. import android.widget.ImageView;
  12. import android.app.Activity;
  13. import android.content.Context;
  14. import android.graphics.Bitmap;
  15. import android.graphics.BitmapFactory;

  16. public class EntryActivity extends Activity {

  17.     private Gallery mGallery;
  18.     private ImageView mImgView;
  19.     private ImageShowAdapter mAdapter = new ImageShowAdapter(this);
  20.     
  21.     @Override
  22.     protected void onCreate(Bundle savedInstanceState) {
  23.         super.onCreate(savedInstanceState);
  24.         setContentView(R.layout.activity_entry);
  25.         
  26.         mGallery = (Gallery) findViewById(R.id.gallery);
  27.         mImgView = (ImageView) findViewById(R.id.image_view);
  28.         try {
  29.             mGallery.setAdapter(mAdapter);
  30.         } catch (IllegalArgumentException e) {
  31.             // TODO Auto-generated catch block
  32.             e.printStackTrace();
  33.         }
  34.         mGallery.setOnItemClickListener(new AdapterView.OnItemClickListener(){
  35.             public void onItemClick(AdapterView<?> parent, View v, int position, long id){
  36.                 Integer[] iView = mAdapter.getImagesId();
  37.                 mImgView.setImageDrawable(getResources().getDrawable(iView[position%iView.length]));
  38.             }
  39.         });
  40.         
  41.     }

  42.     public class ImageShowAdapter extends BaseAdapter{

  43.         private Context mContext;
  44.         private ArrayList<Integer> imgList = new ArrayList<Integer>();
  45.         private ArrayList<Object> imgSize = new ArrayList<Object>();
  46.         
  47.         public ImageShowAdapter(Context c){
  48.             mContext = c;
  49.             
  50.         }
  51.         
  52.         @Override
  53.         public int getCount() {
  54.             // TODO Auto-generated method stub
  55.             return Integer.MAX_VALUE;
  56.         }

  57.         @Override
  58.         public Object getItem(int position) {
  59.             // TODO Auto-generated method stub
  60.             return position;
  61.         }

  62.         @Override
  63.         public long getItemId(int position) {
  64.             // TODO Auto-generated method stub
  65.             return position;
  66.         }

  67.         @Override
  68.         public View getView(int position, View convertView, ViewGroup parent) {
  69.             // TODO Auto-generated method stub
  70.             ImageView i = new ImageView(mContext);
  71.             i.setImageResource(mImagesId[position%mImagesId.length]);
  72.             i.setLayoutParams(new Gallery.LayoutParams(270,190));
  73.             return i;
  74.         }
  75.         
  76.         public Integer[] getImagesId(){
  77.             return mImagesId;
  78.         }
  79.         
  80.         public void setImagesId(Integer[] mImagesId){
  81.             this.mImagesId = mImagesId;
  82.         }
  83.         
  84.         private Integer mImagesId[] = {
  85.                 R.drawable.image1,
  86.                 R.drawable.image10,
  87.                 R.drawable.image11,
  88.                 R.drawable.image12
  89.         };
  90.         
  91.     }

  92. }
阅读(10006) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~