drad n drop などを使用して Image1 を画面全体に移動したいと考えています。多くのサンプルを試しましたが、解決できませんでした。
助けが必要です、私は長い間立ち往生しています。
コード
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" />