我正在创建一个Android应用程序,它将在主要布局上删除图像。我的问题是如何拖动图像并将其放在另一个布局上。
我的部分代码来自:http://www.vogella.com/articles/AndroidDragAndDrop/article.html
只要您将代码拖放到不在其他布局上移动的相同布局上,我的代码就会正常工作。
这是我的代码:
public class MainActivity extends Activity {
int _xDelta;
int _yDelta;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.myimage1).setOnTouchListener(new MyTouchListener());
findViewById(R.id.myimage2).setOnTouchListener(new MyTouchListener());
findViewById(R.id.myimage3).setOnTouchListener(new MyTouchListener());
findViewById(R.id.myimage4).setOnTouchListener(new MyTouchListener());
findViewById(R.id.topleft).setOnDragListener(new MyDragListener());
findViewById(R.id.topright).setOnDragListener(new MyDragListener());
findViewById(R.id.bottomleft).setOnDragListener(new MyDragListener());
findViewById(R.id.bottomright).setOnDragListener(new MyDragListener());
}
private final class MyTouchListener implements OnTouchListener {
public boolean onTouch(View view, MotionEvent motionEvent) {
final int X = (int) motionEvent.getRawX();
final int Y = (int) motionEvent.getRawY();
switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
LinearLayout.LayoutParams lParams = (LinearLayout.LayoutParams) view.getLayoutParams();
_xDelta = X - lParams.leftMargin;
_yDelta = Y - lParams.topMargin;
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_POINTER_DOWN:
break;
case MotionEvent.ACTION_POINTER_UP:
break;
case MotionEvent.ACTION_MOVE:
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) view.getLayoutParams();
layoutParams.leftMargin = X - _xDelta;
layoutParams.topMargin = Y - _yDelta;
layoutParams.rightMargin = -250;
layoutParams.bottomMargin = -250;
view.setLayoutParams(layoutParams);
break;
}
return true;
}
}
class MyDragListener implements OnDragListener {
@Override
public boolean onDrag(View v, DragEvent event) {
int action = event.getAction();
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
break;
case DragEvent.ACTION_DRAG_ENTERED:
break;
case DragEvent.ACTION_DRAG_EXITED:
break;
case DragEvent.ACTION_DROP:
View view = (View) event.getLocalState();
ViewGroup owner = (ViewGroup) view.getParent();
owner.removeView(view);
LinearLayout container = (LinearLayout) v;
container.addView(view);
view.setVisibility(View.VISIBLE);
break;
case DragEvent.ACTION_DRAG_ENDED:
default:
break;
}
return true;
}
} }
希望你能帮助我。
答案 0 :(得分:0)
我使一个触摸的视图不可见,在旧视图的位置以编程方式创建了相同的视图,并将此新视图设置为移动(root是新创建视图的根布局)。
ImageView tmpView;
private float dX;
private float dY;
public boolean onTouch(View view, MotionEvent event) {
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
view.setVisibility(View.GONE);
Log.d(TAG, "event.getRawX() " + event.getRawX() + ", event.getRawY() " + event.getRawY());
dX = view.getX() - event.getRawX();
dY = view.getY() - event.getRawY();
tmpView = new ImageView(SongActivity.this);
tmpView.setImageDrawable(((ImageView) view).getDrawable());
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(100, 100);
params.leftMargin = (int) event.getRawX();
params.topMargin = (int) event.getRawY();
root.addView(tmpView, params);
RelativeLayout.LayoutParams ivLayoutParams = (RelativeLayout.LayoutParams) tmpView.getLayoutParams();
ivLayoutParams.leftMargin = (int) (event.getRawX());
ivLayoutParams.topMargin = (int) (event.getRawY() - 250);
ivLayoutParams.rightMargin = -250;
ivLayoutParams.bottomMargin = -250;
tmpView.setLayoutParams(ivLayoutParams);
break;
case MotionEvent.ACTION_MOVE:
tmpView.setY(event.getRawY() - 250);
tmpView.setX(event.getRawX() + dX);
break;
case MotionEvent.ACTION_UP:
break;
default:
return false;
}
return true;
}