A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© 老衲玩IT 中级黑马   /  2013-8-27 14:36  /  1078 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. /*Gallary对象加载缩略图集,并显示
  2. *为Gallary对象添加选项监听事件,当选中
  3. *一项时,对应的回调函数Switcher对象的图片设置
  4. *为图片资源中一张下标与Gallary选项下标相同的图片
  5. */
  6. public class ImageSwitcher1 extends Activity implements
  7.         AdapterView.OnItemSelectedListener, ViewSwitcher.ViewFactory {//实现viewFactory
  8. //图片切换器的实例引用
  9.     private ImageSwitcher mSwitcher;

  10.     @Override
  11.     public void onCreate(Bundle savedInstanceState) {
  12.         super.onCreate(savedInstanceState);
  13. //设置屏幕无标题
  14.         requestWindowFeature(Window.FEATURE_NO_TITLE);

  15.         setContentView(R.layout.image_switcher_1);
  16. //找到layout中的控件
  17.         mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
  18.         mSwitcher.setFactory(this);
  19. //设置图片切换动画
  20.         mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
  21.                 android.R.anim.fade_in));
  22.         mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
  23.                 android.R.anim.fade_out));
  24. //相册控件
  25.         Gallery g = (Gallery) findViewById(R.id.gallery);
  26.         g.setAdapter(new ImageAdapter(this));
  27.         g.setOnItemSelectedListener(this);
  28.     }

  29.     public void onItemSelected(AdapterView parent, View v, int position, long id) {
  30. //设置一张下标与item选项position相同图片
  31.         mSwitcher.setImageResource(mImageIds[position]);
  32.     }

  33.     public void onNothingSelected(AdapterView parent) {
  34.     }

  35.     public View makeView() {
  36.         ImageView i = new ImageView(this);
  37.         i.setBackgroundColor(0xFF000000);
  38.         i.setScaleType(ImageView.ScaleType.FIT_CENTER);
  39.         i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.MATCH_PARENT,
  40.                 LayoutParams.MATCH_PARENT));
  41.         return i;
  42.     }

  43. //自定义图片适配器
  44.     public class ImageAdapter extends BaseAdapter {
  45.         public ImageAdapter(Context c) {
  46.             mContext = c;
  47.         }

  48.         public int getCount() {
  49.             return mThumbIds.length;
  50.         }

  51.         public Object getItem(int position) {
  52.             return position;
  53.         }

  54.         public long getItemId(int position) {
  55.             return position;
  56.         }

  57.         public View getView(int position, View convertView, ViewGroup parent) {
  58.             ImageView i = new ImageView(mContext);

  59.             i.setImageResource(mThumbIds[position]);
  60.             i.setAdjustViewBounds(true);
  61.             i.setLayoutParams(new Gallery.LayoutParams(
  62.                     LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
  63.             i.setBackgroundResource(R.drawable.picture_frame);
  64.             return i;
  65.         }

  66.         private Context mContext;

  67.     }

  68.     private Integer[] mThumbIds = {
  69.             R.drawable.sample_thumb_0, R.drawable.sample_thumb_1,
  70.             R.drawable.sample_thumb_2, R.drawable.sample_thumb_3,
  71.             R.drawable.sample_thumb_4, R.drawable.sample_thumb_5,
  72.             R.drawable.sample_thumb_6, R.drawable.sample_thumb_7};

  73.     private Integer[] mImageIds = {
  74.             R.drawable.sample_0, R.drawable.sample_1, R.drawable.sample_2,
  75.             R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5,
  76.             R.drawable.sample_6, R.drawable.sample_7};

  77. }
复制代码

评分

参与人数 1黑马币 +3 收起 理由
EYE_SEE_YOU + 3 很好

查看全部评分

1 个回复

倒序浏览
您需要登录后才可以回帖 登录 | 加入黑马