用setOnTouchListener在ViewGroup上添加imageView

问题描述:

编辑:这个问题解决了!用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://*.com/help/mcve)。 – Abbas

您有:

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

你有

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

你扭转了层次相对&布局的话

+0

对不起,这是以前的复制和粘贴错误 – justo