2011-09-09 36 views
0

我的問題是我正在移動ImageView的圖像onTouchEvent,我在一個屏幕上有兩個圖像,但問題是如果我觸摸image2 &移動它是完美的工作,但如果我觸摸image1 image2移動到原來的位置,那麼問題是什麼?對不起,英語交流不好。圖像視圖的觸摸事件問題

請幫幫我。

以下是我的代碼: -

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如)工作,但問題是,如果我使用大分辨率圖像當時的圖像不動Properly.ifü有任何想法,然後告訴我。 –

+0

Hello Raj如何在此代碼上執行放大,縮小和旋轉。請幫幫我。 –

+0

這個小碼這是不可能看到此鏈接:http://dl.dropbox.com/u/38493970/Full.java利用這一點,可能的平移和縮放這個代碼集成到我們的代碼.. –

0

雖然ü這裏有更多的REPU,但我不seeeing ,,相對佈局方向,水平還是垂直?是您的圖片並排或重疊,,手段方面,可能存在的問題與圖像位置onTouch事件越來越第二圖象每當任何圖像是觸摸.. 對不起我的英語水平,如果烏拉圭回合沒有得到它..

+0

如果我在垂直位置使用ImageView的問題是一樣的。 Image2總是進入其原始狀態。 –