Java 在relativelayout中拖放
我在java和android编程方面的知识非常有限(不是来自prog背景)。因此,如果可能,请用外行术语解释。基本上,我想做一个麻将牌,我可以随意移动。我想解决以下问题:Java 在relativelayout中拖放,java,android,Java,Android,我在java和android编程方面的知识非常有限(不是来自prog背景)。因此,如果可能,请用外行术语解释。基本上,我想做一个麻将牌,我可以随意移动。我想解决以下问题: 我已经通过XML为我的按钮(在屏幕底部)设置了一个初始布局。拖动按钮时,按钮将跳到屏幕顶部。我尝试使用.getX()而不是.getRawX(),但按钮将以混乱的模式移动。而且模拟器非常慢 如何为我创建的每个按钮设置边界?就像在中一样,当我拖动其中一个按钮时,我不想让按钮彼此重叠。这意味着,如果我将瓷砖A移到瓷砖B上,瓷砖B将试
@Override
public boolean onTouch(View view, MotionEvent event) {
final int X = (int) event.getRawX();
final int Y = (int) event.getRawY();
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
RelativeLayout.LayoutParams Params = (RelativeLayout.LayoutParams) view.getLayoutParams();
xDelta = X - Params.leftMargin;
yDelta = Y - Params.topMargin;
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_POINTER_DOWN:
break;
case MotionEvent.ACTION_POINTER_UP:
break;
case MotionEvent.ACTION_MOVE:
//restrict image from going over the screen boundary
if ( (X - xDelta <= -5 || X - xDelta >= 390 ) ||
(Y - yDelta <= -5 || Y - yDelta >= 630 )
)
break;
//Goes back to default drag and drop sequence
else {
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
layoutParams.leftMargin = X - xDelta;
layoutParams.topMargin = Y - yDelta;
layoutParams.rightMargin = -10;
layoutParams.bottomMargin = -10;
view.setLayoutParams(layoutParams);
break;
}
}
environment.invalidate();
return true;
}
@覆盖
公共布尔onTouch(视图、运动事件){
final int X=(int)event.getRawX();
final int Y=(int)event.getRawY();
开关(event.getAction()&MotionEvent.ACTION\u掩码){
case MotionEvent.ACTION\u DOWN:
RelativeLayout.LayoutParams Params=(RelativeLayout.LayoutParams)视图。getLayoutParams();
xDelta=X-参数leftMargin;
yDelta=Y-参数topMargin;
打破
case MotionEvent.ACTION\u UP:
打破
case MotionEvent.ACTION\u指针\u向下:
打破
case MotionEvent.ACTION\u指针\u向上:
打破
case MotionEvent.ACTION\u移动:
//限制图像越过屏幕边界
如果((X-xDelta=390)||
(Y-yDelta=630)
)
打破
//返回默认的拖放顺序
否则{
RelativeLayout.LayoutParams LayoutParams=新的RelativeLayout.LayoutParams(LayoutParams.WRAP\u内容,LayoutParams.WRAP\u内容);
layoutParams.leftMargin=X-xDelta;
layoutParams.topMargin=Y-yDelta;
layoutParams.rightMargin=-10;
layoutParams.bottomMargin=-10;
view.setLayoutParams(layoutParams);
打破
}
}
environment.invalidate();
返回true;
}
在android 2.2中拖放
在这里,我给出了成功的代码。我的主要课程在这里
package info.tempDD;
import android.app.Activity;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class TempDDActivity extends Activity implements OnTouchListener {
/** Called when the activity is first created. */
private View selected_item = null;
private int offset_x = 0;
private int offset_y = 0;
Boolean touchFlag=false;
boolean dropFlag=false;
LayoutParams imageParams;
ImageView imageDrop,image1,image2;
int crashX,crashY;
Drawable dropDrawable,selectDrawable;
Rect dropRect,selectRect;
int topy,leftX,rightX,bottomY;
int dropArray[];
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
ViewGroup container = (ViewGroup) findViewById(R.id.container);
imageDrop=(ImageView) findViewById(R.id.ImgDrop);
image1=(ImageView) findViewById(R.id.img);
image2=(ImageView) findViewById(R.id.img2);
container.setOnTouchListener(new View.OnTouchListener()
{
public boolean onTouch(View v, MotionEvent event)
{
if(touchFlag==true)
{
System.err.println("Display If Part ::->"+touchFlag);
switch (event.getActionMasked())
{
case MotionEvent.ACTION_DOWN :
topy=imageDrop.getTop();
leftX=imageDrop.getLeft();
rightX=imageDrop.getRight();
bottomY=imageDrop.getBottom();
System.err.println("Display Top-->"+topy);
System.err.println("Display Left-->"+leftX);
System.err.println("Display Right-->"+rightX);
System.err.println("Display Bottom-->"+bottomY);
//opRect.
break;
case MotionEvent.ACTION_MOVE:
crashX=(int) event.getX();
crashY=(int) event.getY();
System.err.println("Display Here X Value-->"+crashX);
System.err.println("Display Here Y Value-->"+crashY);
int x = (int) event.getX() - offset_x;
int y = (int) event.getY() - offset_y;
//int w = getWindowManager().getDefaultDisplay().getWidth() - 100;
//int h = getWindowManager().getDefaultDisplay().getHeight() - 100;
int w = getWindowManager().getDefaultDisplay().getWidth() - 50;
int h = getWindowManager().getDefaultDisplay().getHeight() - 10;
if (x > w)
x = w;
if (y > h)
y = h;
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(new ViewGroup.MarginLayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
lp.setMargins(x, y, 0, 0);
//Drop Image Here
if(crashX > leftX && crashX < rightX && crashY > topy && crashY < bottomY )
{
Drawable temp=selected_item.getBackground();
imageDrop.setBackgroundDrawable(temp);
imageDrop.bringToFront();
dropFlag=true;
selected_item.setVisibility(View.INVISIBLE);
}
//Drop Image Here
selected_item.setLayoutParams(lp);
break;
case MotionEvent.ACTION_UP:
//
touchFlag=false;
if(dropFlag==true)
{
dropFlag=false;
}
else
{
selected_item.setLayoutParams(imageParams);
}
break;
default:
break;
}
}else
{
System.err.println("Display Else Part ::->"+touchFlag);
}
return true;
}
});
image1.setOnTouchListener(this);
image2.setOnTouchListener(this);
}
public boolean onTouch(View v, MotionEvent event)
{
switch (event.getActionMasked())
{
case MotionEvent.ACTION_DOWN:
touchFlag=true;
offset_x = (int) event.getX();
offset_y = (int) event.getY();
selected_item = v;
imageParams=v.getLayoutParams();
break;
case MotionEvent.ACTION_UP:
selected_item=null;
touchFlag=false;
break;
default:
break;
}
return false;
}
}
以及我的Xml文件的主要部分
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/ImgDrop"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:layout_marginTop="50dp"
android:background="#FFFFFF" >
</ImageView>
<ImageView
android:id="@+id/img"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:background="@drawable/ic_launcher" >
</ImageView>
<ImageView
android:id="@+id/img2"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="@drawable/ic_launcher" >
</ImageView>
</RelativeLayout>
请参阅我将为布局创建子类,覆盖onLayout()以按我喜欢的方式放置瓷砖。。。让拖放框架完成剩下的工作。。。。使用它,很容易。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/ImgDrop"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:layout_marginTop="50dp"
android:background="#FFFFFF" >
</ImageView>
<ImageView
android:id="@+id/img"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:background="@drawable/ic_launcher" >
</ImageView>
<ImageView
android:id="@+id/img2"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="@drawable/ic_launcher" >
</ImageView>
</RelativeLayout>