gpt4 book ai didi

android - createBitmap --- java.lang.IllegalArgumentException : x must be < bitmap. 宽度()

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:57:18 24 4
gpt4 key购买 nike

我在截图和创建带裁剪图片的位图时出错

下面是我的代码

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

我的日志是

        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 < bitmap.getWidth 的意思是 60 < 480

虽然我得到了错误

最佳答案

不,不是x must be < bitmap.width() .它说x + width must be <= bitmap.width() .

您正在创建一个 Bitmap像这样:

Bitmap.createBitmap(source, 60, 0, 480, 260); // 320 - 60 = 260

基本上,您是从 x = 60, y = 0 中绘制的至 x = 480 + 60, y = 260Bitmap 上只有 480x320。显然,这是不可能的,因为你的 x坐标关闭Bitmap .

如果不知道您的确切用例,很难告诉您如何解决此问题。基本上,你的 source图片必须适合 { x1: x, x2: x + width, y1: y, y2: y + height } .

如果你只想从第 60 个像素开始绘制,那么你需要这样做:

Bitmap.createBitmap(source, vListView.getWidth(), 0, width - vListView.getWidth(), height - hListView.getHeight());

关于android - createBitmap --- java.lang.IllegalArgumentException : x must be < bitmap. 宽度(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13171509/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com