ppamorim

Untitled

Mar 11th, 2015
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.13 KB | None | 0 0
  1. public class YoutubeLayout extends ViewGroup {
  2.  
  3. private final ViewDragHelper mDragHelper;
  4.  
  5. private View mHeaderView;
  6. private View mDescView;
  7.  
  8. private float mInitialMotionX;
  9. private float mInitialMotionY;
  10.  
  11. private int mDragRange;
  12. private int mTop;
  13. private float mDragOffset;
  14.  
  15.  
  16. public YoutubeLayout(Context context) {
  17.   this(context, null);
  18. }
  19.  
  20. public YoutubeLayout(Context context, AttributeSet attrs) {
  21.   this(context, attrs, 0);
  22. }
  23.  
  24. @Override
  25. protected void onFinishInflate() {
  26.     mHeaderView = findViewById(R.id.viewHeader);
  27.     mDescView = findViewById(R.id.viewDesc);
  28. }
  29.  
  30. public YoutubeLayout(Context context, AttributeSet attrs, int defStyle) {
  31.   super(context, attrs, defStyle);
  32.   mDragHelper = ViewDragHelper.create(this, 1f, new DragHelperCallback());
  33. }
  34.  
  35. public void maximize() {
  36.     smoothSlideTo(0f);
  37. }
  38.  
  39. boolean smoothSlideTo(float slideOffset) {
  40.     final int topBound = getPaddingTop();
  41.     int y = (int) (topBound + slideOffset * mDragRange);
  42.  
  43.     if (mDragHelper.smoothSlideViewTo(mHeaderView, mHeaderView.getLeft(), y)) {
  44.         ViewCompat.postInvalidateOnAnimation(this);
  45.         return true;
  46.     }
  47.     return false;
  48. }
  49.  
  50. private class DragHelperCallback extends ViewDragHelper.Callback {
  51.  
  52.   @Override
  53.   public boolean tryCaptureView(View child, int pointerId) {
  54.         return child == mHeaderView;
  55.   }
  56.  
  57.     @Override
  58.   public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {
  59.       mTop = top;
  60.  
  61.       mDragOffset = (float) top / mDragRange;
  62.  
  63.         mHeaderView.setPivotX(mHeaderView.getWidth());
  64.         mHeaderView.setPivotY(mHeaderView.getHeight());
  65.         mHeaderView.setScaleX(1 - mDragOffset / 2);
  66.         mHeaderView.setScaleY(1 - mDragOffset / 2);
  67.  
  68.         mDescView.setAlpha(1 - mDragOffset);
  69.  
  70.         requestLayout();
  71.   }
  72.  
  73.   @Override
  74.   public void onViewReleased(View releasedChild, float xvel, float yvel) {
  75.       int top = getPaddingTop();
  76.       if (yvel > 0 || (yvel == 0 && mDragOffset > 0.5f)) {
  77.           top += mDragRange;
  78.       }
  79.       mDragHelper.settleCapturedViewAt(releasedChild.getLeft(), top);
  80.   }
  81.  
  82.   @Override
  83.   public int getViewVerticalDragRange(View child) {
  84.       return mDragRange;
  85.   }
  86.  
  87.   @Override
  88.   public int clampViewPositionVertical(View child, int top, int dy) {
  89.       final int topBound = getPaddingTop();
  90.       final int bottomBound = getHeight() - mHeaderView.getHeight() - mHeaderView.getPaddingBottom();
  91.  
  92.       final int newTop = Math.min(Math.max(top, topBound), bottomBound);
  93.       return newTop;
  94.   }
  95.  
  96. }
  97.  
  98. @Override
  99. public void computeScroll() {
  100.   if (mDragHelper.continueSettling(true)) {
  101.       ViewCompat.postInvalidateOnAnimation(this);
  102.   }
  103. }
  104.  
  105. @Override
  106. public boolean onInterceptTouchEvent(MotionEvent ev) {
  107.   final int action = MotionEventCompat.getActionMasked(ev);
  108.  
  109.   if (( action != MotionEvent.ACTION_DOWN)) {
  110.       mDragHelper.cancel();
  111.       return super.onInterceptTouchEvent(ev);
  112.   }
  113.  
  114.   if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
  115.       mDragHelper.cancel();
  116.       return false;
  117.   }
  118.  
  119.   final float x = ev.getX();
  120.   final float y = ev.getY();
  121.   boolean interceptTap = false;
  122.  
  123.   switch (action) {
  124.       case MotionEvent.ACTION_DOWN: {
  125.           mInitialMotionX = x;
  126.           mInitialMotionY = y;
  127.             interceptTap = mDragHelper.isViewUnder(mHeaderView, (int) x, (int) y);
  128.           break;
  129.       }
  130.  
  131.       case MotionEvent.ACTION_MOVE: {
  132.           final float adx = Math.abs(x - mInitialMotionX);
  133.           final float ady = Math.abs(y - mInitialMotionY);
  134.           final int slop = mDragHelper.getTouchSlop();
  135.           if (ady > slop && adx > ady) {
  136.               mDragHelper.cancel();
  137.               return false;
  138.           }
  139.       }
  140.   }
  141.  
  142.   return mDragHelper.shouldInterceptTouchEvent(ev) || interceptTap;
  143. }
  144.  
  145. @Override
  146. public boolean onTouchEvent(MotionEvent ev) {
  147.   mDragHelper.processTouchEvent(ev);
  148.  
  149.   final int action = ev.getAction();
  150.     final float x = ev.getX();
  151.     final float y = ev.getY();
  152.  
  153.     boolean isHeaderViewUnder = mDragHelper.isViewUnder(mHeaderView, (int) x, (int) y);
  154.     switch (action & MotionEventCompat.ACTION_MASK) {
  155.       case MotionEvent.ACTION_DOWN: {
  156.           mInitialMotionX = x;
  157.           mInitialMotionY = y;
  158.           break;
  159.       }
  160.  
  161.       case MotionEvent.ACTION_UP: {
  162.           final float dx = x - mInitialMotionX;
  163.           final float dy = y - mInitialMotionY;
  164.           final int slop = mDragHelper.getTouchSlop();
  165.           if (dx * dx + dy * dy < slop * slop && isHeaderViewUnder) {
  166.               if (mDragOffset == 0) {
  167.                   smoothSlideTo(1f);
  168.               } else {
  169.                   smoothSlideTo(0f);
  170.               }
  171.           }
  172.           break;
  173.       }
  174.   }
  175.  
  176.  
  177.   return isHeaderViewUnder && isViewHit(mHeaderView, (int) x, (int) y) || isViewHit(mDescView, (int) x, (int) y);
  178. }
  179.  
  180.  
  181. private boolean isViewHit(View view, int x, int y) {
  182.     int[] viewLocation = new int[2];
  183.     view.getLocationOnScreen(viewLocation);
  184.     int[] parentLocation = new int[2];
  185.     this.getLocationOnScreen(parentLocation);
  186.     int screenX = parentLocation[0] + x;
  187.     int screenY = parentLocation[1] + y;
  188.     return screenX >= viewLocation[0] && screenX < viewLocation[0] + view.getWidth() &&
  189.             screenY >= viewLocation[1] && screenY < viewLocation[1] + view.getHeight();
  190. }
  191.  
  192. @Override
  193. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  194.     measureChildren(widthMeasureSpec, heightMeasureSpec);
  195.  
  196.     int maxWidth = MeasureSpec.getSize(widthMeasureSpec);
  197.     int maxHeight = MeasureSpec.getSize(heightMeasureSpec);
  198.  
  199.     setMeasuredDimension(resolveSizeAndState(maxWidth, widthMeasureSpec, 0),
  200.             resolveSizeAndState(maxHeight, heightMeasureSpec, 0));
  201. }
  202.  
  203. @Override
  204. protected void onLayout(boolean changed, int l, int t, int r, int b) {
  205.   mDragRange = getHeight() - mHeaderView.getHeight();
  206.  
  207.     mHeaderView.layout(
  208.             0,
  209.             mTop,
  210.             r,
  211.             mTop + mHeaderView.getMeasuredHeight());
  212.  
  213.     mDescView.layout(
  214.             0,
  215.             mTop + mHeaderView.getMeasuredHeight(),
  216.             r,
  217.             mTop  + b);
  218. }
Advertisement
Add Comment
Please, Sign In to add comment