Example #1
0
  /**
   * Construct the drag view.
   *
   * <p>The registration point is the point inside our view that the touch events should be centered
   * upon.
   *
   * @param launcher The Launcher instance
   * @param bitmap The view that we're dragging around. We scale it up when we draw it.
   * @param registrationX The x coordinate of the registration point.
   * @param registrationY The y coordinate of the registration point.
   */
  public DragView(
      Launcher launcher,
      Bitmap bitmap,
      int registrationX,
      int registrationY,
      int left,
      int top,
      int width,
      int height,
      final float initialScale) {
    super(launcher);
    mDragLayer = launcher.getDragLayer();
    mInitialScale = initialScale;

    final Resources res = getResources();
    final float offsetX = res.getDimensionPixelSize(R.dimen.dragViewOffsetX);
    final float offsetY = res.getDimensionPixelSize(R.dimen.dragViewOffsetY);
    final float scaleDps = res.getDimensionPixelSize(R.dimen.dragViewScale);
    final float scale = (width + scaleDps) / width;

    // Set the initial scale to avoid any jumps
    setScaleX(initialScale);
    setScaleY(initialScale);

    // Animate the view into the correct position
    mAnim = LauncherAnimUtils.ofFloat(this, 0f, 1f);
    mAnim.setDuration(150);
    mAnim.addUpdateListener(
        new AnimatorUpdateListener() {
          @Override
          public void onAnimationUpdate(ValueAnimator animation) {
            final float value = (Float) animation.getAnimatedValue();

            final int deltaX = (int) ((value * offsetX) - mOffsetX);
            final int deltaY = (int) ((value * offsetY) - mOffsetY);

            mOffsetX += deltaX;
            mOffsetY += deltaY;
            setScaleX(initialScale + (value * (scale - initialScale)));
            setScaleY(initialScale + (value * (scale - initialScale)));
            if (sDragAlpha != 1f) {
              setAlpha(sDragAlpha * value + (1f - value));
            }

            if (getParent() == null) {
              animation.cancel();
            } else {
              setTranslationX(getTranslationX() + deltaX);
              setTranslationY(getTranslationY() + deltaY);
            }
          }
        });

    mBitmap = Bitmap.createBitmap(bitmap, left, top, width, height);
    setDragRegion(new Rect(0, 0, width, height));

    // The point in our scaled bitmap that the touch events are located
    mRegistrationX = registrationX;
    mRegistrationY = registrationY;

    // Force a measure, because Workspace uses getMeasuredHeight() before
    // the layout pass
    int ms = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    measure(ms, ms);
    mPaint = new Paint(Paint.FILTER_BITMAP_FLAG);
  }
Example #2
0
  /**
   * 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);
  }