如何通过拖放将图像保持在布局内 [英] How to keep an image within layout with drag and drop

查看:29
本文介绍了如何通过拖放将图像保持在布局内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 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>

这在我的 Android 手机中提供了以下图像

Which gives the following image in my Android Phone

如何编写代码以便用户可以将图像拖动到布局中的任何位置,并且 ImageView 将始终保留在布局内?

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

如果 X<0 它将是 X=(X+imageview.width())

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

如果 Y<0 它将是 Y=(Y+imageview.height())

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

如果 X>layout.width() 它将是 X=(X-imageview.width())

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

如果 Y>layout.height() 它将是 Y=(Y-imageview.height())?

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

类似的东西:

因此,如果用户拖动到视图之外,则方形图像将停留在 FrameLayout

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

我将布局背景转换为位图,以便我可以使用它来获取 X 和 Y 坐标并为我的应用转换为 RGB 值.

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();

我有以下代码可以工作,但每隔一段时间我都会收到一个错误 NPE for x must be <= layout.width()y must be <=layout.height();.有时,该应用程序允许我将布局本身拖到右侧或底部,这是不应该发生的,因为我有一个条件不允许它这样做.

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;
    }

使用下面的代码,X=0 和 Y=0 效果很好!imageview 留在里面,但是当我将 X 或 Y 拖过位图的宽度和高度时会发生问题.

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.

如果我向右拖动太多,这就是我的 LogCat:

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)

推荐答案

看起来你有很多错别字导致了你的问题.

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

改变

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

进入这个

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

也许这会解决您的问题.

Maybe that'll fix your issue.

这篇关于如何通过拖放将图像保持在布局内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