2017-07-25 47 views
0

編輯:這個問題解決了!用setOnTouchListener在ViewGroup上添加imageView

我面對一個,希望,簡單的問題。我需要的是當用戶點擊圖像/區域時在onTouch位置上顯示/添加圖像。 這是佈局:

activity_test_diff.xml 

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context=".TestDiff" 
android:orientation="vertical"> 

<RelativeLayout 
    android:id="@+id/relative_layout" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1"> 

    <ImageView 
     android:id="@+id/img_rectangle" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:scaleType="fitCenter" 
     android:visibility="invisible"/> 

    <ImageView 
     android:id="@+id/img_test_diff1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:scaleType="fitCenter" /> 

    <ImageView 
     android:id="@+id/click_check_ok" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/check_green_48dp"/> 

    <ImageView 
     android:id="@+id/click_check_ko" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:visibility="gone" 
     android:src="@drawable/close_red_48dp"/> 

</RelativeLayout> 

<ImageView 
    android:id="@+id/img_test_diff2" 
    android:layout_width="match_parent" 
    android:layout_weight="1" 
    android:layout_height="0dp" 
    android:scaleType="fitCenter"/> 
</LinearLayout> 

相對佈局這是一個LinearLayout中的父組,但如果有人摸的RelativeLayout之外我不在乎。這是代碼來處理觸摸:

TestDiff.java 

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_test_diff); 

    RelativeLayout relative = (RelativeLayout) findViewById(R.id.relative_layout); 

    relative.setOnTouchListener(this); 
} 

@Override 
public boolean onTouch(View v, MotionEvent ev) { 
    final int action = ev.getAction(); 

    final int evX = (int) ev.getX(); 
    final int evY = (int) ev.getY(); 

    switch (action) { 
     case MotionEvent.ACTION_DOWN: 

      int x = (int) ev.getX(); 
      int y = (int) ev.getY(); 
      RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.WRAP_CONTENT, 
        RelativeLayout.LayoutParams.WRAP_CONTENT); 
      ImageView iv = new ImageView(getApplicationContext()); 
      lp.setMargins(x, y, 0, 0); 
      iv.setLayoutParams(lp); 
      iv.setImageDrawable(getResources().getDrawable(
        R.drawable.check_green_48dp)); 
      ((ViewGroup) v).addView(iv); 

      return true; 
     case MotionEvent.ACTION_UP: 
      // On the UP, we do the click action. 
      // The hidden image (img_rectangle) has three different hotspots on it. 
      // The colors are red, blue, and yellow. 
      // Use img_rectangle to determine which region the user 
      return true; 
     default: 
      return false; 
    } 
} 

這是即將到來的錯誤:

Attempt to invoke virtual method 'void android.widget.RelativeLayout.setOnTouchListener(android.view.View$OnTouchListener)' on a null object reference 
                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646) 

平均有人能搞清楚發生了什麼! 感謝

在XML佈局
+0

RelativeLayout的'layout_relative'is沒有。如果這是您的活動,請確保'layout_relative'存在於佈局'activity_test_diff'中。 – Abbas

+0

編輯了這個問題,這只是我的錯誤,發佈時輸入錯誤 – justo

+0

嗯,這不是我要求的。我無法告訴天氣您發佈的佈局實際上是'activity_test_diff'或其他佈局,1.因爲您尚未在頂部發布名稱,2.您說RelativeLayout是LinearLayout的子項,但我不贊成看不到任何LinearLayout環繞'layout_relative'。請參閱[如何創建最小,完整且可驗證的示例](https://stackoverflow.com/help/mcve)。 – Abbas

回答

0

您有:

<RelativeLayout 
    android:id="@+id/relative_layout" 
在Java代碼中

你有

RelativeLayout relative = (RelativeLayout) findViewById(R.id.layout_relative);

你扭轉了層次相對&佈局的話

+0

對不起,這是以前的複製和粘貼錯誤 – justo