ユーザーが画面に触れた場所に画像を作成する必要があります。ユーザーがより多くの場所に触れると、すべての場所に画像が表示されます。次のコードを使用しました。
package com.tag.image;
import android.app.Activity;
import android.graphics.Canvas;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
public class TagsOverImage extends Activity {
/** Called when the activity is first created. */
Canvas canvas ;
LinearLayout ll;
ImageView myImageView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ll=(LinearLayout)findViewById(R.id.lay_lin);
myImageView= new ImageView(this);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
//ll.removeView(myImageView);
int x = (int)event.getX();
int y = (int)event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
myImageView.setImageResource(R.drawable.sample);
String text = "You click at x = " + event.getX() + " and y = " + event.getY();
Toast.makeText(this, text, Toast.LENGTH_LONG).show();
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(50, 50);
params.setMargins(x,y, 0, 0);
myImageView.setLayoutParams(params);
ll.addView(myImageView);
}
return super.onTouchEvent(event);
}
}
そして、次のエラーが発生しました。
06-25 15:43:44.180: E/AndroidRuntime(2586): Uncaught handler: thread main exiting due to uncaught exception
06-25 15:43:44.190: E/AndroidRuntime(2586): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
06-25 15:43:44.190: E/AndroidRuntime(2586): at android.view.ViewGroup.addViewInner(ViewGroup.java:1946)
06-25 15:43:44.190: E/AndroidRuntime(2586): at android.view.ViewGroup.addView(ViewGroup.java:1841)
06-25 15:43:44.190: E/AndroidRuntime(2586): at android.view.ViewGroup.addView(ViewGroup.java:1798)
06-25 15:43:44.190: E/AndroidRuntime(2586): at android.view.ViewGroup.addView(ViewGroup.java:1778)
06-25 15:43:44.190: E/AndroidRuntime(2586): at com.tag.image.TagsOverImage.onTouchEvent(TagsOverImage.java:41)
06-25 15:43:44.190: E/AndroidRuntime(2586): at android.app.Activity.dispatchTouchEvent(Activity.java:2067)
06-25 15:43:44.190: E/AndroidRuntime(2586): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
06-25 15:43:44.190: E/AndroidRuntime(2586): at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
06-25 15:43:44.190: E/AndroidRuntime(2586): at android.os.Handler.dispatchMessage(Handler.java:99)
06-25 15:43:44.190: E/AndroidRuntime(2586): at android.os.Looper.loop(Looper.java:123)
06-25 15:43:44.190: E/AndroidRuntime(2586): at android.app.ActivityThread.main(ActivityThread.java:4370)
06-25 15:43:44.190: E/AndroidRuntime(2586): at java.lang.reflect.Method.invokeNative(Native Method)
06-25 15:43:44.190: E/AndroidRuntime(2586): at java.lang.reflect.Method.invoke(Method.java:521)
06-25 15:43:44.190: E/AndroidRuntime(2586): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-25 15:43:44.190: E/AndroidRuntime(2586): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-25 15:43:44.190: E/AndroidRuntime(2586): at dalvik.system.NativeStart.main(Native Method)
06-25 15:43:44.200: E/SemcCheckin(2586): Get crash dump level : java.io.FileNotFoundException: /data/semc-checkin/crashdump
アップデート:
ケース MotionEvent.ACTION_DOWN 内でこのコードを使用しました:
myImageView.setImageResource(R.drawable.sample);
WindowManager windowsManager = (WindowManager) TagsOverImage.this.getSystemService(Context.WINDOW_SERVICE);
WindowManager.LayoutParams windowParams = new WindowManager.LayoutParams();
windowParams.x = (int) event.getX();
windowParams.y = (int) event.getY();
windowParams.height = myImageView.getHeight();
windowParams.width = myImageView.getWidth();
windowParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
| WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;
windowParams.format = PixelFormat.TRANSLUCENT;
windowParams.windowAnimations = 0;
windowsManager.updateViewLayout(myImageView, windowParams); //error at this line
最後の行で次の例外を受け取りました
06-25 17:10:05.320: E/AndroidRuntime(3511): Uncaught handler: thread main exiting due to uncaught exception
06-25 17:10:05.330: E/AndroidRuntime(3511): java.lang.IllegalArgumentException: View not attached to window manager
06-25 17:10:05.330: E/AndroidRuntime(3511): at android.view.WindowManagerImpl.findViewLocked(WindowManagerImpl.java:355)
06-25 17:10:05.330: E/AndroidRuntime(3511): at android.view.WindowManagerImpl.updateViewLayout(WindowManagerImpl.java:191)
06-25 17:10:05.330: E/AndroidRuntime(3511): at android.view.Window$LocalWindowManager.updateViewLayout(Window.java:428)
06-25 17:10:05.330: E/AndroidRuntime(3511): at com.tag.image.TagsOverImage.onTouchEvent(TagsOverImage.java:63)
06-25 17:10:05.330: E/AndroidRuntime(3511): at android.app.Activity.dispatchTouchEvent(Activity.java:2067)
06-25 17:10:05.330: E/AndroidRuntime(3511): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
06-25 17:10:05.330: E/AndroidRuntime(3511): at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
06-25 17:10:05.330: E/AndroidRuntime(3511): at android.os.Handler.dispatchMessage(Handler.java:99)
06-25 17:10:05.330: E/AndroidRuntime(3511): at android.os.Looper.loop(Looper.java:123)
06-25 17:10:05.330: E/AndroidRuntime(3511): at android.app.ActivityThread.main(ActivityThread.java:4370)
06-25 17:10:05.330: E/AndroidRuntime(3511): at java.lang.reflect.Method.invokeNative(Native Method)
06-25 17:10:05.330: E/AndroidRuntime(3511): at java.lang.reflect.Method.invoke(Method.java:521)
06-25 17:10:05.330: E/AndroidRuntime(3511): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-25 17:10:05.330: E/AndroidRuntime(3511): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-25 17:10:05.330: E/AndroidRuntime(3511): at dalvik.system.NativeStart.main(Native Method)
06-25 17:10:05.340: E/SemcCheckin(3511): Get crash dump level : java.io.FileNotFoundException: /data/semc-checkin/crashdump
このリンクを試してみましたが、うまくいきました。http://eleanordarephdjournal.blogspot.in/2011/03/simplest-possible-android-graphic.html