2014-01-29 5 views
0

저는 기본적으로 여러 ImageView에서 스 와이프하여 editText의 내용을 편집하려고합니다. 여기 내 코드이지만 아무 것도 작동하지 않는 것 같습니다.onToch 이벤트 절대 위치를 얻고이를 imageView 좌표와 비교하는 방법은 무엇입니까?

package com.example.touchdemo; 

import android.os.Bundle; 
import android.app.Activity; 
import android.graphics.Rect; 
import android.view.Menu; 
import android.view.View; 
import android.widget.ImageView; 
import android.widget.TextView; 
import android.view.View.OnTouchListener; 
import android.view.MotionEvent; 
import android.widget.RelativeLayout; 

public class MainActivity extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    RelativeLayout relLay = (RelativeLayout) findViewById(R.id.RelativeLayout1); 
    final TextView txtv = (TextView) findViewById(R.id.textView1); 
    final ImageView myImageView = (ImageView) findViewById(R.id.imageView1); 
    final ImageView myImageView2 = (ImageView) findViewById(R.id.imageView2); 

    myImageView.setOnTouchListener(new OnTouchListener() 
    { 

     @Override 
     public boolean onTouch(View v, MotionEvent event) 
     { 
      int x,y; 

       if(event.getAction() == MotionEvent.ACTION_MOVE){ 

        Rect rect = new Rect(); 
         rect.left = myImageView.getLeft(); 
         rect.top = myImageView.getTop(); 
         rect.bottom = myImageView.getBottom(); 
         rect.right = myImageView.getRight(); 
        Rect rect2 = new Rect(); 
         rect2.left = myImageView2.getLeft(); 
         rect2.top = myImageView2.getTop(); 
         rect2.bottom = myImageView2.getBottom(); 
         rect2.right = myImageView2.getRight(); 
        x = (int) event.getRawX(); 
        y = (int) event.getRawX(); 
        if(rect.contains(x, y)) { 
         txtv.append("Ti ho trovato 1"); 
        } 
        else if(rect2.contains(x, y)) { 
          txtv.append("Ti ho trovato 2"); 
        } 

       } 
      // txtv.append("ciao"); 

        return false; 
     } 

    }); 
} 



@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

} 

그리고 여기 내 XML입니다 :

<ImageView 
    android:id="@+id/imageView2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignTop="@+id/imageView1" 
    android:layout_marginLeft="86dp" 
    android:layout_toRightOf="@+id/imageView1" 
    android:src="@drawable/ic_launcher" /> 

<ImageView 
    android:id="@+id/imageView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:layout_marginLeft="37dp" 
    android:layout_marginTop="94dp" 
    android:src="@drawable/ic_launcher" /> 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true" 
    android:layout_marginBottom="124dp" 
    android:text="TextView" /> 

나는 가장 좋은 방법은 그들이에 포함되어있는 경우 전체 화면 크기와 검사에 따라 좌표 이벤트를 얻을 수 있다고 생각 내 imageViews를 나타내는 사각형. 또한 코드에서 onTouch 이벤트가 지속적으로 호출되는지 확인하고 싶습니다. 나는 어떤 제안에도 감사 할 것입니다.

답변

0

터치 리스너가 이미지 뷰로 정의 된 rect 내에 터치를 기록하는지 확인해야합니다. 그러나 ACTION_MOVE 이벤트에있을 때만 확인하고 있습니다. 뷰에 정의 된 ACTION_DOWN으로 초기 터치를 잡아 내고 싶을 것입니다. ACTION_MOVE는 이미 뷰를 터치하고 손가락을 움직일 때입니다.