Android入门第十三篇之Gallery + ImageSwitcher

本文来自http://blog.csdn.net/hellogv/,引用必须注明出处!

上次讲了如何使用Gallery控件,这次就讲Gallery 与ImageSwitcher的结合使用,本文实现一个简单的浏览图片的功能。先贴出程序运行截图:

除了Gallery可以拖拉切换图片,我在ImageSwitcher控件加入了setOnTouchListener事件实现,使得ImageSwitcher也可以在拖拉中切换图片。本例子依然使用JAVA的反射机制来自动读取资源中的图片。

main.xml的源码如下:

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent">
  5. <ImageSwitcherandroid:id="@+id/switcher"
  6. android:layout_width="match_parent"android:layout_height="match_parent"/>
  7. <Galleryandroid:id="@+id/gallery"
  8. android:background="#55000000"
  9. android:layout_width="match_parent"
  10. android:layout_alignParentBottom="true"
  11. android:layout_alignParentLeft="true"
  12. android:gravity="center_vertical"
  13. android:spacing="16dp"android:layout_height="100dp"/>
  14. </RelativeLayout>

程序的源码如下:

  1. packagecom.testImageView;
  2. importjava.lang.reflect.Field;
  3. importjava.util.ArrayList;
  4. importandroid.app.Activity;
  5. importandroid.content.Context;
  6. importandroid.os.Bundle;
  7. importandroid.view.MotionEvent;
  8. importandroid.view.View;
  9. importandroid.view.View.OnTouchListener;
  10. importandroid.view.ViewGroup;
  11. importandroid.view.animation.AnimationUtils;
  12. importandroid.widget.AdapterView;
  13. importandroid.widget.BaseAdapter;
  14. importandroid.widget.Gallery;
  15. importandroid.widget.ImageSwitcher;
  16. importandroid.widget.ImageView;
  17. importandroid.widget.AdapterView.OnItemSelectedListener;
  18. importandroid.widget.Gallery.LayoutParams;
  19. importandroid.widget.ViewSwitcher.ViewFactory;
  20. publicclasstestImageViewextendsActivityimplementsViewFactory{
  21. privateImageSwitcheris;
  22. privateGallerygallery;
  23. privateintdownX,upX;
  24. privateArrayList<Integer>imgList=newArrayList<Integer>();//图像ID
  25. @Override
  26. protectedvoidonCreate(BundlesavedInstanceState){
  27. //TODOAuto-generatedmethodstub
  28. super.onCreate(savedInstanceState);
  29. setContentView(R.layout.main);
  30. //用反射机制来获取资源中的图片ID
  31. Field[]fields=R.drawable.class.getDeclaredFields();
  32. for(Fieldfield:fields)
  33. {
  34. if(!"icon".equals(field.getName()))//除了icon之外的图片
  35. {
  36. intindex=0;
  37. try{
  38. index=field.getInt(R.drawable.class);
  39. }catch(IllegalArgumentExceptione){
  40. //TODOAuto-generatedcatchblock
  41. e.printStackTrace();
  42. }catch(IllegalAccessExceptione){
  43. //TODOAuto-generatedcatchblock
  44. e.printStackTrace();
  45. }
  46. //保存图片ID
  47. imgList.add(index);
  48. }
  49. }
  50. //设置ImageSwitcher控件
  51. is=(ImageSwitcher)findViewById(R.id.switcher);
  52. is.setFactory(this);
  53. is.setInAnimation(AnimationUtils.loadAnimation(this,
  54. android.R.anim.fade_in));
  55. is.setOutAnimation(AnimationUtils.loadAnimation(this,
  56. android.R.anim.fade_out));
  57. is.setOnTouchListener(newOnTouchListener(){
  58. /*
  59. *在ImageSwitcher控件上滑动可以切换图片
  60. */
  61. @Override
  62. publicbooleanonTouch(Viewv,MotionEventevent){
  63. if(event.getAction()==MotionEvent.ACTION_DOWN)
  64. {
  65. downX=(int)event.getX();//取得按下时的坐标
  66. returntrue;
  67. }
  68. elseif(event.getAction()==MotionEvent.ACTION_UP)
  69. {
  70. upX=(int)event.getX();//取得松开时的坐标
  71. intindex=0;
  72. if(upX-downX>100)//从左拖到右,即看前一张
  73. {
  74. //如果是第一,则去到尾部
  75. if(gallery.getSelectedItemPosition()==0)
  76. index=gallery.getCount()-1;
  77. else
  78. index=gallery.getSelectedItemPosition()-1;
  79. }
  80. elseif(downX-upX>100)//从右拖到左,即看后一张
  81. {
  82. //如果是最后,则去到第一
  83. if(gallery.getSelectedItemPosition()==(gallery.getCount()-1))
  84. index=0;
  85. else
  86. index=gallery.getSelectedItemPosition()+1;
  87. }
  88. //改变gallery图片所选,自动触发ImageSwitcher的setOnItemSelectedListener
  89. gallery.setSelection(index,true);
  90. returntrue;
  91. }
  92. returnfalse;
  93. }
  94. });
  95. //设置gallery控件
  96. gallery=(Gallery)findViewById(R.id.gallery);
  97. gallery.setAdapter(newImageAdapter(this));
  98. gallery.setOnItemSelectedListener(newOnItemSelectedListener(){
  99. @Override
  100. publicvoidonItemSelected(AdapterView<?>arg0,Viewarg1,
  101. intposition,longarg3){
  102. is.setImageResource(imgList.get(position));
  103. }
  104. @Override
  105. publicvoidonNothingSelected(AdapterView<?>arg0){
  106. //TODOAuto-generatedmethodstub
  107. }
  108. });
  109. }
  110. //设置ImgaeSwitcher
  111. @Override
  112. publicViewmakeView(){
  113. ImageViewi=newImageView(this);
  114. i.setBackgroundColor(0xFF000000);
  115. i.setScaleType(ImageView.ScaleType.CENTER);//居中
  116. i.setLayoutParams(newImageSwitcher.LayoutParams(//自适应图片大小
  117. LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
  118. returni;
  119. }
  120. publicclassImageAdapterextendsBaseAdapter{
  121. publicImageAdapter(Contextc){
  122. mContext=c;
  123. }
  124. publicintgetCount(){
  125. returnimgList.size();
  126. }
  127. publicObjectgetItem(intposition){
  128. returnposition;
  129. }
  130. publiclonggetItemId(intposition){
  131. returnposition;
  132. }
  133. publicViewgetView(intposition,ViewconvertView,ViewGroupparent){
  134. ImageViewi=newImageView(mContext);
  135. i.setImageResource(imgList.get(position));
  136. i.setAdjustViewBounds(true);
  137. i.setLayoutParams(newGallery.LayoutParams(
  138. LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
  139. returni;
  140. }
  141. privateContextmContext;
  142. }
  143. }

你可能感兴趣的:(ImageSwitcher)