1

我想知道是否有可能通過GLSurfaceView獲取ImageView並獲取兩者的相應觸摸事件。我的問題是,我想知道何時按下ImageView(因此我會使用OnTouchListener)。但是,我仍然希望能夠獲得我的GLSurfaceView的touchEvents。觸摸事件ImageView + GLSurfaceView

我曾嘗試是從我的GLSurfaceView刪除的onTouchEvent的實施,使類實現OnTouchListener並實現onTouchsuch爲:

公共布爾onTouch(視圖V,MotionEvent事件){ INT行動=事件。的getAction();

if(v.getId() == R.id.ImageView ;){ 
    switch (action) { 
     case MotionEvent.ACTION_DOWN: 
      //Do something 
      break ; 
     //.... 
    } 
} 
else{ 
    //Process GLSurfaceView touch events 
} 
}         

當我這樣做的問題是,我不能結合撫摸我的ImageView和撫摸我的GLSurfaceView這是我真正想要達到的。因此,我嘗試添加一個檢查手指的數量,但仍然無法使其工作,因爲當我同時觸摸ImageView和GLSurfaceView時只檢測到一個手指。 現在,很難解釋,但是當我將一個手指放在imageView和另一個手指放在我的GLSurfaceView的其他位置時,不知何故我的一部分代碼在調用時,手指位置在ImageView上,而不是其他手指。

一個用例的例子是:想象一下,我在拿着平板電腦時使用我的兩隻手。如果我用左手拇指按下這個ImageView,我想從我的右拇指在屏幕上進行手指移動來執行旋轉,而不是翻譯或類似的東西。

所以,我想知道到目前爲止我的實現有什麼問題。也許我沒有正確地考慮這個問題。在這種情況下,我很樂意知道什麼是錯的,以及如何正確地實現這種事情。

由於提前,


編輯: 這是我activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
android:id= "@+id/linearlayout1" > 

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="450dp" 
    android:id= "@+id/relativeLayout" > 


     <com.kitware.VolumeRender.VolumeRenderView 
      android:id="@+id/glSurfaceViewID" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.23" /> 


     <ImageView 
      android:id="@+id/ImageView" 
      android:layout_width="60dp" 
      android:layout_height="70dp" 
      android:layout_alignParentLeft="true" 
      android:layout_centerInParent="true" 
      android:src="@drawable/i3" /> 

     <ImageView 
      android:id="@+id/ImageView2" 
      android:layout_width="40dp" 
      android:layout_height="match_parent" 
      android:layout_alignParentRight="true" 
      android:src="@drawable/tmp" /> 

</RelativeLayout>   

<Spinner 
    android:id="@+id/myspinner" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:entries="@array/country_arrays" 
    android:prompt="@string/country_prompt" /> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:orientation="horizontal" 
android:id= "@+id/linearlayout2" > 
// And so on, the end of the file is irrelevant I suppose 
+0

是這兩個視圖的視圖邊界是否一樣?還有哪些是最重要的? – Bhargav

+0

@Bhargav不太明白你的第一個問題。 ImageView處於頂端。即將編輯添加我的activity_main.xml內容 – LBes

+0

意思是ImageView完全覆蓋了表面視圖?如果這樣你的surfaceView永遠無法獲得焦點 – Bhargav

回答

0

你可以做這樣的事情

imageView.setOnFocusChangeListener(new View.OnFocusChangeListener() { 
    @Override 
    public void onFocusChange(View v, boolean hasFocus) { 
     if(hasFocus) { 
      v.getParent().requestDisallowInterceptTouchEvent(true); 
     } else { 
      v.getParent().requestDisallowInterceptTouchEvent(false); 
     } 
    } 
}); 

而且具有相同的surfaceView太

+0

不知道這是行不通的。我希望能夠同時觸摸我的ImageView和GLSurfaceView,而不是使用imageView或GLSurfaceView的二進制文件,這正是我已經擁有的。你的代碼如何改變這一點?我不確定我是否理解這段代碼的目的 – LBes

+0

是的,只要它們都是可聚焦的,就可以觸摸它們兩個,正如您從代碼中看到的那樣,當hasFocus爲false時,false將傳遞給'v.getParent()。requestDisallowInterceptTouchEvent(false)' – Bhargav

+0

剛剛嘗試過,它並未改變事件,即併發觸摸事件無法正常工作。問題是我在GLSurfaceView ProccesTouchEvent中的代碼塊被調用時,手指在imageView – LBes