Как сохранить непрозрачность тени перетаскивания после события выпуска - PullRequest
0 голосов
/ 23 ноября 2018

Я смог сделать тень перетаскивания непрозрачной с непрозрачным флагом

view.startDrag(data, shadowBuilder, view, DRAG_FLAG_OPAQUE);

Однако, когда я отпускаю тень перетаскивания, она становится полупрозрачной, когда она возвращается в свою позицию касания.

Я бы хотел, чтобы тень перетаскивания сохраняла непрозрачность 100% на протяжении всего жизненного цикла.Есть ли способ сделать это?

@Override
public boolean onTouch(View view, MotionEvent motionEvent)
{
    Log.d(TAG,"onTouch()");
    switch (motionEvent.getAction())
    {
        case ACTION_DOWN:
            Log.d(TAG, "ACTION_DOWN");

            int[] topLeft = new int[2];
            view.getLocationOnScreen(topLeft);
            float touchPointX = motionEvent.getRawX() - topLeft[0];
            float touchPointY = motionEvent.getRawY() - topLeft[1];

            ClipData data = ClipData.newPlainText("", "");
            //DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
            MyDragShadowBuilder shadowBuilder = new MyDragShadowBuilder((int)touchPointY,(int)touchPointX,view);

            view.setAlpha(0); //transparent thus invisible
            view.startDrag(data, shadowBuilder, view, DRAG_FLAG_OPAQUE);
            break;

        case ACTION_MOVE:
            Log.d(TAG, "ACTION_MOVE");
            break;
        default :
            return false;
    }
    return true;
}

private static class MyDragShadowBuilder extends View.DragShadowBuilder {

    int mTouchPointY;
    int mTouchPointX;
    View mView;

    // The drag shadow image, defined as a drawable thing
    private static Drawable shadow;

    // Defines the constructor for myDragShadowBuilder
    public MyDragShadowBuilder(int touchPointY, int touchPointX,View view) {

        // Stores the View parameter passed to myDragShadowBuilder.
        super(view);

        mView = view;
        mTouchPointY = touchPointY;
        mTouchPointX = touchPointX;

        // Creates a draggable image that will fill the Canvas provided by the system.
        shadow = new ColorDrawable(Color.WHITE);
    }

    // Defines a callback that sends the drag shadow dimensions and touch point back to the
    // system.
    @Override
    public void onProvideShadowMetrics (Point size, Point touch) {
        // Defines local variables
         int width, height;

        // Sets the width of the shadow to half the width of the original View
        width = getView().getWidth();// / 2;

        // Sets the height of the shadow to half the height of the original View
        height = getView().getHeight();// / 2;

        // The drag shadow is a ColorDrawable. This sets its dimensions to be the same as the
        // Canvas that the system will provide. As a result, the drag shadow will fill the
        // Canvas.
        shadow.setBounds(0, 0, width, height);

        // Sets the size parameter's width and height values. These get back to the system
        // through the size parameter.
        size.set(width, height);

        // Sets the touch point's position to be in the middle of the drag shadow
        touch.set(mTouchPointX, mTouchPointY);
    }

    // Defines a callback that draws the drag shadow in a Canvas that the system constructs
    // from the dimensions passed in onProvideShadowMetrics().
    @Override
    public void onDrawShadow(Canvas canvas) {

        // Draws the ColorDrawable in the Canvas passed in from the system.

        //canvas.drawARGB(0,0,0,0);
        mView.draw(canvas);
        //shadow.draw(canvas);
    }
}

@Override
public boolean onDrag(View view, DragEvent event)
{
    Log.d(TAG,"onDrag()");
    final View viewToDrag = (View) event.getLocalState();
    switch (event.getAction()) {
        case DragEvent.ACTION_DRAG_STARTED:
            Log.d(TAG,"ACTION_DRAG_STARTED");
            break;
        case DragEvent.ACTION_DRAG_ENTERED:
            Log.d(TAG,"ACTION_DRAG_ENTERED");
            break;
        case DragEvent.ACTION_DRAG_EXITED:
            Log.d(TAG,"ACTION_DRAG_EXITED");
            break;
        case DragEvent.ACTION_DROP:
            Log.d(TAG,"ACTION_DROP");
            break;
        case DragEvent.ACTION_DRAG_ENDED:
            Log.d(TAG,"ACTION_DRAG_ENDED");
            viewToDrag.setAlpha(1f); //transparent thus invisible
            break;
        default:
            Log.d(TAG,"default");
            break;
    }
    return true;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...