2014-01-29 27 views
0

我基本上试图通过在多个ImageViews上滑动来编辑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

您正在检查触摸侦听器是否在您的imageview定义的矩形内记录触摸。但是,您只是在ACTION_MOVE事件中检查该事件。您可能希望用ACTION_DOWN捕捉初始触摸,ACTION_DOWN被定义在视图上。 ACTION_MOVE是当你已经触摸视图并移动你的手指

相关问题