本文介绍了放置图像在Android的触摸区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要的任何地方的用户触摸屏幕上创建一个图像。如果用户触摸更多的地方,图像应在所有的地方显示。我用下面的code。
I need to create a image wherever the user touched on the screen. If the user touches more places, the image should be shown in all the places. I used the following code.
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);
}
}
和我得到了下面的错误。
And I got the following error.
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这里面code:
used this code inside case 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
在了最后一行以下异常
got the following exception at the last 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
我试过这个链接,它为我工作。的
推荐答案
您需要更新视图的位置,而不是仅仅重新添加。
You need to update the view's position, instead of just add it again.
下面是如何做到这一点:
Here's how to do it:
WindowManager windowsManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE)
WindowManager.LayoutParams windowParams = new WindowManager.LayoutParams();
windowParams.x = <new X coord>;
windowParams.y = <new Y coord>
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;
windowManager.updateViewLayout(myImageView, windowParams);
这篇关于放置图像在Android的触摸区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!