createBitmap --- java.lang.IllegalArgumentException:x必须是<bitmap.width()

Sid*_*mit 10 android android-layout

我在拍摄截图时遇到错误并使用裁剪图片创建位图

下面是我的代码

    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());
Run Code Online (Sandbox Code Playgroud)

我的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)
Run Code Online (Sandbox Code Playgroud)

在这里你可以看到x <bitmap.getWidth意味着60 <480

虽然我收到了错误

Eri*_*ric 21

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

你是这样创造的Bitmap:

Bitmap.createBitmap(source, 60, 0, 480, 260); // 320 - 60 = 260
Run Code Online (Sandbox Code Playgroud)

基本上,你是从图纸x = 60, y = 0x = 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());
Run Code Online (Sandbox Code Playgroud)