public void onTouchEvent(MotionEvent event) { mGestrueDetector.onTouchEvent(event); int action = MotionEventCompat.getActionMasked(event); switch (action) { case (MotionEvent.ACTION_UP): // Log.d(DEBUG_TAG, "Action was UP"); if (mStarted) { mListener.onDragEnd(mOriginalEvent, event); } mStarted = false; break; case (MotionEvent.ACTION_DOWN): // need to set this, quick tap will not generate drap event, so the // originalEvent may be null for case action_up // which lead to null pointer mOriginalEvent = event; break; case (MotionEvent.ACTION_CANCEL): Log.d(DEBUG_TAG, "Action was cancel!"); if (mStarted) { mListener.onDragEnd(mOriginalEvent, event); } mStarted = false; break; } }
@Override protected boolean handleEvent(Event evt) { if (evt.isMouseEventNoWheel()) { if (dragActive) { if (evt.isMouseDragEnd()) { if (listener != null) { listener.dragStopped(this, evt); } dragActive = false; getAnimationState().setAnimationState(STATE_DRAG_ACTIVE, false); } else if (listener != null) { listener.dragging(this, evt); } } else if (evt.isMouseDragEvent()) { dragActive = true; getAnimationState().setAnimationState(STATE_DRAG_ACTIVE, true); if (listener != null) { listener.dragStarted(this, evt); } } return true; } return super.handleEvent(evt); }
/** This only gets called as a result of drag view cleanup being deferred in endDrag(); */ void onDeferredEndDrag(DragView dragView) { dragView.remove(); if (mDragObject.deferDragViewCleanupPostAnimation) { // If we skipped calling onDragEnd() before, do it now for (DragListener listener : mListeners) { listener.onDragEnd(); } } }
/** * 进行处理拖拽事件 * * @param mainLeft */ private void dispatchDragEvent(int mainLeft) { if (dragListener == null) { return; } float percent = mainLeft / (float) range; animateView(percent); // 进行回调滑动的百分比 dragListener.onDrag(percent); Status lastStatus = status; if (lastStatus != getStatus() && status == Status.Close) { dragListener.onClose(); } else if (lastStatus != getStatus() && status == Status.Open) { dragListener.onOpen(); } }
private void endDrag() { if (mDragging) { mDragging = false; if (mDragBitmap != null) { mDragBitmap.recycle(); } if (mOriginator != null) { mOriginator.setVisibility(VISIBLE); } // Faruq: ArrayList For Each for (DragListener l : mListener) { l.onDragEnd(); } } }
/** * Shuffle list items given new float position. * * @param floatPos */ private boolean shuffleItems(int floatPos) { // Log.d("mobeta", "float position: " + floatPos); // Log.d("mobeta", "exp position: " + mExpDragPos); // Log.d("mobeta", "first position: " + getFirstVisiblePosition() + // " height: " + getChildAt(0).getHeight()); if (floatPos != mExpDragPos) { // if we get here, the ListView is inconsistent with the // floating view // Log.d("mobeta", // "collapsing="+mExpDragPos+" expanding="+floatPos); collapseItem(mExpDragPos); expandItem(floatPos); // callback if (mDragListener != null) { final int numHeaders = getHeaderViewsCount(); mDragListener.drag(mExpDragPos - numHeaders, floatPos - numHeaders); } // update state mExpDragPos = floatPos; updateListState(); return true; } else { return false; } }
@Override public boolean onTouchEvent(MotionEvent ev) { if ((mDragListener != null || mDropListener != null) && mDragView != null) { int action = ev.getAction(); switch (action) { case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: Rect r = mTempRect; mDragView.getDrawingRect(r); stopDragging(); if (mDropListener != null && mDragPos >= 0 && mDragPos < getCount()) { mDropListener.drop(mFirstDragPos, mDragPos); } unExpandViews(false); break; case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_MOVE: int x = (int) ev.getX(); int y = (int) ev.getY(); dragView(x, y); int itemnum = getItemForPosition(y); if (itemnum >= 0) { if (action == MotionEvent.ACTION_DOWN || itemnum != mDragPos) { if (mDragListener != null) { mDragListener.drag(mDragPos, itemnum); } mDragPos = itemnum; doExpansion(); } int speed = 0; adjustScrollBounds(y); if (y > mLowerBound) { // scroll the list up a bit speed = y > (mHeight + mLowerBound) / 2 ? 16 : 4; } else if (y < mUpperBound) { // scroll the list down a bit speed = y < mUpperBound / 2 ? -16 : -4; } if (speed != 0) { int ref = pointToPosition(0, mHeight / 2); if (ref == AdapterView.INVALID_POSITION) { // we hit a divider or an invisible view, check // somewhere else ref = pointToPosition(0, mHeight / 2 + getDividerHeight() + 64); } View v = getChildAt(ref - getFirstVisiblePosition()); if (v != null) { int pos = v.getTop(); setSelectionFromTop(ref, pos - speed); } } } break; } return true; } return super.onTouchEvent(ev); }
/** * Starts a drag. * * @param b The bitmap to display as the drag image. It will be re-scaled to the enlarged size. * @param screenX The x position on screen of the left-top of the bitmap. * @param screenY The y position on screen of the left-top of the bitmap. * @param textureLeft The left edge of the region inside b to use. * @param textureTop The top edge of the region inside b to use. * @param textureWidth The width of the region inside b to use. * @param textureHeight The height of the region inside b to use. * @param source An object representing where the drag originated * @param dragInfo The data associated with the object that is being dragged * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or {@link * #DRAG_ACTION_COPY} */ public void startDrag( Bitmap b, int screenX, int screenY, int textureLeft, int textureTop, int textureWidth, int textureHeight, DragSource source, Object dragInfo, int dragAction) { if (PROFILE_DRAWING_DURING_DRAG) { android.os.Debug.startMethodTracing("Launcher"); } // Hide soft keyboard, if visible if (mInputMethodManager == null) { mInputMethodManager = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE); } mInputMethodManager.hideSoftInputFromWindow(mWindowToken, 0); if (mListener != null) { mListener.onDragStart(source, dragInfo, dragAction); } int registrationX = ((int) mMotionDownX) - screenX; int registrationY = ((int) mMotionDownY) - screenY; mTouchOffsetX = mMotionDownX - screenX; mTouchOffsetY = mMotionDownY - screenY; mDragging = true; mDragSource = source; mDragInfo = dragInfo; mVibrator.vibrate(VIBRATE_DURATION); DragView dragView = mDragView = new DragView( mContext, b, registrationX, registrationY, textureLeft, textureTop, textureWidth, textureHeight); if (mDragView != null) { mDragView.setmOriginalWidth(textureWidth); mDragView.setmOriginalHeight(textureHeight); dragView.setmOriginalWidth(textureWidth); dragView.setmOriginalHeight(textureHeight); } dragView.show(mWindowToken, (int) mMotionDownX, (int) mMotionDownY); }
// destroy drag view private void stopDrag(int itemIndex) { if (mDragView != null) { if (mDragListener != null) mDragListener.onStopDrag(getChildAt(itemIndex)); mDragView.setVisibility(GONE); WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE); wm.removeView(mDragView); mDragView.setImageDrawable(null); mDragView = null; } }
// move the drag view private void drag(int x, int y) { if (mDragView != null) { WindowManager.LayoutParams layoutParams = (WindowManager.LayoutParams) mDragView.getLayoutParams(); layoutParams.x = x; layoutParams.y = y - mDragPointOffset; WindowManager mWindowManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE); mWindowManager.updateViewLayout(mDragView, layoutParams); if (mDragListener != null) mDragListener.onDrag(x, y, null); // change null to "this" when ready to use } }
private void endDrag() { if (mDragging) { mDragging = false; clearScrollRunnable(); boolean isDeferred = false; if (mDragObject.dragView != null) { isDeferred = mDragObject.deferDragViewCleanupPostAnimation; if (!isDeferred) { mDragObject.dragView.remove(); } mDragObject.dragView = null; } // Only end the drag if we are not deferred if (!isDeferred) { for (DragListener listener : mListeners) { listener.onDragEnd(); } } } releaseVelocityTracker(); }
private void endDrag() { if (mDragging) { mDragging = false; if (mOriginator != null) { mOriginator.setVisibility(View.VISIBLE); } if (mListener != null) { mListener.onDragEnd(); } if (mDragView != null) { mDragView.remove(); mDragView = null; } } }
// destroy drag view private void stopDrag(int itemIndex, int x, int y) { if (mDragView != null) { View item = getChildAt(itemIndex); if (mDragListener != null) mDragListener.onStopDrag(item); if (mRemoveListener != null && x > mDragView.getWidth() * 0.7 && y - mDragItemY > 0 && y - mDragItemY < mDragView.getHeight()) { mRemoveListener.onRemove(itemIndex); } mDragView.setVisibility(GONE); WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE); wm.removeView(mDragView); mDragView.setImageDrawable(null); mDragView = null; } }
// enable the drag view for dragging private void startDrag(int itemIndex, int y) { stopDrag(itemIndex); View item = getChildAt(itemIndex); if (item == null) { return; } item.setDrawingCacheEnabled(true); if (mDragListener != null) { mDragListener.onStartDrag(item); } // Create a copy of the drawing cache so that it does not get recycled // by the framework when the list tries to clean up memory // FIXME bitmap should to be a mDragView, but not work for android 4.0.x // Bitmap bitmap = Bitmap.createBitmap(item.getDrawingCache()); WindowManager.LayoutParams mWindowParams = new WindowManager.LayoutParams(); mWindowParams.gravity = Gravity.TOP; mWindowParams.x = 0; mWindowParams.y = y - mDragPointOffset; mWindowParams.height = android.view.ViewGroup.LayoutParams.WRAP_CONTENT; mWindowParams.width = android.view.ViewGroup.LayoutParams.WRAP_CONTENT; mWindowParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS; mWindowParams.format = PixelFormat.TRANSLUCENT; mWindowParams.windowAnimations = 0; Context context = getContext(); ImageView v = new ImageView(context); v.setBackgroundResource(R.drawable.icmpmove); // v.setImageBitmap(bitmap); WindowManager mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); mWindowManager.addView(v, mWindowParams); mDragView = v; }
private void endDrag() { if (mDragging) { mDragging = false; if (mOriginator != null && (mOriginator.getContentDescription() == null || !mOriginator.getContentDescription().toString().equalsIgnoreCase("dropped"))) { mOriginator.setVisibility(View.VISIBLE); } if (mListener != null) { mListener.onDragEnd(); } if (mDragView != null) { mDragView.remove(); mDragView = null; } // mOriginator.getParent().recomputeViewAttributes(mOriginator); } }
// move the drag view private void drag(int x, int y) { WindowManager.LayoutParams layoutParams = (WindowManager.LayoutParams) mDragView.getLayoutParams(); layoutParams.x = mRemoveListener != null ? x : 0; layoutParams.y = y - mDragPointOffset; WindowManager mWindowManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE); mWindowManager.updateViewLayout(mDragView, layoutParams); if (mRemoveListener != null && x > mDragView.getWidth() * 0.7 && y - mDragItemY > 0 && y - mDragItemY < mDragView.getHeight()) { mDragView.setBackgroundResource(R.drawable.alert); } else if (mRemoveListener != null) { mDragView.setBackgroundResource(android.R.drawable.alert_dark_frame); } if (mDragListener != null) mDragListener.onDrag(x, y, null); // change null to "this" when ready to use }
@Override public boolean onTouchEvent(MotionEvent ev) { if (mGestureDetector != null) { mGestureDetector.onTouchEvent(ev); } if ((mDragListener != null || mDropListener != null) && mDragView != null) { int action = ev.getAction(); switch (action) { case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: Rect r = mTempRect; mDragView.getDrawingRect(r); stopDragging(); if (mRemoveMode == SLIDE && ev.getX() > r.right * 3 / 4) { if (mRemoveListener != null) { mRemoveListener.remove(mSrcDragPos); } unExpandViews(true); } else { if (mDropListener != null && mDragPos >= 0 && mDragPos < getCount()) { mDropListener.drop(mSrcDragPos, mDragPos); } unExpandViews(false); } break; case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_MOVE: int x = (int) ev.getX(); int y = (int) ev.getY(); dragView(x, y); int itemnum = getItemForPosition(y); if (itemnum >= 0) { if (action == MotionEvent.ACTION_DOWN || itemnum != mDragPos) { if (mDragListener != null) { mDragListener.drag(mDragPos, itemnum); } mDragPos = itemnum; doExpansion(); } int speed = 0; adjustScrollBounds(y); if (y > mLowerBound) { // scroll the list up a bit if (getLastVisiblePosition() < getCount() - 1) { speed = y > (mHeight + mLowerBound) / 2 ? 16 : 4; } else { speed = 1; } } else if (y < mUpperBound) { // scroll the list down a bit speed = y < mUpperBound / 2 ? -16 : -4; if (getFirstVisiblePosition() == 0 && getChildAt(0).getTop() >= getPaddingTop()) { // if we're already at the top, don't try to scroll, because // it causes the framework to do some extra drawing that messes // up our animation speed = 0; } } if (speed != 0) { smoothScrollBy(speed, 30); } } break; } return true; } return super.onTouchEvent(ev); }
/** * Starts a drag. * * @param b The bitmap to display as the drag image. It will be re-scaled to the enlarged size. * @param dragLayerX The x position in the DragLayer of the left-top of the bitmap. * @param dragLayerY The y position in the DragLayer of the left-top of the bitmap. * @param source An object representing where the drag originated * @param dragInfo The data associated with the object that is being dragged * @param dragAction The drag action: either {@link #DRAG_ACTION_MOVE} or {@link * #DRAG_ACTION_COPY} * @param dragRegion Coordinates within the bitmap b for the position of item being dragged. Makes * dragging feel more precise, e.g. you can clip out a transparent border */ public void startDrag( Bitmap b, int dragLayerX, int dragLayerY, DragSource source, Object dragInfo, int dragAction, Point dragOffset, Rect dragRegion, float initialDragViewScale) { if (PROFILE_DRAWING_DURING_DRAG) { android.os.Debug.startMethodTracing("Launcher"); } // Hide soft keyboard, if visible if (mInputMethodManager == null) { mInputMethodManager = (InputMethodManager) mLauncher.getSystemService(Context.INPUT_METHOD_SERVICE); } mInputMethodManager.hideSoftInputFromWindow(mWindowToken, 0); for (DragListener listener : mListeners) { listener.onDragStart(source, dragInfo, dragAction); } final int registrationX = mMotionDownX - dragLayerX; final int registrationY = mMotionDownY - dragLayerY; final int dragRegionLeft = dragRegion == null ? 0 : dragRegion.left; final int dragRegionTop = dragRegion == null ? 0 : dragRegion.top; mDragging = true; mDragObject = new DropTarget.DragObject(); mDragObject.dragComplete = false; mDragObject.xOffset = mMotionDownX - (dragLayerX + dragRegionLeft); mDragObject.yOffset = mMotionDownY - (dragLayerY + dragRegionTop); mDragObject.dragSource = source; mDragObject.dragInfo = dragInfo; final DragView dragView = mDragObject.dragView = new DragView( mLauncher, b, registrationX, registrationY, 0, 0, b.getWidth(), b.getHeight(), initialDragViewScale); if (dragOffset != null) { dragView.setDragVisualizeOffset(new Point(dragOffset)); } if (dragRegion != null) { dragView.setDragRegion(new Rect(dragRegion)); } mLauncher.getDragLayer().performHapticFeedback(HapticFeedbackConstants.LONG_PRESS); dragView.show(mMotionDownX, mMotionDownY); handleMoveEvent(mMotionDownX, mMotionDownY); }
public void startDrag(View v, DragSource source, Object dragInfo, int dragAction) { if (PROFILE_DRAWING_DURING_DRAG) { android.os.Debug.startMethodTracing("Launcher"); } mTagPopup = v.getTag(R.id.TAG_PREVIEW); // Hide soft keyboard, if visible if (mInputMethodManager == null) { mInputMethodManager = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE); } mInputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0); // Faruq: ArrayList For Each for (DragListener l : mListener) { l.onDragStart(v, source, dragInfo, dragAction); } Rect r = mDragRect; r.set(v.getScrollX(), v.getScrollY(), 0, 0); mOriginalX = mLastMotionX; mOriginalY = mLastMotionY; offsetDescendantRectToMyCoords(v, r); mTouchOffsetX = mLastMotionX - r.left; mTouchOffsetY = mLastMotionY - r.top; v.clearFocus(); v.setPressed(false); boolean willNotCache = v.willNotCacheDrawing(); v.setWillNotCacheDrawing(false); // Reset the drawing cache background color to fully transparent // for the duration of this operation int color = v.getDrawingCacheBackgroundColor(); v.setDrawingCacheBackgroundColor(0); if (color != 0) { v.destroyDrawingCache(); } v.buildDrawingCache(); Bitmap viewBitmap = v.getDrawingCache(); if (viewBitmap != null) { mDrawModeBitmap = true; int width = viewBitmap.getWidth(); int height = viewBitmap.getHeight(); Matrix scale = new Matrix(); float scaleFactor = v.getWidth(); scaleFactor = (scaleFactor + DRAG_SCALE) / scaleFactor; scale.setScale(scaleFactor, scaleFactor); mAnimationTo = 1.0f; mAnimationFrom = 1.0f / scaleFactor; mAnimationDuration = ANIMATION_SCALE_UP_DURATION; mAnimationState = ANIMATION_STATE_STARTING; mAnimationType = ANIMATION_TYPE_SCALE; try { mDragBitmap = Bitmap.createBitmap(viewBitmap, 0, 0, width, height, scale, true); final Bitmap dragBitmap = mDragBitmap; mBitmapOffsetX = (dragBitmap.getWidth() - width) / 2; mBitmapOffsetY = (dragBitmap.getHeight() - height) / 2; } catch (OutOfMemoryError e) { mDrawModeBitmap = false; width = v.getWidth(); height = v.getHeight(); scaleFactor = v.getWidth(); scaleFactor = (scaleFactor + DRAG_SCALE) / scaleFactor; mDrawWidth = (int) (v.getWidth() * scaleFactor); mDrawHeight = (int) (v.getHeight() * scaleFactor); mAnimationTo = 1.0f; mAnimationFrom = 1.0f / scaleFactor; mAnimationDuration = ANIMATION_SCALE_UP_DURATION; mAnimationState = ANIMATION_STATE_STARTING; mAnimationType = ANIMATION_TYPE_SCALE; mBitmapOffsetX = (mDrawWidth - width) / 2; mBitmapOffsetY = (mDrawHeight - height) / 2; } v.destroyDrawingCache(); v.setWillNotCacheDrawing(willNotCache); v.setDrawingCacheBackgroundColor(color); } else { mDrawModeBitmap = false; int width = v.getWidth(); int height = v.getHeight(); float scaleFactor = v.getWidth(); scaleFactor = (scaleFactor + DRAG_SCALE) / scaleFactor; mDrawWidth = (int) (v.getWidth() * scaleFactor); mDrawHeight = (int) (v.getHeight() * scaleFactor); mAnimationTo = 1.0f; mAnimationFrom = 1.0f / scaleFactor; mAnimationDuration = ANIMATION_SCALE_UP_DURATION; mAnimationState = ANIMATION_STATE_STARTING; mAnimationType = ANIMATION_TYPE_SCALE; mBitmapOffsetX = (mDrawWidth - width) / 2; mBitmapOffsetY = (mDrawHeight - height) / 2; } if (dragAction == DRAG_ACTION_MOVE) { v.setVisibility(GONE); } mDragPaint = null; mDragging = true; mShouldDrop = true; mOriginator = v; mDragSource = source; mDragInfo = dragInfo; performHapticFeedback( HapticFeedbackConstants.LONG_PRESS, HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING); mEnteredRegion = false; invalidate(); }