0
我想使用drad n drop或其他方式将Image1移动到屏幕上。我尝试了很多样本,但无法解决。拖放n Drop或其他东西?
需要帮助,我坚持了很久。
代码
public class DnDActivity extends Activity {
int windowwidth;
int windowheight;
ImageView ima1,ima2;
int i=0;
private android.widget.RelativeLayout.LayoutParams layoutParams ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dn_d);
windowwidth = getWindowManager().getDefaultDisplay().getWidth();
windowheight = getWindowManager().getDefaultDisplay().getHeight();
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.setMargins((windowwidth+x_cord)- windowwidth, windowwidth - (windowwidth-y_cord), (windowwidth+x_cord)- windowwidth, windowwidth - (windowwidth-y_cord));
layoutParams.bottomMargin=y_cord-25;
layoutParams.rightMargin=x_cord;
ima1.setLayoutParams(layoutParams);
break;
default: break;
}
return true;
}
});
}
XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/imageview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="@drawable/ic_launcher" />
@RafiKamal:http://polamreddyn.blogspot.in/2013/02/android-drag-and-drop-images-in-one.html –
我试着这个工作很好,但是当我改变ImageView1的位置时。它不会给我预期的答案。 @RafiKamal –
请发布您的完整代码(activity和xml)。 –