当前位置: 代码迷 >> Android >> android开发之gallery 兑现滚动一张且短距离滑动实现滚动
  详细解决方案

android开发之gallery 兑现滚动一张且短距离滑动实现滚动

热度:75   发布时间:2016-05-01 19:34:23.0
android开发之gallery 实现滚动一张且短距离滑动实现滚动

首先gallery的特点就不用多说了吧,惯性滚动、半屏翻页,但是很多时候我们不需要它的这些特性。我今天就介绍一下 去掉惯性滚动 以及 短距离翻页的实现:?
代码先晒出来:?
main.xml:?

Xml代码??收藏代码
  1. <?xml?version="1.0"?encoding="utf-8"?>??
  2. <LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"??
  3. ????android:orientation="vertical"??
  4. ????android:layout_width="fill_parent"??
  5. ????android:layout_height="fill_parent"??
  6. ????>??
  7. <com.mh.DetialGallery??android:id="@+id/gallery"?android:layout_width="fill_parent"????
  8. ????????android:layout_height="fill_parent"??/>????
  9. </LinearLayout>??


DetialGallery 是自己继承的类。?

Java代码??收藏代码
  1. import?android.app.Activity;??
  2. import?android.content.Context;??
  3. import?android.os.Bundle;??
  4. import?android.view.View;??
  5. import?android.view.ViewGroup;??
  6. import?android.view.Window;??
  7. import?android.view.WindowManager;??
  8. import?android.widget.BaseAdapter;??
  9. import?android.widget.Gallery;??
  10. import?android.widget.ImageView;??
  11. ??
  12. public?class?main?extends?Activity?{??
  13. ????/**?Called?when?the?activity?is?first?created.?*/??
  14. ????@Override??
  15. ????public?void?onCreate(Bundle?savedInstanceState)?{??
  16. ????????super.onCreate(savedInstanceState);??
  17. ??????????
  18. ????????requestWindowFeature(Window.FEATURE_NO_TITLE);??
  19. ????????getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN?,????
  20. ?????WindowManager.LayoutParams.FLAG_FULLSCREEN);??
  21. ??
  22. ????????setContentView(R.layout.main);??
  23. ????????Gallery?gallery?=?(Gallery)?findViewById(R.id.gallery);?????
  24. ????????gallery.setAdapter(new?ImageAdapter(this));;????
  25. ??
  26. ????}??
  27. ??????
  28. ????private?int[]?resIds?=?new?int[]?????
  29. ?????????{?R.drawable.a1,?R.drawable.a2,?R.drawable.a3,?????????????
  30. ???????????R.drawable.a4,?R.drawable.a5,?R.drawable.a6,???????????
  31. ???????????R.drawable.a7,?R.drawable.a8,?R.drawable.a9??????
  32. ?????????};????
  33. ??????
  34. ??????
  35. ?????
  36. ??
  37. ?public?class?ImageAdapter?extends?BaseAdapter?????
  38. ????{?????
  39. ????????int?mGalleryItemBackground;?????
  40. ????????private?Context?mContext;?????
  41. ????????
  42. ????????public?ImageAdapter(Context?context)?????
  43. ????????{?????
  44. ????????????mContext?=?context;???????????????????????
  45. ????????}????
  46. ??
  47. ????????public?int?getCount()?????
  48. ????????{?????
  49. ????????????return?resIds.length;?????
  50. ????????}?????
  51. ????????public?Object?getItem(int?position)?????
  52. ????????{?????
  53. ????????????return?position;?????
  54. ????????}?????
  55. ????????
  56. ????????public?long?getItemId(int?position)?????
  57. ????????{?????
  58. ????????????return?position;?????
  59. ????????}?????
  60. ??????????
  61. ????????public?View?getView(int?position,?View?convertView,?ViewGroup?parent)?????
  62. ????????{?????
  63. ????????????ImageView?imageView?=?new?ImageView(mContext);?????
  64. ????????????imageView.setImageResource(resIds[position]);??
  65. ????????????imageView.setScaleType(ImageView.ScaleType.FIT_XY);?????
  66. ????????????imageView.setLayoutParams(new?Gallery.LayoutParams(480,?800));???//分辨率自己定??
  67. ????????????return?imageView;?????
  68. ????????}?????
  69. ????}???
  70. ??
  71. }??


Java代码??收藏代码
  1. import?android.content.Context;??
  2. import?android.util.AttributeSet;??
  3. import?android.view.KeyEvent;??
  4. import?android.view.MotionEvent;??
  5. import?android.widget.Gallery;??
  6. ??
  7. public?class?DetialGallery?extends?Gallery?{??
  8. ??
  9. ????public?DetialGallery(Context?context?,AttributeSet?attrSet)?{??
  10. ?????super(context,attrSet);??
  11. ?????//?TODO?Auto-generated?constructor?stub??
  12. ????}??
  13. ??
  14. ?private?boolean?isScrollingLeft(MotionEvent?e1,?MotionEvent?e2)??
  15. ???{?????
  16. ????return?e2.getX()?>?e1.getX();???
  17. ???}??
  18. ?@Override??
  19. ?public?boolean?onFling(MotionEvent?e1,?MotionEvent?e2,?float?velocityX,??
  20. ???float?velocityY)?{??
  21. ??//?TODO?Auto-generated?method?stub??
  22. //??return?super.onFling(e1,?e2,?0,?velocityY);//方法一:只去除翻页惯性??
  23. //??return?false;


  24. //方法二:只去除翻页惯性??注:没有被注释掉的代码实现了开始说的2种效果。??
  25. ??int?kEvent;????
  26. ??if(isScrollingLeft(e1,?e2)){???
  27. ???//Check?if?scrolling?left???????
  28. ???kEvent?=?KeyEvent.KEYCODE_DPAD_LEFT;????
  29. ???}??else{???
  30. ????//Otherwise?scrolling?right??????
  31. ????kEvent?=?KeyEvent.KEYCODE_DPAD_RIGHT;?????
  32. ????}????
  33. ??onKeyDown(kEvent,?null);????
  34. ??return?true;????
  35. ??}??
  36. ?}??


  相关解决方案