Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
260 views
in Technique[技术] by (71.8m points)

java - How to keep an image within layout with drag and drop

I have the following XML:

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/palette2"
    android:id="@+id/fl" >

    <ImageView
        android:id="@+id/iv"
        android:layout_width="10dp"
        android:layout_height="10dp"
        android:src="@drawable/esquare" />

</FrameLayout>

Which gives the following image in my Android Phone

enter image description here

How can I code so that user can drag the image anywhere in a layout and the ImageView will always stay within the layout?

If X<0 it will be X=(X+imageview.width())

If Y<0 it will be Y=(Y+imageview.height())

If X>layout.width() it will be X=(X-imageview.width())

If Y>layout.height() it will be Y=(Y-imageview.height())?

Something similar to this:

enter image description here

So if the user drags outside of the view, the square image will stay in the last known location within the FrameLayout

I convert the layout background to a bitmap so I can use it to get the X and Y coordinate and convert to RGB value for my app.

f = (FrameLayout) findViewById(R.id.fl);
f.setOnTouchListener(flt);
iv = (ImageView) findViewById(R.id.iv);
f.setDrawingCacheEnabled(true);
f.buildDrawingCache();
bm = f.getDrawingCache();

I have the following code which works but every once in a while I get an error NPE for x must be <= layout.width() or y must be <= layout.height();. Sometime the app allows me to drag to the right or bottom out of the layout itself which shouldn't be happening because I have a condition for it to not allow it.

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        int x = (int)event.getX();
        int y = (int)event.getY();
        if (x<0) {
            iv.setX(0);
            iv.setY(y);
            x=0;
            Toast.makeText(getApplicationContext(), "x=0", 2000).show();
        }
        if (y<0) {
            iv.setY(0);
            iv.setX(x);
            y=0;
            Toast.makeText(getApplicationContext(), "y=0", 2000).show();
        }
        if (x>bm.getWidth()) {
            x=bm.getWidth()-20;
            iv.setX(x);
            iv.setY(y);
            Toast.makeText(getApplicationContext(), "x=bitmap.maxwidth(}", 2000).show();
        }
        if (y>bm.getHeight()) {
            y=bm.getHeight()-20;
            iv.setY(y);
            iv.setX(x);
            Toast.makeText(getApplicationContext(), "y=bitmap.maxheight(}", 2000).show();
        }
        if (x>0 || x<bm.getWidth() || y>0 || y<bm.getHeight()) {
            int action = event.getAction();
            int pixel = bm.getPixel((int)x,(int) y);

            switch (action & MotionEvent.ACTION_MASK) {
                case MotionEvent.ACTION_DOWN: {
                    Log.i("COORDINATES","Touch coordinates : x" + String.valueOf(x) + "y" + String.valueOf(y));
                    iv.setX(x);
                    iv.setY(y);
                    inRed = Color.red(pixel);
                    inBlue = Color.blue(pixel);
                    inGreen = Color.green(pixel);
                    Log.d("Colors","R:" +inRed +" G:" +inGreen+" B:" + inBlue);
                    break;
                }
                case MotionEvent.ACTION_MOVE:{
                    Log.i("COORDINATES","Touch coordinates : x" + String.valueOf(x) + "y" + String.valueOf(y));
                    iv.setX(x);
                    iv.setY(y);
                    inRed = Color.red(pixel);
                    inBlue = Color.blue(pixel);
                    inGreen = Color.green(pixel);
                    Log.d("Colors","R:" +inRed +" G:" +inGreen+" B:" + inBlue);
                    break;
                }
            }
        }
        return true;
    }

With the below code, the X=0 and the Y=0 works just great! the imageview stays inside but the issue happens when I drag the X or the Y past the bitmap's width and height.

This is my LogCat if I drag too much to the right:

