private void startDragging(Bitmap bitmap, int y) {
    stopDragging();

    if (bitmap.getHeight() > maximumDragViewHeight) {
      bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), maximumDragViewHeight);
    }

    ImageView imageView = getGlowingBorder(bitmap);

    WindowManager.LayoutParams dragViewParameters = createLayoutParameters();
    if (isScrolling) {
      isScrolling = false;

      dragViewParameters.y = getHeight() / 2 - bitmap.getHeight() / 2;
    } else {
      dragViewParameters.y = y - bitmap.getHeight() / 2;
    }
    dragViewParameters.windowAnimations = R.style.brick_new;

    WindowManager windowManager = getWindowManager();

    windowManager.addView(imageView, dragViewParameters);

    dragView = imageView;
  }
  public void resetDraggingScreen() {
    stopDragging();

    dimBackground = false;
    dragNewBrick = false;

    invalidate();
  }
  @Override
  public boolean onTouchEvent(MotionEvent event) {

    int x = (int) event.getX();
    int y = (int) event.getY();

    if (y < 0) {
      y = 0;
    } else if (y > getHeight()) {
      y = getHeight();
    }

    int itemPosition = pointToPosition(x, y);
    itemPosition =
        itemPosition < 0 ? ((BrickAdapter) dragAndDropListener).getCount() - 1 : itemPosition;

    if (touchedListPosition != itemPosition) {
      touchedListPosition = itemPosition;
      if (dragAndDropListener != null) {
        dragAndDropListener.setTouchedScript(touchedListPosition);
      }
    }

    if (dragAndDropListener != null && dragView != null) {
      int action = event.getAction();
      switch (action) {
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
          setDragViewAnimation(0);
          dragAndDropListener.drop();

          stopDragging();

          dimBackground = false;
          dragNewBrick = false;
          break;

        case MotionEvent.ACTION_MOVE:
          scrollListWithDraggedItem(y);

          dragTouchedListItem((int) event.getRawY());
          dragItemInList(y, itemPosition);

          dimBackground = true;
          break;
      }
      return true;
    }
    return super.onTouchEvent(event);
  }