createBitmap --- java.lang.IllegalArgumentException:如果x必须&LT; bitmap.width() [英] createBitmap --- java.lang.IllegalArgumentException: x must be < bitmap.width()
问题描述
我收到错误而采取的截图并创建位图裁剪图片
I am getting error while taking screenshot and create bitmap with cropping picture
下面是我的code
View v1 = mKittyBGLayer.getRootView();
v1.setDrawingCacheEnabled(true);
Bitmap source = v1.getDrawingCache();
int width = source.getWidth();
int height = source.getHeight();
System.out.println("vListView : -"+vListView.getWidth());
System.out.println("hListView : -"+hListView.getHeight());
System.out.println("Width : -"+width);
System.out.println("Height : -"+height);
bitmap = Bitmap.createBitmap(source, vListView.getWidth(), 0, width, height - hListView.getHeight());
我logcat的是
my logcat is
11-01 11:00:31.419: I/System.out(1658): vListView :- 60
11-01 11:00:31.429: I/System.out(1658): hListView :- 60
11-01 11:00:31.429: I/System.out(1658): Width :- 480
11-01 11:00:31.429: I/System.out(1658): Height :- 320
11-01 11:00:31.429: D/AndroidRuntime(1658): Shutting down VM
11-01 11:00:31.429: W/dalvikvm(1658): threadid=1: thread exiting with uncaught exception (group=0x40018560)
11-01 11:00:31.429: E/AndroidRuntime(1658): FATAL EXCEPTION: main
11-01 11:00:31.429: E/AndroidRuntime(1658): java.lang.IllegalArgumentException: x + width must be <= bitmap.width()
11-01 11:00:31.429: E/AndroidRuntime(1658): at android.graphics.Bitmap.createBitmap(Bitmap.java:410)
11-01 11:00:31.429: E/AndroidRuntime(1658): at android.graphics.Bitmap.createBitmap(Bitmap.java:383)
11-01 11:00:31.429: E/AndroidRuntime(1658): at com.appsehs.android.CUTECRAZYKITTENDRESSUPGAME.PhotoSortrActivity.takeScreenShot(PhotoSortrActivity.java:247)
11-01 11:00:31.429: E/AndroidRuntime(1658): at com.appsehs.android.CUTECRAZYKITTENDRESSUPGAME.PhotoSortrActivity.onOptionsItemSelected(PhotoSortrActivity.java:274)
11-01 11:00:31.429: E/AndroidRuntime(1658): at android.app.Activity.onMenuItemSelected(Activity.java:2205)
在这里你可以看到是x&LT; bitmap.getWidth平均60℃; 480
Here you can see that x < bitmap.getWidth mean 60 < 480
虽然我收到错误
推荐答案
没有,不是 x必须&LT; bitmap.width()
。它说 X +宽度必须与LT; = bitmap.width()
您正在创建一个位图
像这样:
You are creating a Bitmap
like so:
Bitmap.createBitmap(source, 60, 0, 480, 260); // 320 - 60 = 260
基本上,你是从绘图X = 60,Y = 0
到 X = 480 + 60,Y = 260
在位图
这是只有小480x320。很显然,这是不可能的,因为你的 X
坐标离开位图
。
Basically, you are drawing from x = 60, y = 0
to x = 480 + 60, y = 260
on a Bitmap
which is only 480x320. Obviously, this is not possible, since your x
coordinate is off the Bitmap
.
这是很难告诉你如何解决这个问题不知道您的具体使用案例。基本上,你的源
图像必须适合在 {X1:X,X2:X +宽度,Y1:Y,Y2:Y +高}
。
It's hard to tell you how to fix this without knowing your exact use case. Basically, your source
image has to fit within { x1: x, x2: x + width, y1: y, y2: y + height }
.
如果您只想从60像素向前画,那你需要这样做:
If you only want to draw from the 60th pixel onward, then you need to do this:
Bitmap.createBitmap(source, vListView.getWidth(), 0, width - vListView.getWidth(), height - hListView.getHeight());
这篇关于createBitmap --- java.lang.IllegalArgumentException:如果x必须&LT; bitmap.width()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!