我想以imageview的边距(10,50,10,50),(left,top,right,bottom)分别裁剪图像。这是我的代码
Bitmap bitmap = Bitmap.createBitmap(imgView.getWidth(),imgView.getHeight(), Bitmap.Config.ARGB_8888);
Bitmap result = Bitmap.createBitmap(bitmap, (imgView.getLeft() + 10),
imgView.getTop() + 50,imgView.getRight() - 10,imgView.getBottom() - 50);
bitmap.recycle();
Canvas canvas = new Canvas(result);
imgView.draw(canvas);
即,如果我的imageview的尺寸为200X300。那么我只想在指定区域中获取位图图像。当我尝试执行此操作时,它将显示错误
FATAL EXCEPTION: main
java.lang.IllegalArgumentException: x + width must be <= bitmap.width()
at android.graphics.Bitmap.createBitmap(Bitmap.java:410)
at android.graphics.Bitmap.createBitmap(Bitmap.java:383)
at android.view.View.performClick(View.java:2485)
at android.view.View$PerformClick.run(View.java:9080)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
at dalvik.system.NativeStart.main(Native Method)
我该如何解决?
谢谢
最佳答案
试试看,看看是否可行:
// using this method createBitmap(source, x, y, width, height)
Bitmap result = Bitmap.createBitmap(bitmap, 10,
50, imgView.getWidth() - 20, imgView.getHeight() - 100);
getLeft
和其他用于获取视图在其父视图中的位置。不是位图的尺寸。关于android - IllegalArgumentException:X +宽度必须在Android中为<= bitmap.width(),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12852968/