編集:この問題は解決しました。setOnTouchListenerを使用してViewGroupにimageViewを追加する
私は、うまくいけば、簡単な問題に直面しています。ユーザーが画像/エリアをクリックしたときにオンタッチ位置に画像を表示/追加する必要がある。 これはレイアウトです:
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レイアウト上 おかげ
RelativeLayoutを '逆転は階層ではありませんlayout_relative'is。これがあなたの活動であれば、 'layout_relative'が' activity_test_diff'レイアウト内に存在することを確認してください。 – Abbas
質問を編集した、それはちょうど私の間違いです、投稿時にタイプエラーがあります – justo
これは私が求めたものではありません。あなたが掲示したレイアウトが実際には 'activity_test_diff'か他のレイアウトであることを天気に伝える方法はありません。1.あなたがトップに名前を投稿していないので、2. RelativeLayoutはLinearLayoutの子ですが、 'layout_relative'をラップするLinearLayoutはありません。 [最小で完全で検証可能な例を作成する方法](https://stackoverflow.com/help/mcve)を参照してください。 – Abbas