0

我需要在用户触摸屏幕的任何位置创建图像。如果用户触摸更多的地方,图像应该显示在所有的地方。我使用了以下代码。

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

4

1 回答 1

2

您需要更新视图的位置,而不是再次添加它。

这是如何做到的:

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);
于 2012-06-25T11:14:22.417 回答