0

こんにちは私は2つの画像ビューを個別に移動しようとしています。私の問題は、最初のビューを正しく移動できるのに、2番目のビューに問題があることです。タッチするとすぐに画面から移動します。両方のImageViewが同じ場所にある場合は正常に機能していますが、質問が非常に基本的なものである場合は、別の申し訳ありませんが必要です。

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>
4

0 に答える 0