02-09 12:34:32.791: E/AndroidRuntime(12607): FATAL EXCEPTION: main
02-09 12:34:32.791: E/AndroidRuntime(12607): java.lang.IllegalArgumentException: x must be < bitmap.width()
02-09 12:34:32.791: E/AndroidRuntime(12607):    at android.graphics.Bitmap.checkPixelAccess(Bitmap.java:1155)
02-09 12:34:32.791: E/AndroidRuntime(12607):    at android.graphics.Bitmap.getPixel(Bitmap.java:1107)
02-09 12:34:32.791: E/AndroidRuntime(12607):    at com.example.dragdrop.MainActivity$1.onTouch(MainActivity.java:116)
02-09 12:34:32.791: E/AndroidRuntime(12607):    at android.view.View.dispatchTouchEvent(View.java:7241)
02-09 12:34:32.791: E/AndroidRuntime(12607):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2168)
02-09 12:34:32.791: E/AndroidRuntime(12607):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1903)
02-09 12:34:32.791: E/AndroidRuntime(12607):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
02-09 12:34:32.791: E/AndroidRuntime(12607):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
02-09 12:34:32.791: E/AndroidRuntime(12607):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
02-09 12:34:32.791: E/AndroidRuntime(12607):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
02-09 12:34:32.791: E/AndroidRuntime(12607):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
02-09 12:34:32.791: E/AndroidRuntime(12607):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
02-09 12:34:32.791: E/AndroidRuntime(12607):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
02-09 12:34:32.791: E/AndroidRuntime(12607):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
02-09 12:34:32.791: E/AndroidRuntime(12607):    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1953)
02-09 12:34:32.791: E/AndroidRuntime(12607):    at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1405)
02-09 12:34:32.791: E/AndroidRuntime(12607):    at android.app.Activity.dispatchTouchEvent(Activity.java:2410)
02-09 12:34:32.791: E/AndroidRuntime(12607):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1901)
02-09 12:34:32.791: E/AndroidRuntime(12607):    at android.view.View.dispatchPointerEvent(View.java:7426)
02-09 12:34:32.791: E/AndroidRuntime(12607):    at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3220)
02-09 12:34:32.791: E/AndroidRuntime(12607):    at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3165)
02-09 12:34:32.791: E/AndroidRuntime(12607):    at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4292)
02-09 12:34:32.791: E/AndroidRuntime(12607):    at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4271)
02-09 12:34:32.791: E/AndroidRuntime(12607):    at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4363)
02-09 12:34:32.791: E/AndroidRuntime(12607):    at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:179)
02-09 12:34:32.791: E/AndroidRuntime(12607):    at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
02-09 12:34:32.791: E/AndroidRuntime(12607):    at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:171)
02-09 12:34:32.791: E/AndroidRuntime(12607):    at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:4342)
02-09 12:34:32.791: E/AndroidRuntime(12607):    at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:4382)
02-09 12:34:32.791: E/AndroidRuntime(12607):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
02-09 12:34:32.791: E/AndroidRuntime(12607):    at android.view.Choreographer.doCallbacks(Choreographer.java:562)
02-09 12:34:32.791: E/AndroidRuntime(12607):    at android.view.Choreographer.doFrame(Choreographer.java:530)
02-09 12:34:32.791: E/AndroidRuntime(12607):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
02-09 12:34:32.791: E/AndroidRuntime(12607):    at android.os.Handler.handleCallback(Handler.java:725)
02-09 12:34:32.791: E/AndroidRuntime(12607):    at android.os.Handler.dispatchMessage(Handler.java:92)
02-09 12:34:32.791: E/AndroidRuntime(12607):    at android.os.Looper.loop(Looper.java:137)
02-09 12:34:32.791: E/AndroidRuntime(12607):    at android.app.ActivityThread.main(ActivityThread.java:5195)
02-09 12:34:32.791: E/AndroidRuntime(12607):    at java.lang.reflect.Method.invokeNative(Native Method)
02-09 12:34:32.791: E/AndroidRuntime(12607):    at java.lang.reflect.Method.invoke(Method.java:511)
02-09 12:34:32.791: E/AndroidRuntime(12607):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
02-09 12:34:32.791: E/AndroidRuntime(12607):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
02-09 12:34:32.791: E/AndroidRuntime(12607):    at dalvik.system.NativeStart.main(Native Method)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Looks like you have so e typos that cause your problem.

Change

if (x>0 || x<bm.getWidth() || y>0 || y<bm.getHeight())

into this

if (x>0 && x<bm.getWidth() && y>0 && y<bm.getHeight())

Maybe that'll fix your issue.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...