移动两个图像视图问题

时间:2013-03-08 09:18:33

标签: android

您好我正在尝试独立移动两个图像视图。我的问题是我能够正确地移动第一个但第二个有问题。我很快触摸它就会移出屏幕。 如果两个ImageViews在同一个位置,它工作正常。但我需要它是不同的 抱歉,如果问题非常基础 这是代码中的任何一个帮助吗?

int windowwidth;
    int windowheight;      
    ImageView ima1,ima2;
    Rect rect=null;

    private android.widget.RelativeLayout.LayoutParams 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.touch);

       /* Display display = getWindowManager().getDefaultDisplay(); 
        int width = display.getWidth();
        int height = display.getHeight();*/


        windowwidth = getWindowManager().getDefaultDisplay().getWidth()-130; 
        windowheight = getWindowManager().getDefaultDisplay().getHeight()-165;

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

        ima1 = (ImageView)findViewById(R.id.icon);
        ima2 = (ImageView)findViewById(R.id.icon1);
        ima2.setX(250);
        ima2.setY(1000);
        ima1.setOnTouchListener(new View.OnTouchListener() {  

            public boolean onTouch(View v, MotionEvent event) {

                layoutParams = (RelativeLayout.LayoutParams) ima1.getLayoutParams();

              //  layoutParams = (RelativeLayout.LayoutParams ima1.getLayoutParams();

                switch(event.getAction())

                {
                    case MotionEvent.ACTION_DOWN:                           
                        break;                   
                    /*case MotionEvent.ACTION_CANCEL:
                        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.leftMargin = x_cord-25;
                        layoutParams.topMargin = y_cord-25;
                 //       layoutParams.rightMargin = x_cord-25;
                  //      layoutParams.bottomMargin = y_cord-25;
                        ima1.setLayoutParams(layoutParams);
                        break;
                    default: break;
                }  
                return true;
            }
        });


        ima2.setOnTouchListener(new View.OnTouchListener() {         

            public boolean onTouch(View v, MotionEvent event) {
                layoutParams = (RelativeLayout.LayoutParams) ima2.getLayoutParams();
                switch(event.getActionMasked())
                {
                    case MotionEvent.ACTION_DOWN:
                         // rect = new Rect(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());

                        break;
                    case MotionEvent.ACTION_MOVE:

                    //  if(!rect.contains((int)event.getX(), (int)event.getY())){

                //      }
//                      else

                        {
                        int x_cord = (int) event.getRawX();
                        int y_cord = (int) event.getRawY();


                        System.out.println("value of x1" +x_cord);
                        System.out.println("value of y1" +y_cord);                          

                        if (x_cord > windowwidth) {
                            x_cord = windowwidth;
                        }
                        if (y_cord > windowheight) {
                            y_cord = windowheight;
                        }

           layoutParams.leftMargin = x_cord - 25;
                        layoutParams.topMargin = y_cord - 75;
                        ima2.setLayoutParams(layoutParams);
                        }
                        break;
                    default: break;
                }
                return true;
            }
        });
    }
}   


XML here


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout  
    xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent">  
   <ImageView
     android:id="@+id/icon"
     android:layout_width="40dp"
     android:layout_height="40dp"
     android:src="@drawable/icon" />      
  <ImageView
     android:id="@+id/icon1"
     android:layout_y="30dip" 
     android:layout_x="118dip"
     android:layout_width="40dp"
     android:layout_height="40dp"
     android:src="@drawable/icon1" />
  <Button
     android:id="@+id/getvalue"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignParentBottom="true"
     android:layout_alignParentLeft="true"
     android:layout_marginBottom="25dp"
     android:text="Reset"
     android:textSize="30dp" />

 </RelativeLayout>

1 个答案:

答案 0 :(得分:0)

请浏览此链接:

它可以帮到你。

http://pragprog.com/titles/eband3/source_code