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

0

在 onTouchListener 中,您需要在开始拖动之前更改背景,因为一旦您调用view.startDrag(),视图就会被复制。然后,当拖动结束并再次可见之前,您需要将背景重置为开始时的状态(或您想要的任何其他背景)。

下面显示了它是如何工作的:

public class MainActivity extends Activity implements OnTouchListener, OnDragListener {
    private static final String LOGCAT = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // doing view stuff...
    }

    public boolean onTouch(View view, MotionEvent motionEvent) {
        if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
            DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
            // set background of dragged view to red
            view.setBackgroundResource(android.R.color.holo_red_dark);  
            view.startDrag(null, shadowBuilder, view, 0);
            view.setVisibility(View.INVISIBLE);
            return true;
        } else {
            return false;
        }
    }

    public boolean onDrag(View layoutview, DragEvent dragevent) {
        int action = dragevent.getAction();
        View dragView = (View) dragevent.getLocalState();
        switch (action) {
            case DragEvent.ACTION_DRAG_STARTED:
                Log.d(LOGCAT, "Drag event started");
                break;
            case DragEvent.ACTION_DRAG_ENTERED:
                Log.d(LOGCAT, "Drag event entered into " + layoutview.toString());
                break;
            case DragEvent.ACTION_DRAG_EXITED:
                Log.d(LOGCAT, "Drag event exited from " + layoutview.toString());
                break;
            case DragEvent.ACTION_DROP:
                Log.d(LOGCAT, "Dropped");
                // reset background of dragged view
                dragView.setBackground(null); 
                ViewGroup owner = (ViewGroup) dragView.getParent();
                owner.removeView(dragView);
                LinearLayout container = (LinearLayout) layoutview;
                container.addView(dragView);
                dragView.setVisibility(View.VISIBLE);
                break;
            case DragEvent.ACTION_DRAG_ENDED:
                Log.d(LOGCAT, "Drag ended");
                break;
            default:
                break;
        }
        return true;
    }
}
于 2013-10-29T11:15:57.383 回答