Android 将一个图像放到另一个图像位置

Android 将一个图像放到另一个图像位置,android,Android,我正在做一个与拖放相关的项目。我有拖放代码。我想要拖放代码 public class MprojectActivity extends Activity implements OnTouchListener{ /** Called when the activity is first created. */ private ImageView dragimage; private ImageView dropimage;

我正在做一个与拖放相关的项目。我有拖放代码。我想要拖放代码

public class MprojectActivity extends Activity implements OnTouchListener{
    /** Called when the activity is first created. */

    private ImageView dragimage;                       
    private ImageView dropimage;                
    int x1,y1; 


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        dragimage = (ImageView) findViewById(R.id.imageView12);
        dragimage.setOnTouchListener(this);
        dropimage = (ImageView) findViewById(R.id.imageView10);

    }

    public boolean onTouch(View v, MotionEvent event) {

            int x = (int) event.getRawX();
            int y = (int) event.getRawY();


        if(event.getAction()==MotionEvent.ACTION_DOWN){
            AbsoluteLayout.LayoutParams lParams = (AbsoluteLayout.LayoutParams) v.getLayoutParams();
            x1 = x - lParams.x;
            y1 = y - lParams.y;

        }


       if (event.getAction() == MotionEvent.ACTION_MOVE) {

            AbsoluteLayout.LayoutParams mParams = (AbsoluteLayout.LayoutParams) v.getLayoutParams(); 
            mParams.x = x-x1; 
            mParams.y = y-y1 ; 
            v.setLayoutParams(mParams);


             }   }

        }  
        return true;

        }

    }

现在,如果我将绘图拖到dropimage附近,我想将绘图拖到dropimage中。我只想将绘图拖到dropimage中,否则绘图会自动返回其位置。我需要拖放代码和位置返回代码。

拖放:

活动:

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布局:

<RelativeLayout android:id="@+id/my_ph"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="bottom">
        <ImageView android:id="@+id/image" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:layout_alignParentTop="true"
            android:background="@drawable/map" />
        <TextView android:id="@+id/textView1" android:layout_width="wrap_content"  android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_below="@+id/image" android:layout_alignParentLeft="true"></TextView>

  </RelativeLayout>

所以,您希望将一个图像置于另一个图像之上。?
RelativeLayout rv = (RelativeLayout) findViewById(R.id.my_ph);
 RelativeLayout.LayoutParams params;
 ImageButton im1 = new ImageButton(this);

 im1.setBackgroundResource(R.drawable.lamp);
 im1.setId(i);
 im1.setOnClickListener(new OnClickListener() {
     public void onClick(View v) {
    TextView tx = (TextView) findViewById(R.id.textView1);
    tx.setText("lamp #" + v.getId());
}
 });

 params = new RelativeLayout.LayoutParams(40, 40);
 params.leftMargin = x;
 params.topMargin = y;
 rv.addView(im1, params);
<RelativeLayout android:id="@+id/my_ph"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="bottom">
        <ImageView android:id="@+id/image" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:layout_alignParentTop="true"
            android:background="@drawable/map" />
        <TextView android:id="@+id/textView1" android:layout_width="wrap_content"  android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_below="@+id/image" android:layout_alignParentLeft="true"></TextView>

  </RelativeLayout>