2011-10-19 67 views
15

以下は、私のxmlファイルです。それはの名前はmain.xmlAndroidの複数のImageViewがタッチで移動する


<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" android:layout_height="fill_parent"> 
    <ImageView android:id="@+id/imageView" android:layout_width="fill_parent" 
     android:layout_height="fill_parent" android:src="@drawable/icon" 
     android:scaleType="matrix"> 
    </ImageView> 
    <ImageView android:id="@+id/imageView1" android:layout_width="fill_parent" 
     android:layout_height="fill_parent" android:src="@drawable/bg_sound" 
     android:scaleType="matrix"></ImageView> 
</FrameLayout> 

で、私のJavaファイルは、私はタッチで移動を達成することができています


import android.app.Activity; 
import android.graphics.Matrix; 
import android.graphics.PointF; 
import android.graphics.drawable.Drawable; 
import android.os.Bundle; 
import android.util.FloatMath; 
import android.util.Log; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.View.OnTouchListener; 
import android.widget.ImageView; 

public class Test1Activity extends Activity implements OnTouchListener { 
    private static final String TAG = "Touch"; 
    // These matrices will be used to move and zoom image 
    Matrix matrix = new Matrix(); 
    Matrix savedMatrix = new Matrix(); 

    // We can be in one of these 3 states 

    // Remember some things for zooming 
    PointF start = new PointF(); 
    PointF mid = new PointF(); 
    float oldDist = 1f; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     ImageView view = (ImageView) findViewById(R.id.imageView); 
     view.setOnTouchListener(this); 

     ImageView view1 = (ImageView) findViewById(R.id.imageView1); 
     view1.setOnTouchListener(this); 
    } 

    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     ImageView view = (ImageView) v; 
     // Dump touch event to log 

     // Handle touch events here... 
     switch (event.getAction() & MotionEvent.ACTION_MASK) { 
     case MotionEvent.ACTION_DOWN: 
      savedMatrix.set(matrix); 
      start.set(event.getX(), event.getY()); 
      break; 
     case MotionEvent.ACTION_UP: 
      savedMatrix.set(matrix); 
      //matrix.postRotate(90); 

      break; 
     case MotionEvent.ACTION_MOVE: 
       // ... 
       matrix.set(savedMatrix); 
       matrix.postTranslate(event.getX() - start.x, event.getY() 
         - start.y); 
      break; 
     } 

     view.setImageMatrix(matrix); 
     view.invalidate(); 
     return true; // indicate event was handled 
    } 

} 

を下回っているが、あるとして2つのimageviewsだけ追加された最新のimageviewsは可動です。

問題はlayout_width="fill_parent"だから、正面画像のみがタッチで認識されることになります。イメージビューよりlayout_width="wrap_content"を使用している場合は、そのイメージサイズの領域を移動して見えなくなります。

この問題を解決するにはどうすればよいですか?

おかげで、

+1

良い質問を使用して、フロントにImageViewのをもたらすことができます。.. –

答えて

1

一つだけのビュープロセスonTouchアクション。そうそこにいくつかのオプション:

1)リターン "false" - それはあなた

2) "wrap_content" を使用してImageMatrixを使用していないに役立つことがあります。画像の左上隅を移動

2

私はあなたの問題をほぼ解決しました。 ので、私は前方 を移動するわけではない時の私はあなたのmain.xmlファイルAのいくつかの変更が

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent" 
    android:orientation="vertical"> 

     <ImageView android:id="@+id/imageView" android:layout_width="fill_parent" 
      android:layout_height="100dp" android:src="@drawable/ic_launcher" 
      android:scaleType="matrix" 
      android:layout_alignParentTop="true" 
      > 
     </ImageView> 
     <ImageView android:id="@+id/imageView1" android:layout_width="fill_parent" 
      android:layout_height="fill_parent" android:src="@drawable/toyota_altis" 
      android:scaleType="matrix" 
      android:layout_below="@+id/imageView"></ImageView> 

</RelativeLayout> 

独自のイメージを使用するに注意してくださいし、適切な変更を行い 私が使用される2枚の画像は

    ですが、以下の作っ
  1. ic_launcher
  2. toyota_altisあなたの活動ファイルにコードの下
0

書き込み。

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


tv1 = (ImageView)findViewById(R.id.image); 
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 = (ImageView)findViewById(R.id.image1); 
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; 
    } 
}); 

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> 

を、より多くの情報については、以下のSOの答えのリンクを参照してください。

Drag & Drop Two Images

1

Uはまた、トグルボタンを追加することができますし、トグルボタンのクリック時にuがこの

imageView1.bringToFront()/imageView2.bringToFront() 
関連する問題