Welcome 微信登录
编程资源 图片资源库 蚂蚁家优选 PDF转换器

首页 / 操作系统 / Linux / Android FlingGallery类完美手势拖动实现

网上找了很多关于FlingGallery类拖动的例子,还是很不错的,但是感觉不是很完美,拖动的只能是界面一致的布局,很不灵活,我觉得如果能够拖动不同的布局页面,就是拖动一下换一个页面布局,那就很完美了,于是我就在原有的基础上修改了一点点代码,最终被我给解决了这个问题。下面我就给大家看看我修改后的代码:先贴上几张图        

FlingGalleryActivity.java 实现类:
  1. package com.droidful.flinggallery;  
  2.   
  3. import Android.app.Activity;  
  4. import android.content.Context;  
  5. import android.os.Bundle;  
  6. import android.util.Log;  
  7. import android.view.MotionEvent;  
  8. import android.view.View;  
  9. import android.view.View.OnClickListener;  
  10. import android.view.ViewGroup;  
  11. import android.widget.ArrayAdapter;  
  12. import android.widget.CheckBox;  
  13. import android.widget.LinearLayout;  
  14. import android.widget.TableLayout;  
  15. import android.widget.TextView;  
  16. import android.widget.Toast;  
  17.   
  18. public class FlingGalleryActivity extends Activity  
  19. {  
  20.     private final String[] mLabelArray = {"View1""View2""View3""View4"};  
  21.   
  22.     private FlingGallery mGallery;  
  23.     private CheckBox mCheckBox;  
  24.     private TextView t1;  
  25.     // Note: The following handler is critical to correct function of   
  26.     // the FlingGallery class. This enables the FlingGallery class to   
  27.     // detect when the motion event has ended by finger being lifted   
  28.   
  29.     @Override  
  30.     public boolean onTouchEvent(MotionEvent event)  
  31.     {  
  32.         return mGallery.onGalleryTouchEvent(event);  
  33.     }  
  34.   
  35.     public void onCreate(Bundle savedInstanceState)  
  36.     {  
  37.         super.onCreate(savedInstanceState);  
  38.   
  39.         mGallery = new FlingGallery(this);  
  40.         mGallery.setPaddingWidth(5);  
  41.         mGallery.setAdapter(new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, mLabelArray)  
  42.         {  
  43.             @Override  
  44.             public View getView(int position, View convertView, ViewGroup parent)  
  45.             {  
  46.                 Log.d("msg""count="+position);  
  47. //              if (convertView != null && convertView instanceof GalleryViewItem)   
  48. //                {   
  49. //                  GalleryViewItem galleryView = (GalleryViewItem) convertView;   
  50. //   
  51. //                  galleryView.mEdit1.setText("");   
  52. //                  galleryView.mText1.setText(mLabelArray[position]);   
  53. //                  galleryView.mText1.setBackgroundColor(mColorArray[position]);   
  54. //                  galleryView.mText2.setText(mLabelArray[position]);   
  55. //                  galleryView.mText2.setBackgroundColor(mColorArray[position]);   
  56. //                  Log.d("msg", "count="+position);           
  57. //                  return galleryView;   
  58. //                }   
  59.                 return new GalleryViewItem(getApplicationContext(), position);  
  60.             }  
  61.         });  
  62.   
  63.         LinearLayout layout = new LinearLayout(getApplicationContext());  
  64.         layout.setOrientation(LinearLayout.VERTICAL);  
  65.   
  66.         LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(  
  67.                 LinearLayout.LayoutParams.MATCH_PARENT,  
  68.                 LinearLayout.LayoutParams.MATCH_PARENT);  
  69.   
  70.         //layoutParams.setMargins(10, 10, 10, 10);   
  71.         layoutParams.weight = 1.0f;  
  72.     
  73.         layout.addView(mGallery, layoutParams);  
  74.           
  75.         mCheckBox = new CheckBox(getApplicationContext());  
  76.         mCheckBox.setText("Gallery is Circular");  
  77.         mCheckBox.setPadding(5010010);  
  78.         mCheckBox.setTextSize(30);  
  79.         mCheckBox.setChecked(true);  
  80.         mCheckBox.setOnClickListener(new OnClickListener()  
  81.         {  
  82.             @Override  
  83.             public void onClick(View view)  
  84.             {  
  85.                 mGallery.setIsGalleryCircular(mCheckBox.isChecked());  
  86.             }  
  87.         });  
  88.   
  89.         layout.addView(mCheckBox, new LinearLayout.LayoutParams(  
  90.                 LinearLayout.LayoutParams.MATCH_PARENT,  
  91.                 LinearLayout.LayoutParams.WRAP_CONTENT));  
  92.           
  93.         setContentView(layout);  
  94.           
  95.           
  96.     }     
  97.     private class GalleryViewItem extends TableLayout  
  98.     {  
  99.         public GalleryViewItem(Context context, int position)  
  100.         {  
  101.             super(context);  
  102.   
  103.             this.setOrientation(LinearLayout.VERTICAL);  
  104.             this.setLayoutParams(new LinearLayout.LayoutParams(  
  105.                     LinearLayout.LayoutParams.WRAP_CONTENT,  
  106.                     LinearLayout.LayoutParams.WRAP_CONTENT));  
  107.               
  108.             if(position==0){  
  109.                 View view = (View)getLayoutInflater().inflate(R.layout.main01,null);  
  110.                 //给里面控件设置事件监听   
  111.                 t1 = (TextView)view.findViewById(R.id.main_view01);  
  112.                 t1.setOnClickListener(new OnClickListener() {  
  113.                       
  114.                     @Override  
  115.                     public void onClick(View v) {  
  116.                         Toast.makeText(getApplicationContext(), t1.getText().toString(), Toast.LENGTH_SHORT).show();  
  117.                     }  
  118.                 });  
  119.                 this.addView(view,new LinearLayout.LayoutParams(  
  120.                         LinearLayout.LayoutParams.FILL_PARENT,  
  121.                         LinearLayout.LayoutParams.FILL_PARENT));  
  122.             }else if(position==1){  
  123.                 View view = (View)getLayoutInflater().inflate(R.layout.main02,null);  
  124.                 this.addView(view,new LinearLayout.LayoutParams(  
  125.                         LinearLayout.LayoutParams.FILL_PARENT,  
  126.                         LinearLayout.LayoutParams.FILL_PARENT));  
  127.             }else if(position==2){  
  128.                 View view = (View)getLayoutInflater().inflate(R.layout.main03,null);  
  129.                 this.addView(view,new LinearLayout.LayoutParams(  
  130.                         LinearLayout.LayoutParams.FILL_PARENT,  
  131.                         LinearLayout.LayoutParams.FILL_PARENT));  
  132.             }else if(position==3){  
  133.                 View view = (View)getLayoutInflater().inflate(R.layout.main04,null);  
  134.                 this.addView(view,new LinearLayout.LayoutParams(  
  135.                         LinearLayout.LayoutParams.FILL_PARENT,  
  136.                         LinearLayout.LayoutParams.FILL_PARENT));  
  137.             }  
  138.         }  
  139.     }  
  140. }  
