我有以下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手机中提供了以下图像
我如何编码,以便用户可以将图像拖动到布局中的任何位置,ImageVIEw将始终保持在布局中?
如果X <0则为X =(X imagevIEw.wIDth())如果Y <0则为Y =(Y imagevIEw.height())如果X> layout.wIDth(),它将是X =(X-imagevIEw.wIDth())
如果Y> layout.height()它将是Y =(Y-imagevIEw.height())?
与此类似的东西:
因此,如果用户拖动视图外,方形图像将保留在FrameLayout中的最后一个已知位置
我将布局背景转换为位图,以便我可以使用它来获取X和Y坐标并转换为我的应用程序的RGB值.
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必须是< = layout.wIDth()或y必须是< = layout.height();.有时应用程序允许我拖动布局本身的右侧或底部,这不应该发生,因为我有条件不允许它.
@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拖过位图的宽度和高度时会出现问题.
如果我向右拖动太多,这是我的LogCat:
02-09 12:34:32.791: E/AndroIDRuntime(12607): FATAL EXCEPTION: main02-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.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)
解决方法:
看起来你有这样的错字导致你的问题.
更改
if (x>0 || x<bm.getWIDth() || y>0 || y<bm.getHeight())
进入这个
if (x>0 && x<bm.getWIDth() && y>0 && y<bm.getHeight())
也许这会解决你的问题.
总结以上是内存溢出为你收集整理的java – 如何通过拖放将图像保留在布局中全部内容,希望文章能够帮你解决java – 如何通过拖放将图像保留在布局中所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)