0

レイアウトをドラッグしながらレイアウトの背景を動的に変更したい。

タッチリスナーには次のコードを使用しました。

RelativeLayout   rlImages.setOnDragListener(new MyDragListener());
    RelativeLayout   half_left.setOnTouchListener(new MyTouchListener());


private final class MyTouchListener implements OnTouchListener {
    Drawable normalShape = getResources().getDrawable(R.drawable.pinkcircle);

    @SuppressWarnings("deprecation")
    public boolean onTouch(View view, MotionEvent motionEvent) {
        view.setBackgroundResource(R.drawable.pinkcircle);

        if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
            ClipData data = ClipData.newPlainText("", "");
            DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
            view.startDrag(data, shadowBuilder, view, 0);
            view.setVisibility(View.INVISIBLE);
            return true;
        } else {

            return false;
        }
    }
}

私の dragListener クラス:

class MyDragListener implements OnDragListener {
    Drawable enterShape = getResources().getDrawable(R.drawable.halfcircle_left);
    Drawable normalShape = getResources().getDrawable(R.drawable.pinkcircle);

    @SuppressWarnings("deprecation")
    @Override
    public boolean onDrag(View v, DragEvent event) {
        int dragAction = event.getAction();
        final View dragView = (View) event.getLocalState();

        dragView.setBackgroundDrawable(normalShape);
        switch (event.getAction()) {
            case DragEvent.ACTION_DRAG_STARTED:
                // dragView.setBackgroundDrawable(normalShape);
                dragView.setBackgroundDrawable(normalShape);
                // Do nothing
                break;
            case DragEvent.ACTION_DRAG_ENTERED:
                dragView.setBackgroundDrawable(normalShape);
                // v.setBackgroundDrawable(enterShape);
                break;
            case DragEvent.ACTION_DRAG_EXITED:
                // v.setBackgroundDrawable(normalShape);
                break;
            case DragEvent.ACTION_DROP:
                // Dropped, reassign View to ViewGroup
                dragView.setBackgroundDrawable(normalShape);
                break;
            case DragEvent.ACTION_DRAG_ENDED:
            default:
                break;
        }
        return true;
    }
}
4

1 に答える 1