位图超出范围

时间:2014-12-02 18:05:55

标签: android for-loop bitmap error-handling crop

我正在尝试创建一个以3x3 / 4x4 / 5x5裁剪图像的功能,具体取决于玩家选择的难度,并将它们放在位图数组中。但是,当我运行此功能时,我收到错误:

x + width必须是< = bitmap.width()!

我试着看看我做错了什么,但我看不出来。这是代码:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Bundle extras = this.getIntent().getExtras();
    resource = extras.getInt("Image");
    difficulty = extras.getInt("difficulty");
    Bitmap bmp = BitmapFactory.decodeResource(getResources(), resource);
    divideImages(bmp);

private void divideImages(Bitmap bmp) {

    int width = bmp.getWidth();
    int height =bmp.getHeight();
    float w_ratio = (float) width/bmp.getWidth();
    int scaledwidth = (int) (bmp.getWidth()*w_ratio);
    int scaledheight = (int) (bmp.getHeight()*w_ratio);
    Bitmap scaledbmp = Bitmap.createScaledBitmap(bmp,scaledwidth,scaledheight,true);

    switch(difficulty){
        case 0:
            for(int i=0;i<3;i++){

                System.out.println("row no. "+i);

                for(int j=0;j<3;j++){

                    System.out.println("Column no."+j);
                    int startx = (scaledwidth/3)*j;
                    int starty = (scaledheight/3)*i;
                    Bitmap b1  = Bitmap.createBitmap(scaledbmp,startx,starty,scaledwidth,scaledheight);
                    bs.add(b1);
                    b1 = null;
                    }
                bmp = null;
            }
            break;                                
        case 1:
            for(int i=0;i<4;i++){

                System.out.println("row no. "+i);

                for(int j=0;j<4;j++){

                    System.out.println("Column no."+j);
                    int startx = (scaledwidth/4)*j;
                    int starty = (scaledheight/4)*i;
                    Bitmap b1  = Bitmap.createBitmap(bmp,startx,starty,scaledwidth,scaledheight);
                    bs.add(b1);
                    b1 = null;
                    }
                bmp = null;
            }
        case 2:
            for(int i=0;i<5;i++){

                System.out.println("row no. "+i);

                for(int j=0;j<5;j++){

                    System.out.println("Column no."+j);
                    int startx = (scaledwidth/5)*j;
                    int starty = (scaledheight/5)*i;
                    Bitmap b1  = Bitmap.createBitmap(bmp,startx,starty,scaledwidth,scaledheight);
                    bs.add(b1);
                    b1 = null;
                    }
                bmp = null;
            }
    }
}

我认为Bitmap b1超出边界但我似乎没有看到为什么.....

0 个答案:

没有答案