11

I am getting error while taking screenshot and create bitmap with cropping picture

below is my 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());

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)

Here you can see that x < bitmap.getWidth mean 60 < 480

although i am getting error

4

4 に答える 4

24

いいえ、違いx must be < bitmap.width()ます。それは言いx + width must be <= bitmap.width()ます。

あなたは次のBitmapようなものを作成しています:

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

基本的に、480x320 しかないからx = 60, y = 0に描画しx = 480 + 60, y = 260ています。明らかに、座標が から外れているBitmapため、これは不可能です。xBitmap

正確なユースケースを知らずに、これを修正する方法を説明するのは困難です。基本的に、source画像は に収まる必要があります{ x1: x, x2: x + width, y1: y, y2: y + height }

60 ピクセル目以降のみを描画する場合は、次のようにする必要があります。

Bitmap.createBitmap(source, vListView.getWidth(), 0, width - vListView.getWidth(), height - hListView.getHeight());
于 2012-11-01T05:40:19.680 に答える
1
Bitmap resultBitmap = Bitmap.createBitmap(sourceBitmap, 0, 0,
        sourceBitmap.getWidth() - 1, sourceBitmap.getHeight() - 1);

リストと同様に、0 から始まるため、幅はビットマップ幅 - 1 である必要があります。

于 2013-09-09T09:29:28.147 に答える