我想在拖动布局时动态更改布局的背景。
我将以下代码用于触摸侦听器。
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;
}
}