java.lang.IllegalArgumentException:y + height必须是< = bitmap.height()

时间:2016-07-12 09:26:26

标签: android bitmap

我正在尝试构建这个应用程序,我需要将给定的位图图像分成更小的块,为此,我使用以下代码片段。

PuzzleBoard(Bitmap bitmap, int parentWidth) {
    tiles=new ArrayList<>();
    Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, parentWidth, parentWidth, true);
    int chunkSize=parentWidth/NUM_TILES;
    int cnt=0;
    for(int i=0;i<NUM_TILES;i++){
        for(int j=0;j<NUM_TILES;i++){
             if((cnt)<NUM_TILES*NUM_TILES-1){
                   Bitmap tile= Bitmap.createBitmap(scaledBitmap,j*chunkSize,i*chunkSize,chunkSize,chunkSize);
                   PuzzleTile t=new PuzzleTile(tile,cnt);
                   tiles.add(t);
             }
             cnt++;
        }
    }
}

但是,当我在设备上运行代码时,我的应用程序崩溃了。当我看到logcat时,我得到了这个

07-12 14:33:36.747 21428-21428/com.google.engedu.puzzle8 E/AndroidRuntime: FATAL EXCEPTION: main
   Process: com.google.engedu.puzzle8, PID: 21428
   java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.google.engedu.puzzle8/com.google.engedu.puzzle8.PuzzleActivity}: java.lang.IllegalArgumentException: y + height must be <= bitmap.height()
       at android.app.ActivityThread.deliverResults(ActivityThread.java:3365)
       at android.app.ActivityThread.handleSendResult(ActivityThread.java:3408)
       at android.app.ActivityThread.access$1300(ActivityThread.java:135)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:136)
       at android.app.ActivityThread.main(ActivityThread.java:5017)
       at java.lang.reflect.Method.invokeNative(Native Method)
       at java.lang.reflect.Method.invoke(Method.java:515)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
       at dalvik.system.NativeStart.main(Native Method)
    Caused by: java.lang.IllegalArgumentException: y + height must be <= bitmap.height()
       at android.graphics.Bitmap.createBitmap(Bitmap.java:668)
       at android.graphics.Bitmap.createBitmap(Bitmap.java:632)
       at com.google.engedu.puzzle8.PuzzleBoard.<init>(PuzzleBoard.java:51)
       at com.google.engedu.puzzle8.PuzzleBoardView.initialize(PuzzleBoardView.java:29)
       at com.google.engedu.puzzle8.PuzzleActivity.onActivityResult(PuzzleActivity.java:68)
       at android.app.Activity.dispatchActivityResult(Activity.java:5430)
       at android.app.ActivityThread.deliverResults(ActivityThread.java:3361)
       at android.app.ActivityThread.handleSendResult(ActivityThread.java:3408) 
       at android.app.ActivityThread.access$1300(ActivityThread.java:135) 
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244) 
       at android.os.Handler.dispatchMessage(Handler.java:102) 
       at android.os.Looper.loop(Looper.java:136) 
       at android.app.ActivityThread.main(ActivityThread.java:5017) 
       at java.lang.reflect.Method.invokeNative(Native Method) 
       at java.lang.reflect.Method.invoke(Method.java:515) 
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
       at dalvik.system.NativeStart.main(Native Method) 

尝试了一下解决问题之后,我将Bitmap.createScaledBitmap()的filter参数设置为&#34; false&#34;然后我的应用程序运行正常。但是,我并不了解它背后的逻辑。有人能解释我为什么会这样吗?

编辑:    parentWidth值为432.构造函数从另一个类方法initialize()

中调用
public void initialize(Bitmap imageBitmap) {
    int width = getWidth();
    puzzleBoard = new PuzzleBoard(imageBitmap, width);
}

1 个答案:

答案 0 :(得分:0)

你有一个错字:

for(int i=0;i<NUM_TILES;i++){
    for(int j=0;j<NUM_TILES;i++){

用j ++替换第二个i ++

btw:我和j是非常糟糕的变量名称 - 更好的是x和y - 然后你可能已经看到了这个你自己......