0
我想在拖動佈局的同時動態更改佈局的背景。如何在Android中拖動佈局時動態更改佈局背景?
我用下面的代碼來實現觸摸監聽器。
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;
}
}
什麼問題? –
我想在拖動它的同時更改佈局的背景,但無法在任何DragEvents中做到這一點 – user1891910
您可以顯示代碼將「MyTouchListener」和「MyDragListener」設置爲View的代碼? –