【问题标题】:createBitmap --- java.lang.IllegalArgumentException: x must be < bitmap.width()createBitmap --- java.lang.IllegalArgumentException: x must be < bitmap.width()
【发布时间】:2019-12-16 17:13:05
【问题描述】:

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

下面是我的代码

    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 是

        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 = Bitmap.createBitmap(source, vListView.getWidth(), 0, width, height - hListView.getHeight());
  • 如果我做 bitmap = Bitmap.createBitmap(source, 0, 0, width, height - hListView.getHeight());它会工作
  • ok @Siddhpura 你的问题是你的 vListView.getWidth() 给出的宽度大于你的位图的宽度,所以它会抛出错误。明白我的意思。

标签: android android-layout


【解决方案1】:

不,不是x must be &lt; bitmap.width()。上面写着x + width must be &lt;= bitmap.width()

您正在创建一个Bitmap,如下所示:

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

基本上,您是在只有 480x320 的 Bitmap 上从 x = 60, y = 0x = 480 + 60, y = 260 绘制的。显然,这是不可能的,因为您的 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());

【讨论】:

  • 非常感谢 Eric,你能告诉我你是怎么得到这个的吗?我已经阅读了文档,但找不到这个! Eric 我想从左侧裁剪图像!请你告诉我该怎么做?
【解决方案2】:
Bitmap resultBitmap = Bitmap.createBitmap(sourceBitmap, 0, 0,
        sourceBitmap.getWidth() - 1, sourceBitmap.getHeight() - 1);

和列表一样,它从 0 开始,所以宽度必须是位图宽度 - 1。

【讨论】:

    【解决方案3】:

    如果你是从 drawable 中获取图像,则将其添加到 drawable-nodpi 文件夹中。

    我有同样的问题。现在,工作正常。

    【讨论】:

      【解决方案4】:

      如果你得到 java.lang.IllegalArgumentException: x 必须是

      这是三星 6.0.1 版本的系统崩溃。目前我找不到原因。

      【讨论】:

        猜你喜欢
        • 2016-05-07
        • 1970-01-01
        • 2015-01-26
        • 1970-01-01
        • 1970-01-01
        • 2019-02-07
        • 2014-10-25
        • 2020-06-14
        • 1970-01-01
        相关资源
        最近更新 更多