0

タッチで画面上のレイアウトを移動しようとしましたが、右側に沿って画面の下部に向かって移動するとレイアウトが消えます。これを修正する方法。

これが私のコードです:

    public class MainActivity extends Activity {
int windowwidth;
int windowheight;
ImageView ima1, ima2;
RelativeLayout rely1, rely2;
private android.widget.RelativeLayout.LayoutParams layoutParams1,
        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.activity_main);

    windowwidth = getWindowManager().getDefaultDisplay().getWidth();
    windowheight = getWindowManager().getDefaultDisplay().getHeight();

    System.out.println("width" + windowwidth);
    System.out.println("height" + windowheight);

    rely1 = (RelativeLayout) findViewById(R.id.rel1);
    rely1.setOnTouchListener(new View.OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event) {
            layoutParams = (RelativeLayout.LayoutParams) rely1
                    .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 - rely1.getWidth()) {
                    layoutParams.leftMargin = x_cord;
                }
                if (y_cord < windowheight - rely1.getHeight()) {
                    layoutParams.topMargin = y_cord;
                }

                rely1.setLayoutParams(layoutParams);
                break;
            default:
                break;
            }
            return true;
        }
    });

    rely2 = (RelativeLayout) findViewById(R.id.rel2);
    rely2.setOnTouchListener(new View.OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event) {
            layoutParams1 = (RelativeLayout.LayoutParams) rely2
                    .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 - rely2.getWidth()) {
                    layoutParams1.leftMargin = x_cord;
                }
                if (y_cord < windowheight - rely2.getHeight()) {
                    layoutParams1.topMargin = y_cord;
                }
                // layoutParams1.leftMargin = x_cord-25;
                // layoutParams1.topMargin = y_cord-25;
                // layoutParams1.rightMargin = x_cord-25;
                // layoutParams1.bottomMargin = y_cord-25;
                rely2.setLayoutParams(layoutParams1);
                break;
            default:
                break;
            }
            return true;
        }
    });
}

}

ここに私のxmlがあります:

<RelativeLayout
    android:id="@+id/rel1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
 <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />
</RelativeLayout>

<RelativeLayout
    android:id="@+id/rel2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />
</RelativeLayout>



   </RelativeLayout>
4

0 に答える 0