我正在为项目用户工作,将图像在一个位置移动到屏幕上的另一个位置.我写了一个示例代码来移动图像,但问题是如果我移动一个图像,相邻的图像也开始移动..这是示例代码.任何一个想法.
Main.java
public class MainActivity extends Activity { int windowwIDth; int windowheight; ImageVIEw ima1,ima2; private androID.Widget.relativeLayout.LayoutParams layoutParams ; // private androID.Widget.relativeLayout.LayoutParams layoutParams ; //private androID.Widget.relativeLayout.LayoutParams layoutParams ; @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.main); windowwIDth = getwindowManager().getDefaultdisplay().getWIDth(); windowheight = getwindowManager().getDefaultdisplay().getHeight(); System.out.println("wIDth" +windowwIDth); System.out.println("height" +windowheight); ima1 = (ImageVIEw)findVIEwByID(R.ID.imagevIEw1); ima1.setontouchListener(new VIEw.OntouchListener() { public boolean ontouch(VIEw v, MotionEvent event) { layoutParams = (relativeLayout.LayoutParams) ima1.getLayoutParams(); switch(event.getAction()) { case MotionEvent.ACTION_DOWN: break; case MotionEvent.ACTION_MOVE: int x_cord = (int) event.getRawX(); int y_cord = (int) event.getRawY(); System.out.println("value of x" +x_cord); System.out.println("value of y" +y_cord); if (x_cord > windowwIDth) { x_cord = windowwIDth; } if (y_cord > windowheight) { y_cord = windowheight; } layoutParams.leftmargin = x_cord-25; layoutParams.topmargin = y_cord-25; // layoutParams.rightmargin = x_cord-25; // layoutParams.bottommargin = y_cord-25; ima1.setLayoutParams(layoutParams); break; default: break; } return true; } }); ima2 = (ImageVIEw)findVIEwByID(R.ID.imagevIEw2); ima2.setontouchListener(new VIEw.OntouchListener() { public boolean ontouch(VIEw v, MotionEvent event) { layoutParams = (relativeLayout.LayoutParams) ima2.getLayoutParams(); switch(event.getActionMasked()) { case MotionEvent.ACTION_DOWN: break; case MotionEvent.ACTION_MOVE: int x_cord = (int) event.getRawX(); int y_cord = (int) event.getRawY(); System.out.println("value of x1" +x_cord); System.out.println("value of y1" +y_cord); if (x_cord > windowwIDth) { x_cord = windowwIDth; } if (y_cord > windowheight) { y_cord = windowheight; } layoutParams.leftmargin = x_cord - 25; layoutParams.topmargin = y_cord - 75; ima2.setLayoutParams(layoutParams); break; default: break; } return true; } }); } }
main.xml中
<?xml version="1.0" enCoding="utf-8"?> <relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent"> <ImageVIEw androID:layout_wIDth="100dp" androID:layout_height="100dp" androID:ID="@+ID/imagevIEw1" androID:src="@drawable/image1" /> <ImageVIEw androID:layout_wIDth="100sp" androID:layout_height="100sp" androID:ID="@+ID/imagevIEw2" androID:src="@drawable/image2" /> </relativeLayout>
解决方法:
将以下代码写入您的活动文件.
windowwIDth = getwindowManager().getDefaultdisplay().getWIDth();windowheight = getwindowManager().getDefaultdisplay().getHeight();tv1 = (ImageVIEw)findVIEwByID(R.ID.image);tv1.setontouchListener(new VIEw.OntouchListener() { @OverrIDe public boolean ontouch(VIEw v, MotionEvent event) { layoutParams1 = (relativeLayout.LayoutParams) tv1.getLayoutParams(); switch(event.getActionMasked()) { case MotionEvent.ACTION_DOWN: break; case MotionEvent.ACTION_MOVE: int x_cord = (int) event.getRawX(); int y_cord = (int) event.getRawY(); if (x_cord > windowwIDth) { x_cord = windowwIDth; } if (y_cord > windowheight) { y_cord = windowheight; } layoutParams1.leftmargin = x_cord - 25; layoutParams1.topmargin = y_cord - 75; tv1.setLayoutParams(layoutParams1); break; default: break; } return true; }});tv2 = (ImageVIEw)findVIEwByID(R.ID.image1);tv2.setontouchListener(new VIEw.OntouchListener() { @OverrIDe public boolean ontouch(VIEw v, MotionEvent event) { layoutParams2 = (relativeLayout.LayoutParams) tv2.getLayoutParams(); switch(event.getActionMasked()) { case MotionEvent.ACTION_DOWN: break; case MotionEvent.ACTION_MOVE: int x_cord = (int) event.getRawX(); int y_cord = (int) event.getRawY(); if (x_cord > windowwIDth) { x_cord = windowwIDth; } if (y_cord > windowheight) { y_cord = windowheight; } layoutParams2.leftmargin = x_cord - 25; layoutParams2.topmargin = y_cord - 75; tv2.setLayoutParams(layoutParams2); break; default: break; } return true; }});
XML文件: –
<?xml version="1.0" enCoding="utf-8"?><relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent"> <ImageVIEw androID:layout_wIDth="50sp" androID:layout_height="50sp" androID:ID="@+ID/image" androID:src="@drawable/image"> </ImageVIEw> <ImageVIEw androID:layout_y="30dip" androID:layout_x="118dip" androID:layout_wIDth="50sp" androID:layout_height="50sp" androID:ID="@+ID/image1" androID:src="@drawable/image1"> </ImageVIEw></relativeLayout>
总结 以上是内存溢出为你收集整理的Android在屏幕上拖放图像?全部内容,希望文章能够帮你解决Android在屏幕上拖放图像?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)