我正在创建一个拖放android应用程序,我需要在相对布局上拖动对象(星形,心形)。
我的问题是每次我将对象拖到任何地方。
这是我的代码:
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.left).setOnDragListener(new MyDragListener());
findViewById(R.id.right).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;
ClipData data = ClipData.newPlainText("", "");
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
view.startDrag(data, shadowBuilder, view, 0);
view.setVisibility(View.INVISIBLE);
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:
// Dropped, reassign View to ViewGroup
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;
}
}
这是我的布局:
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="2"
android:columnWidth="300dp"
android:orientation="vertical"
android:rowCount="2"
android:stretchMode="columnWidth" >
<LinearLayout
android:id="@+id/topleft"
android:layout_width="478dp"
android:layout_height="200dp"
android:layout_column="0"
android:layout_row="0"
android:background="@drawable/shape"
android:orientation="vertical"
>
</LinearLayout>
<LinearLayout
android:id="@+id/topright"
android:layout_width="wrap_content"
android:layout_height="200dp"
android:layout_column="1"
android:layout_gravity="left"
android:layout_row="0"
android:background="@drawable/shape"
android:orientation="vertical" >
<ImageView
android:id="@+id/myimage2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_row="0"
android:src="@drawable/ic_launcher" />
<ImageView
android:id="@+id/myimage1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_row="0"
android:src="@drawable/ic_launcher" />
<ImageView
android:id="@+id/myimage3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
</LinearLayout>
希望你能帮助我!感谢
答案 0 :(得分:0)
更新了您的代码。抱歉这一切搞砸了,但你仍然可以移动你的形象。我只研究过Image 1,你也可以在另一方面工作。
MainActivity
import android.app.Activity;
import android.content.ClipData;
import android.os.Bundle;
import android.view.DragEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.DragShadowBuilder;
import android.view.View.OnDragListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;
import android.widget.LinearLayout;
public class ActivityMain extends Activity {
int _xDelta;
int _yDelta;
int dropZone1_X, dropZone2_X, dropZone3_X, dropZone1_Y, dropZone2_Y,
dropZone3_Y, movingCoordinateLeft = 0, movingCoordinateTop = 0;
int leftMargin, topMargin, bottomMargin, rightMargin;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.myimage1).setOnTouchListener(dragt);
findViewById(R.id.myimage2).setOnTouchListener(new MyTouchListener());
findViewById(R.id.myimage3).setOnTouchListener(new MyTouchListener());
// findViewById(R.id.left).setOnDragListener(new MyDragListener());
// findViewById(R.id.right).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();
FrameLayout.LayoutParams lParams = (LayoutParams) view
.getLayoutParams();
_xDelta = X - lParams.leftMargin;
_yDelta = Y - lParams.topMargin;
ClipData data = ClipData.newPlainText("", "");
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(
view);
view.startDrag(data, shadowBuilder, view, 0);
view.setVisibility(View.INVISIBLE);
break;
case MotionEvent.ACTION_UP:
// code to set to a position
LinearLayout.LayoutParams dropLayoutParams = (LinearLayout.LayoutParams) view
.getLayoutParams();
dropLayoutParams.leftMargin = leftMargin;
dropLayoutParams.topMargin = topMargin;
dropLayoutParams.rightMargin = topMargin;
dropLayoutParams.bottomMargin = bottomMargin;
view.setLayoutParams(dropLayoutParams);
view.setVisibility(View.VISIBLE);
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;
leftMargin = layoutParams.leftMargin;
layoutParams.topMargin = Y - _yDelta;
topMargin = layoutParams.topMargin;
layoutParams.rightMargin = -250;
rightMargin = layoutParams.rightMargin;
layoutParams.bottomMargin = -250;
bottomMargin = layoutParams.bottomMargin;
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:
// Dropped, reassign View to ViewGroup
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;
}
}
// onCreate
OnTouchListener dragt = new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
FrameLayout.LayoutParams par = (LayoutParams) v.getLayoutParams();
switch (v.getId()) {// What is being touched
/***
*
* Answer option 1
*
* ***/
case R.id.myimage1: {
// Which action is being taken
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE: {
par.topMargin = (int) event.getRawY()
- (v.getHeight() + 22);
par.leftMargin = (int) event.getRawX()
- (v.getWidth() / 2 + 350);
movingCoordinateLeft = (int) event.getRawX()
- (v.getWidth() / 2 + 0);
movingCoordinateTop = par.topMargin;
System.out.println("Answer 1 --- left"
+ movingCoordinateLeft + "---top"
+ movingCoordinateTop);
v.setLayoutParams(par);
break;
}// inner case MOVE
case MotionEvent.ACTION_UP: {
break;
}// inner case UP
case MotionEvent.ACTION_DOWN: {
System.out.println("left" + event.getRawX());
System.out.println("top" + event.getRawY());
break;
}// inner case UP
}// inner switch
break;
}// case pawn
/***
*
* Answer option 2
*
* ***/
case R.id.myimage2: {// Which action is being taken
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE: {
par.topMargin = (int) event.getRawY()
- (v.getHeight() + 22);
par.leftMargin = (int) event.getRawX()
- (v.getWidth() / 2 + 150);
movingCoordinateLeft = (int) event.getRawX()
- (v.getWidth() / 2 + 0);
movingCoordinateTop = par.topMargin;
v.setLayoutParams(par);
break;
}// inner case MOVE
case MotionEvent.ACTION_UP: {
break;
}// inner case UP
case MotionEvent.ACTION_DOWN: {
break;
}// inner case UP
}// inner switch
break;
}// case pawn2
/***
*
* Answer option 3
*
* ***/
case R.id.myimage3: {// Which action is being taken
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE: {
par.topMargin = (int) event.getRawY()
- (v.getHeight() + 22);
par.leftMargin = (int) event.getRawX()
- (v.getWidth() / 2 + 150);
movingCoordinateLeft = (int) event.getRawX()
- (v.getWidth() / 2 + 0);
movingCoordinateTop = par.topMargin;
v.setLayoutParams(par);
break;
}// inner case MOVE
case MotionEvent.ACTION_UP: {
break;
}// inner case UP
case MotionEvent.ACTION_DOWN: {
v.setLayoutParams(par);
break;
}// inner case UP
}// inner switch
break;
}// case pawn2
}// switch
return true;
}
};
}
您还需要在 activity_main.xml
中重新定位您的观看次数<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="2"
android:columnWidth="300dp"
android:orientation="vertical"
android:rowCount="2"
android:stretchMode="columnWidth" >
<FrameLayout
android:id="@+id/topright"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="left"
android:background="@drawable/shape" >
<ImageView
android:id="@+id/myimage1"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="150dp"
android:src="@drawable/ic_launcher" >
</ImageView>
<ImageView
android:id="@+id/myimage2"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_below="@+id/answer_option_1"
android:layout_gravity="center_horizontal"
android:layout_marginTop="200dp"
android:src="@drawable/ic_launcher" >
</ImageView>
<ImageView
android:id="@+id/myimage3"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="250dp"
android:src="@drawable/ic_launcher" >
</ImageView>
</FrameLayout>
</GridLayout>