2013-03-08 2 views
0

Привет, я пытаюсь переместить два изображения независимо. Моя проблема в том, что я могу правильно перенести первый, но второй имеет проблемы. Как только я прикасаюсь к нему, он выходит из экрана. Если оба 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> 
+0

Почему вы берете -75 из y_cord для 2-го образа, принимая при этом только 25 прочь для первого изображения –

+0

Я просто пытаюсь с различными values.Even я отрегулировать его до 25 он все еще не работает – user2147760

ответ

0

пожалуйста, перейдите по этой ссылке:

Это должно помочь вам.

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

+0

Спасибо за ссылку. Но ссылка содержит только одно перемещение изображения. Любой, кто-нибудь, дайте мне знать, почему, когда я нажимаю на второе представление, оно выходит за границы – user2147760

Смежные вопросы