2011-10-19 5 views
15

아래는 내 xml 파일입니다. 그것의 이름은 main.xml에Android 다중 이미지 뷰 터치로 이동


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

입니다 그리고 내 자바 파일 내가 터치에 움직임을 달성 할 수 있어요


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

좋은 질문을 사용하여 전면에 이미지 뷰를 가져올 수 있습니다 .. –

답변

1

하나 개의보기 과정 onTouch 작업입니다. 그래서 거기에 몇 가지 옵션 :

1) 반환 "false"- 그것은 당신에게

이 도움이 될 수 있습니다)는 "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> 

자신의 이미지를 사용 명심하고 적절한 변화를 내가 사용 두 이미지입니다

    을 다음과 만든
  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는이

imageView1.bringToFront()/imageView2.bringToFront() 
관련 문제