2011-09-09 5 views
0

내 문제는 ImageView의 onTouchEvent 이미지를 이동합니다. 하나의 화면에 두 개의 이미지가 있지만 이미지를 터치하면 문제가 발생합니다. & 완벽하게 작동하지만 이미지 1을 터치하면 문제가 발생합니다. 그 순간 image2가 원래 위치로 이동하기 때문에 문제가 무엇입니까? 나쁜 영어 의사 소통에 죄송합니다.터치 이벤트 문제 이미지보기

도와주세요. ,, u는 여기에 더 어 Repu을 가지고 있지만

main.xml에

<?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:layout_width="50sp" android:layout_height="50sp" 
     android:id="@+id/image" android:src="@drawable/image"> 
    </ImageView> 
    <ImageView android:layout_y="30dip" android:layout_x="118dip" 
     android:layout_width="50sp" android:layout_height="50sp" android:id="@+id/image1" 
     android:src="@drawable/image1"> 
    </ImageView> 
</RelativeLayout> 

MainActivity.java:-

public class MainActivity extends Activity implements OnTouchListener { 
    int windowwidth; 
    int windowheight; 

    private RelativeLayout.LayoutParams layoutParams; 
    private RelativeLayout.LayoutParams layoutParams1; 

     ImageView image, image1; 
    int x_cord, y_cord; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

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

     image = (ImageView) findViewById(R.id.image); 
     image.setOnTouchListener(this); 

     image1 = (ImageView) findViewById(R.id.image1); 
     image1.setOnTouchListener(this); 

    } 

    public boolean onTouch(View v, MotionEvent event) { 
     switch (v.getId()) { 
     case R.id.image: 
      System.out.println("Image is Touched"); 

      image = (ImageView) findViewById(R.id.image); 
      layoutParams = (RelativeLayout.LayoutParams) image.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(); 

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

       layoutParams.leftMargin = x_cord - 25; 
       layoutParams.topMargin = y_cord - 75; 

       image.setLayoutParams(layoutParams); 
       break; 
      default: 
       break; 
      } 
     case R.id.image1: 
      System.out.println("Image 1 is Touched"); 
      image1 = (ImageView) findViewById(R.id.image1); 
      layoutParams1 = (RelativeLayout.LayoutParams) image1 
        .getLayoutParams(); 
      switch (event.getAction()) { 
      case MotionEvent.ACTION_DOWN: 
       break; 
      case MotionEvent.ACTION_MOVE: 
       x_cord = (int) event.getRawX(); 
       y_cord = (int) event.getRawY(); 

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

       layoutParams1.leftMargin = x_cord - 25; 
       layoutParams1.topMargin = y_cord - 75; 

       image1.setLayoutParams(layoutParams1); 

       break; 
      default: 
       break; 
      } 
     } 
     return true; 
    } 
} 

답변

2

사용 : 내가 수직 위치에 이미지 뷰를 사용하고있는 경우

tv1 = (TextView)findViewById(R.id.text_view1); 
    tv1.setOnTouchListener(new View.OnTouchListener() {   

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      layoutParams1 = (RelativeLayout.LayoutParams) tv1.getLayoutParams(); 
      switch(event.getActionMasked()) 
      { 
     case MotionEvent.ACTION_DOWN: 
      break; 
     case MotionEvent.ACTION_MOVE: 
      int x_cord = (int) event.getRawX(); 
      int y_cord = (int) event.getRawY(); 
      if (x_cord > windowwidth) { 
       x_cord = windowwidth; 
      } 
      if (y_cord > windowheight) { 
       y_cord = windowheight; 
      } 
      layoutParams1.leftMargin = x_cord - 25; 
      layoutParams1.topMargin = y_cord - 75; 
      tv1.setLayoutParams(layoutParams1); 
      break; 
     default: 
      break; 
     } 
     return true; 
    } 
    }); 

    tv2 = (TextView)findViewById(R.id.text_view2); 
    tv2.setTextColor(Color.MAGENTA); 
    tv2.setOnTouchListener(new View.OnTouchListener() {   

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      layoutParams2 = (RelativeLayout.LayoutParams) tv2.getLayoutParams(); 
      switch(event.getActionMasked()) 
      { 
     case MotionEvent.ACTION_DOWN: 
      break; 
     case MotionEvent.ACTION_MOVE: 
      int x_cord = (int) event.getRawX(); 
      int y_cord = (int) event.getRawY(); 
      if (x_cord > windowwidth) { 
       x_cord = windowwidth; 
      } 
      if (y_cord > windowheight) { 
       y_cord = windowheight; 
      } 
      layoutParams2.leftMargin = x_cord - 25; 
      layoutParams2.topMargin = y_cord - 75; 
      tv2.setLayoutParams(layoutParams2); 
      break; 
     default: 
      break; 
     } 
     return true; 
    } 
    }); 
+0

당신의 코드는 이미지보기로도 작업하고 있지만 문제는 큰 해상도 이미지 (예 : 320 * 480)를 사용하는 경우 이미지가 제대로 움직이지 않을 때입니다. –

+0

Hello Raj 수행 방법이 코드를 확대, 축소 및 회전합니다. 도와주세요. –

+0

이 작은 코드로는이 링크를 볼 수 없습니다 : http://dl.dropbox.com/u/38493970/Full.java 이것을 사용하면 패닝 (panning) 및 확대/축소가 가능한 코드를이 코드와 통합 할 수 있습니다 .. –

0

,하지만 난 seeeing하고 있지 않다 - :

다음은 내 코드입니다 상대적 레이아웃 방향, 가로 또는 세로 ?? 귀하의 이미지가 나란히 또는 겹쳐 있다는 의미, 어떤 이미지를 터치 할 때마다 이미지 위치 onTouch 이벤트에 문제가있을 수 있습니다. 두 번째 이미지를 가져올 때마다 .. 내게 미안 미안하지만, 그것을 듣지 못할 경우 ..

u는 달성 할 수있는이 같은
+0

문제는 동일합니다. Image2는 항상 원래 상태로옵니다. –