FlingGallery.java 工具类
  1. package com.droidful.flinggallery;  
  2.   
  3. import android.content.Context;  
  4. import android.view.GestureDetector;  
  5. import android.view.KeyEvent;  
  6. import android.view.MotionEvent;  
  7. import android.view.View;  
  8. import android.view.animation.Animation;  
  9. import android.view.animation.AnimationUtils;  
  10. import android.view.animation.Interpolator;  
  11. import android.view.animation.Transformation;  
  12. import android.widget.Adapter;  
  13. import android.widget.FrameLayout;  
  14. import android.widget.LinearLayout;  
  15.   
  16. // TODO:   
  17.   
  18. // 1. In order to improve performance Cache screen bitmap and use for animation   
  19. // 2. Establish superfluous memory allocations and delay or replace with reused objects   
  20. //    Probably need to make sure we are not allocating objects (strings, etc.) in loops   
  21.   
  22. //为了提高性能,使用缓存屏幕图动画   
  23. //建立多余的内存分配和延迟或替换为重用对象可能需要确保我们不分配对象(字符串,等等。)在循环   
  24. public class FlingGallery extends FrameLayout  
  25. {  
  26.     // Constants 常量   
  27.       
  28.     private final int swipe_min_distance = 120;  
  29.     private final int swipe_max_off_path = 250;  
  30.     private final int swipe_threshold_veloicty = 400;  
  31.   
  32.     // Properties 属性   
  33.       
  34.     private int mViewPaddingWidth = 0;  
  35.     private int mAnimationDuration = 250;  
  36.     private float mSnapBorderRatio = 0.5f;  
  37.     private boolean mIsGalleryCircular = true;  
  38.   
  39.     // Members 成员   
  40.   
  41.     private int mGalleryWidth = 0;  
  42.     private boolean mIsTouched = false;  
  43.     private boolean mIsDragging = false;  
  44.     private float mCurrentOffset = 0.0f;  
  45.     private long mScrollTimestamp = 0;  
  46.     private int mFlingDirection = 0;  
  47.     private int mCurrentPosition = 0;  
  48.     private int mCurrentViewNumber = 0;  
  49.   
  50.     private Context mContext;  
  51.     private Adapter mAdapter;  
  52.     private FlingGalleryView[] mViews;  
  53.     private FlingGalleryAnimation mAnimation;  
  54.     private GestureDetector mGestureDetector;  
  55.     private Interpolator mDecelerateInterpolater;  
  56.   
  57.     public FlingGallery(Context context)  
  58.     {  
  59.         super(context);  
  60.   
  61.         mContext = context;  
  62.         mAdapter = null;  
  63.           
  64.         mViews = new FlingGalleryView[3];  
  65.         mViews[0] = new FlingGalleryView(0this);  
  66.         mViews[1] = new FlingGalleryView(1this);  
  67.         mViews[2] = new FlingGalleryView(2this);  
  68.   
  69.         mAnimation = new FlingGalleryAnimation();  
  70.         mGestureDetector = new GestureDetector(new FlingGestureDetector());  
  71.         mDecelerateInterpolater = AnimationUtils.loadInterpolator(mContext, android.R.anim.decelerate_interpolator);  
  72.     }  
  73.   
  74.     public void setPaddingWidth(int viewPaddingWidth)  
  75.     {  
  76.         mViewPaddingWidth = viewPaddingWidth;  
  77.     }  
  78.   
  79.     public void setAnimationDuration(int animationDuration)  
  80.     {  
  81.         mAnimationDuration = animationDuration;  
  82.     }  
  83.       
  84.     public void setSnapBorderRatio(float snapBorderRatio)  
  85.     {  
  86.         mSnapBorderRatio = snapBorderRatio;  
  87.     }  
  88.   
  89.     public void setIsGalleryCircular(boolean isGalleryCircular)   
  90.     {  
  91.         if (mIsGalleryCircular != isGalleryCircular)  
  92.         {  
  93.             mIsGalleryCircular = isGalleryCircular;  
  94.       
  95.             if (mCurrentPosition == getFirstPosition())  
  96.             {  
  97.                 // We need to reload the view immediately to the left to change it to circular view or blank   
  98.                 //我们需要重新加载视图立即到左边来改变它圆视图或空白   
  99.                 mViews[getPrevViewNumber(mCurrentViewNumber)].recycleView(getPrevPosition(mCurrentPosition));             
  100.             }  
  101.       
  102.             if (mCurrentPosition == getLastPosition())  
  103.             {  
  104.                 // We need to reload the view immediately to the right to change it to circular view or blank   
  105.                 //我们需要重新加载视图立即向右改变它圆视图或空白   
  106.                 mViews[getNextViewNumber(mCurrentViewNumber)].recycleView(getNextPosition(mCurrentPosition));             
  107.             }  
  108.         }  
  109.     }  
  110.   
  111.     public int getGalleryCount()  
  112.     {  
  113.         return (mAdapter == null) ? 0 : mAdapter.getCount();  
  114.     }  
  115.   
  116.     public int getFirstPosition()  
  117.     {  
  118.         return 0;  
  119.     }  
  120.   
  121.     public int getLastPosition()  
  122.     {  
  123.         return (getGalleryCount() == 0) ? 0 : getGalleryCount() - 1;  
  124.     }  
  125.   
  126.     private int getPrevPosition(int relativePosition)  
  127.     {  
  128.         int prevPosition = relativePosition - 1;  
  129.   
  130.         if (prevPosition < getFirstPosition())  
  131.         {  
  132.             prevPosition = getFirstPosition() - 1;  
  133.   
  134.             if (mIsGalleryCircular == true)  
  135.             {  
  136.                 prevPosition = getLastPosition();  
  137.             }  
  138.         }  
  139.   
  140.         return prevPosition;  
  141.     }  
  142.   
  143.     private int getNextPosition(int relativePosition)  
  144.     {  
  145.         int nextPosition = relativePosition + 1;  
  146.   
  147.         if (nextPosition > getLastPosition())  
  148.         {  
  149.             nextPosition = getLastPosition() + 1;  
  150.   
  151.             if (mIsGalleryCircular == true)  
  152.             {  
  153.                 nextPosition = getFirstPosition();  
  154.             }  
  155.         }  
  156.   
  157.         return nextPosition;  
  158.     }  
  159.   
  160.     private int getPrevViewNumber(int relativeViewNumber)  
  161.     {  
  162.         return (relativeViewNumber == 0) ? 2 : relativeViewNumber - 1;  
  163.     }  
  164.   
  165.     private int getNextViewNumber(int relativeViewNumber)  
  166.     {  
  167.         return (relativeViewNumber == 2) ? 0 : relativeViewNumber + 1;  
  168.     }  
  169.       
  170.     @Override  
  171.     protected void onLayout(boolean changed, int left, int top, int right, int bottom)  
  172.     {  
  173.         super.onLayout(changed, left, top, right, bottom);  
  174.   
  175.         // Calculate our view width 计算我们的视图的宽度   
  176.         mGalleryWidth = right - left;  
  177.   
  178.         if (changed == true)  
  179.         {  
  180.             // Position views at correct starting offsets 在正确的开始偏移位置的观点   
  181.             mViews[0].setOffset(00, mCurrentViewNumber);  
  182.             mViews[1].setOffset(00, mCurrentViewNumber);  
  183.             mViews[2].setOffset(00, mCurrentViewNumber);  
  184.         }  
  185.     }  
  186.   
  187.     public void setAdapter(Adapter adapter)  
  188.     {  
  189.         mAdapter = adapter;  
  190.         mCurrentPosition = 0;  
  191.         mCurrentViewNumber = 0;  
  192.   
  193.         // Load the initial views from adapter 加载初始视图从适配器   
  194.         mViews[0].recycleView(mCurrentPosition);  
  195.         mViews[1].recycleView(getNextPosition(mCurrentPosition));  
  196.         mViews[2].recycleView(getPrevPosition(mCurrentPosition));  
  197.   
  198.         // Position views at correct starting offsets  在正确的开始偏移位置的观点   
  199.         mViews[0].setOffset(00, mCurrentViewNumber);  
  200.         mViews[1].setOffset(00, mCurrentViewNumber);  
  201.         mViews[2].setOffset(00, mCurrentViewNumber);  
  202.     }  
  203.   
  204.     private int getViewOffset(int viewNumber, int relativeViewNumber)  
  205.     {  
  206.         // Determine width including configured padding width 确定宽度包括配置填充宽度   
  207.         int offsetWidth = mGalleryWidth + mViewPaddingWidth;  
  208.   
  209.         // Position the previous view one measured width to left 位置之前的观点一个测量宽度到左   
  210.         if (viewNumber == getPrevViewNumber(relativeViewNumber))  
  211.         {  
  212.             return offsetWidth;  
  213.         }  
  214.   
  215.         // Position the next view one measured width to the right 位置下一个视图向右一个测量宽度   
  216.         if (viewNumber == getNextViewNumber(relativeViewNumber))  
  217.         {  
  218.             return offsetWidth * -1;  
  219.         }  
  220.   
  221.         return 0;  
  222.     }  
  223.   
  224.     void movePrevious()  
  225.     {  
  226.         // Slide to previous view 滑到上一个视图   
  227.         mFlingDirection = 1;  
  228.         processGesture();  
  229.     }  
  230.   
  231.     void moveNext()  
  232.     {  
  233.         // Slide to next view  滑到下一个视图   
  234.         mFlingDirection = -1;  
  235.         processGesture();  
  236.     }  
  237.   
  238.      @Override  
  239.      public boolean onKeyDown(int keyCode, KeyEvent event)  
  240.      {  
  241.         switch (keyCode)  
  242.         {  
  243.         case KeyEvent.KEYCODE_DPAD_LEFT:  
  244.             movePrevious();  
  245.             return true;  
  246.       
  247.         case KeyEvent.KEYCODE_DPAD_RIGHT:  
  248.             moveNext();  
  249.             return true;  
  250.       
  251.         case KeyEvent.KEYCODE_DPAD_CENTER:  
  252.         case KeyEvent.KEYCODE_ENTER:  
  253.         }  
  254.   
  255.         return super.onKeyDown(keyCode, event);  
  256.     }  
  257.   
  258.     public boolean onGalleryTouchEvent(MotionEvent event)  
  259.     {  
  260.         boolean consumed = mGestureDetector.onTouchEvent(event);  
  261.           
  262.         if (event.getAction() == MotionEvent.ACTION_UP)  
  263.         {  
  264.             if (mIsTouched || mIsDragging)  
  265.             {  
  266.                 processScrollSnap();  
  267.                 processGesture();  
  268.             }  
  269.         }  
  270.           
  271.         return consumed;  
  272.     }  
  273.   
  274.     void processGesture()  
  275.     {  
  276.         int newViewNumber = mCurrentViewNumber;  
  277.         int reloadViewNumber = 0;  
  278.         int reloadPosition = 0;  
  279.   
  280.         mIsTouched = false;  
  281.         mIsDragging = false;  
  282.   
  283.         if (mFlingDirection > 0)  
  284.         {  
  285.             if (mCurrentPosition > getFirstPosition() || mIsGalleryCircular == true)  
  286.             {  
  287.                 // Determine previous view and outgoing view to recycle 确定之前的观点和外向视图回收   
  288.                 newViewNumber = getPrevViewNumber(mCurrentViewNumber);  
  289.                 mCurrentPosition = getPrevPosition(mCurrentPosition);  
  290.                 reloadViewNumber = getNextViewNumber(mCurrentViewNumber);   
  291.                 reloadPosition = getPrevPosition(mCurrentPosition);  
  292.             }  
  293.         }  
  294.   
  295.         if (mFlingDirection < 0)  
  296.         {  
  297.             if (mCurrentPosition < getLastPosition() || mIsGalleryCircular == true)  
  298.             {  
  299.                 // Determine the next view and outgoing view to recycle 确定下一个视图和外向视图回收   
  300.                 newViewNumber = getNextViewNumber(mCurrentViewNumber);  
  301.                 mCurrentPosition = getNextPosition(mCurrentPosition);  
  302.                 reloadViewNumber = getPrevViewNumber(mCurrentViewNumber);  
  303.                 reloadPosition = getNextPosition(mCurrentPosition);  
  304.             }  
  305.         }  
  306.   
  307.         if (newViewNumber != mCurrentViewNumber)  
  308.         {  
  309.             mCurrentViewNumber = newViewNumber;   
  310.   
  311.             // Reload outgoing view from adapter in new position 重新加载外向视图从适配器在新位置   
  312.             mViews[reloadViewNumber].recycleView(reloadPosition);  
  313.         }  
  314.   
  315.         // Ensure input focus on the current view 确保输入关注当前视图   
  316.         mViews[mCurrentViewNumber].requestFocus();  
  317.   
  318.         // Run the slide animations for view transitions 运行这个幻灯片动画视图转换   
  319.   
  320.         mAnimation.prepareAnimation(mCurrentViewNumber);  
  321.         this.startAnimation(mAnimation);  
  322.   
  323.         // Reset fling state 重置扔状态   
  324.         mFlingDirection = 0;  
  325.     }  
  326.   
  327.     void processScrollSnap()  
  328.     {  
  329.         // Snap to next view if scrolled passed snap position 对齐到下一个视图如果滚动通过快速的位置   
  330.         float rollEdgeWidth = mGalleryWidth * mSnapBorderRatio;  
  331.         int rollOffset = mGalleryWidth - (int) roll