Skip to content

Instantly share code, notes, and snippets.

@pskink
Last active August 29, 2015 14:07
Show Gist options
  • Save pskink/b747e89c1e1a1e314ca6 to your computer and use it in GitHub Desktop.
Save pskink/b747e89c1e1a1e314ca6 to your computer and use it in GitHub Desktop.
// ViewDragHelper test
public class VG extends ViewGroup {
private static final String TAG = "VG";
private final ViewDragHelper mDragHelper;
private Rect[] mBounds = new Rect[4];
public VG(Context context) {
super(context);
int[] colors = {Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW};
for (int i = 0; i < 4; i++) {
TextView tv = new TextView(getContext());
tv.setTextSize(32);
tv.setGravity(Gravity.CENTER);
tv.setText("#" + i);
tv.setBackgroundColor(colors[i]);
tv.setClickable(true);
addView(tv);
}
mDragHelper = ViewDragHelper.create(this, mCallback);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
int w2 = w / 2;
int h2 = h / 2;
for (int i = 0; i < 4; i++) {
Rect r = new Rect(0, 0, w2, h2);
r.offset(w2 * (i % 2), h2 * (i / 2));
mBounds[i] = r;
getChildAt(i).layout(r.left, r.top, r.right, r.bottom);
}
}
ViewDragHelper.Callback mCallback = new ViewDragHelper.Callback() {
@Override
public boolean tryCaptureView(View view, int i) {
return true;
}
@Override
public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {
invalidate();
// requestLayout();
}
@Override
public int getViewHorizontalDragRange(View child) {
return getWidth();
}
@Override
public int getViewVerticalDragRange(View child) {
return getHeight();
}
@Override
public void onViewCaptured(View capturedChild, int activePointerId) {
bringChildToFront(capturedChild);
}
@Override
public void onViewReleased(View releasedChild, float xvel, float yvel) {
Point pos = computeFinalPosition(releasedChild, xvel, yvel);
mDragHelper.settleCapturedViewAt(pos.x, pos.y);
Log.d(TAG, "onViewReleased settleCapturedViewAt " + pos.x + " " + pos.y);
invalidate();
}
private Point computeFinalPosition(View child, float xvel, float yvel) {
Rect r = new Rect();
child.getHitRect(r);
int cx = r.centerX();
int cy = r.centerY();
if (xvel != 0 || yvel != 0) {
Scroller s = new Scroller(getContext());
int w2 = r.width() / 2;
int h2 = r.height() / 2;
s.fling(cx, cy, (int) xvel, (int) yvel, w2, getWidth() - w2, h2, getHeight() - h2);
cx = s.getFinalX();
cy = s.getFinalY();
}
for (int i = 0; i < 4; i++) {
Rect b = mBounds[i];
if (b.contains(cx, cy)) {
return new Point(b.left, b.top);
}
}
return new Point();
}
@Override
public int clampViewPositionHorizontal(View child, int left, int dx) {
return Math.max(Math.min(left, getWidth() - child.getWidth()), 0);
}
@Override
public int clampViewPositionVertical(View child, int top, int dy) {
return Math.max(Math.min(top, getHeight() - child.getHeight()), 0);
}
};
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
int action = event.getAction();
if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
mDragHelper.cancel();
return false;
}
return mDragHelper.shouldInterceptTouchEvent(event);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
mDragHelper.processTouchEvent(event);
return true;
}
@Override
public void computeScroll() {
boolean rc = mDragHelper.continueSettling(true);
if (rc) {
invalidate();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment