如何在子布局中设置imageView位置

时间:2014-05-10 17:48:43

标签: android

我有嵌套布局。内部相对布局我想设置拖动图像,但问题是图像跨越边界,可以拖动到其他布局。我想限制其区域中的图像,以便它们不会被拖到边界之外。我已经计算了特定布局的宽度和高度,但我无法在特定布局上设置图像。请问有什么解决方案吗? xml布局:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/R1Layout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/wood_bg"
        android:orientation="vertical" >

       <RelativeLayout
            android:id="@+id/R2Layout2"
            android:layout_width="wrap_content"
            android:layout_height="100dp"
            android:background="@drawable/wood_bg" >

            <ImageView
                android:id="@+id/playerimage1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBottom="@+id/playerimage2"
                android:layout_alignParentLeft="true"
                android:layout_marginLeft="35dp"
                android:src="@drawable/player1" />

            <ImageView
                android:id="@+id/playerimage2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignTop="@+id/playerimage4"
                android:layout_marginLeft="39dp"
                android:layout_toRightOf="@+id/playerimage1"
                android:src="@drawable/player2" />

            <ImageView
                android:id="@+id/playerimage3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBottom="@+id/playerimage2"
                android:layout_marginLeft="48dp"
                android:layout_toRightOf="@+id/playerimage2"
                android:layout_weight="0.16"
                android:src="@drawable/player3" />

            <ImageView
                android:id="@+id/playerimage4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginLeft="30dp"
                android:layout_toRightOf="@+id/playerimage3"
                android:src="@drawable/player4" />

            </RelativeLayout>

    < ImageButton
            android:id="@+id/imageButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentBottom="true"
            android:layout_marginBottom="20dp"

            android:src="@drawable/chat" />

        <RelativeLayout
            android:id="@+id/R3Layout"
            android:layout_width="wrap_content"
            android:layout_height="500dp"
             android:layout_below="@+id/relLayout2"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_above="@id/imageButton1"
            android:background="@drawable/ludo" >

            <ImageView
                android:id="@+id/imageView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_x="0dp"
                android:layout_y="0dp"
                android:src="@drawable/green" />

            <ImageView
                android:id="@+id/imageView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_x="0dp"
                android:layout_y="0dp"
                android:src="@drawable/green2" />

            <ImageView
                android:id="@+id/imageView3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_x="0dp"
                android:layout_y="0dp"
                android:src="@drawable/green3" />

        </RelativeLayout>  
    </RelativeLayout>

这是.java文件代码:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    //  setContentView(R.layout.main);
        // MediaPlayer music =
        // MediaPlayer.create(MoveImagesOnTouchActivity.this, R.raw.playback);+

        rLayout = (RelativeLayout) findViewById(R.id.R3Layout);
    //  r2Layout = (RelativeLayout) findViewById(R.id.R2Layout2);
    //  r1Layout = (RelativeLayout) findViewById(R.id.R1Layout);
        //int value= this.rLayout();
        //rLayout.layout(10, 10, 10, 10);
    //  {
    //  RelativeLayout rLayout = new RelativeLayout(getApplicationContext());
        img1 = (ImageView) findViewById(R.id.imageView);
        x1 = (int) ((76 * width) / 100);
        y1 = (int) ((11 * height) / 100);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(480,423);
        //params.leftMargin = x1;
        //params.topMargin = y1;
        img1.setLayoutParams(params);
        img2 = (ImageView) findViewById(R.id.imageView2);
        x2 = (int) ((69 * width) / 100);
        y2 = (int) ((11 * height) / 100);
        RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        params2.leftMargin = x2;
        params2.topMargin = y2;
        img2.setLayoutParams(params2);
        img1.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                status = 1;

                // Log.i("ImageStatus", "" + status);
                // img.setBackgroundColor(Color.WHITE);
                int[] values = new int[2];
                img1.getLocationOnScreen(values);
                // img.getLocationInWindow(values);
                int x = values[0];
                int y = values[1];

                /*
                 * if (event.getAction() == MotionEvent.ACTION_DOWN){ //
                 * if(v.getId()==R.id.imageView){} int eventX=(int)event.getX();
                 * int eventY=(int)event.getY(); int[] values = new int[2];
                 * values[0]= eventX; values[1]= eventY; Log.i("x is @@@@@@@@",
                 * +values[0]+ "y is @@@@@@@:" +values[1]);
                 * //Toast.makeText(MoveImagesOnTouchActivity.this,
                 * " x is : "+x+ // "y :"+y , 5000).show(); } if
                 * (event.getAction() == MotionEvent.ACTION_UP){ int
                 * eventX=(int)event.getX(); int eventY=(int)event.getY(); int[]
                 * values = new int[2]; values[0]= eventX; values[1]= eventY;
                 * Toast.makeText(MoveImagesOnTouchActivity.this, " x is : "+x+
                 * "y :"+y , 5000).show();
                 */

                // }

                // mp=MediaPlayer.create(getApplicationContext(), R.raw.shake);
                // mp.start();

                return false;
            }
        });
        img2.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                status = 2;
                // lp2 = (RelativeLayout.LayoutParams) img2.getLayoutParams();
                // Log.i("ImageStatus", "" + status);
                // img.setBackgroundColor(Color.WHITE);
                int[] values = new int[2];
                img2.getLocationOnScreen(values);
                // img.getLocationInWindow(values);
                int x2 = values[0];
                int y2 = values[1];
                // Toast.makeText(MoveImagesOnTouchActivity.this,
                // " x2 is : "+x2+ "y2 :"+y2 , 500).show();
                return false;
            }
        });
        rLayout.setOnTouchListener(new OnTouchListener() {
            // green

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                if (status == 1) // any event from down and move
                {
                    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                            LayoutParams.WRAP_CONTENT,423);
                    params.topMargin = (int) event.getY() - img1.getHeight() / 2;
                    params.leftMargin = (int) event.getX() - img1.getWidth() / 2;
                    // LayoutParams lp = new
                    // LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,(int)event.getX()-img.getWidth()/2,(int)event.getY()-img.getHeight()/2);
                    img1.setLayoutParams(params);
                    // img.setPadding(20, 40, 20, 40);
                    // img.setBaseline(baseline)

                }
                if (status == 2) // any event from down and move
                {

                    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                            LayoutParams.WRAP_CONTENT,
                            LayoutParams.WRAP_CONTENT);
                    params.topMargin = (int) event.getY() - img2.getHeight()
                            / 2;
                    params.leftMargin = (int) event.getX() - img2.getWidth()
                            / 2;
                    img2.setLayoutParams(params);

                }

1 个答案:

答案 0 :(得分:0)

使用marings并不是最好的选择。相反,请查看此课程https://github.com/rfreedman/android-constrained-drag-and-drop-view/blob/master/src/main/java/net/greybeardedgeek/ConstrainedDragAndDropView.java

尤其

protected void updateDragPosition(MotionEvent motionEvent) {}

方法应该让你知道如何做到这一点。获取目标视图,获取其边界(请记住必须首先绘制视图,但如果您使用的听众不应该那么麻烦)并且您很乐意去。