1 package com.arc; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.view.View; 6 import android.widget.AdapterView; 7 import android.widget.AdapterView.OnItemSelectedListener; 8 import android.widget.Gallery; 9 import android.widget.ImageView; 10 import android.widget.TextView; 11 import android.widget.Toast; 12 import android.widget.AdapterView.OnItemClickListener; 13 14 public class gallery_Act extends Activity 15 { 16 private ImageView[] mImageViewIds;// 小圆点ImageView数组 17 private static final int IMAGE_COUNT = 3;// 小圆点个数 18 @Override 19 public void onCreate(Bundle savedInstanceState) 20 { 21 super.onCreate(savedInstanceState); 22 setContentView(R.layout.main); 23 mImageViewIds = new ImageView[] { 24 (ImageView) findViewById(R.id.dot_1), 25 (ImageView) findViewById(R.id.dot_2), 26 (ImageView) findViewById(R.id.dot_3)}; 27 28 29 //gallery 30 Gallery g = (Gallery) findViewById(R.id.gallery); 31 g.setAdapter(new ImageAdapter(this)); 32 g.setSelection(200); 33 g.setOnItemClickListener(new GalleryItemListener()); 34 g.setOnItemSelectedListener((OnItemSelectedListener) new GalleryItemSelectedListener()); 35 36 37 } 38 class GalleryItemListener implements OnItemClickListener { 39 public void onItemClick(AdapterView<?> parent, View view, int position, 40 long id) { 41 Toast.makeText(gallery_Act.this, "你选择了" + (position + 1) + " 号图片", 42 Toast.LENGTH_SHORT).show(); 43 44 } 45 } 46 47 class GalleryItemSelectedListener implements OnItemSelectedListener { 48 49 public void onItemSelected(AdapterView<?> parent, View view, 50 int position, long id) { 51 int pos = position % IMAGE_COUNT; 52 mImageViewIds[pos].setImageDrawable(getBaseContext().getResources() 53 .getDrawable(R.drawable.ic_dot_focused)); 54 // if (pos > 0) { 55 // mImageViewIds[pos - 1].setImageDrawable(getBaseContext() 56 // .getResources().getDrawable(R.drawable.ic_dot_normal)); 57 // } 58 // if (pos < (IMAGE_COUNT - 1)) { 59 // mImageViewIds[pos + 1].setImageDrawable(getBaseContext() 60 // .getResources().getDrawable(R.drawable.ic_dot_normal)); 61 // } 62 // if (pos == 0) { 63 // mImageViewIds[IMAGE_COUNT - 1] 64 // .setImageDrawable(getBaseContext().getResources() 65 // .getDrawable(R.drawable.ic_dot_normal)); 66 // } 67 if (pos == 1) { 68 mImageViewIds[2] 69 .setImageDrawable(getBaseContext().getResources() 70 .getDrawable(R.drawable.ic_dot_normal)); 71 mImageViewIds[0] 72 .setImageDrawable(getBaseContext().getResources() 73 .getDrawable(R.drawable.ic_dot_normal)); 74 } 75 if (pos == 2) { 76 mImageViewIds[1] 77 .setImageDrawable(getBaseContext().getResources() 78 .getDrawable(R.drawable.ic_dot_normal)); 79 mImageViewIds[0] 80 .setImageDrawable(getBaseContext().getResources() 81 .getDrawable(R.drawable.ic_dot_normal)); 82 } 83 if (pos == 0) { 84 mImageViewIds[1] 85 .setImageDrawable(getBaseContext().getResources() 86 .getDrawable(R.drawable.ic_dot_normal)); 87 mImageViewIds[2] 88 .setImageDrawable(getBaseContext().getResources() 89 .getDrawable(R.drawable.ic_dot_normal)); 90 } 91 } 92 93 public void onNothingSelected(AdapterView<?> arg0) { 94 // TODO Auto-generated method stub 95 96 } 97 } 98 99 }
package com.arc; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Gallery; import android.widget.ImageView; public class ImageAdapter extends BaseAdapter { // 定义Context private Context mContext; // 定义整型数组 即图片源, news1,news2,news3存放在 mImagineIds的数组中 private Integer[] mImageIds = { R.drawable.news1, R.drawable.news2, R.drawable.news3 }; // 声明ImageAdapter public ImageAdapter(Context c) { mContext = c; } // 获取图片的个数 public int getCount() { return Integer.MAX_VALUE; } // 获取图片在库中的位置 public Object getItem(int position) { return position; } // 获取图片ID public long getItemId(int position) { return position; } public View getView(int position, View convertView, ViewGroup parent) { // ImageView imageview = new ImageView(mContext); // // // 给ImageView设置资源 // imageview.setImageResource(mImageIds[position]); // // 设置布局 图片120*120 // imageview.setLayoutParams(new Gallery.LayoutParams(120, 120)); // // 设置显示比例类型 // imageview.setScaleType(ImageView.ScaleType.FIT_CENTER); // // return imageview; //下面的方法实现了循环播放。。。。。 /* 创建一个ImageView对象 */ ImageView i = new ImageView(this.mContext); i.setPadding(10, 10, 10, 10); i.setLayoutParams(new Gallery.LayoutParams(300, 150)); //i.setAlpha(80); // i.setImageResource(this.myImageIds[position]); if(position<0){ position =position+mImageIds.length; } i.setImageResource(this.mImageIds[position% mImageIds.length]); i.setScaleType(ImageView.ScaleType.FIT_XY); //i.setBackgroundResource(mGalleryItemBackground); /* 设置这个ImageView对象的宽高,单位为dip */ //i.setLayoutParams(new Gallery.LayoutParams(85, 72)); return i; } }
最后输出的时候三张图片(news1,news2,news3)没有显示,这是为什么?
在eclipse里面调试通过了
三个黄点那应该出现三个图片的