屏幕太大时出错

时间:2017-03-21 08:34:52

标签: java android bitmap illegalargumentexception

我最近向Google Play商店上传了app。它适用于某些设备,但不适用于像nexus 7这样的大设备。我得到以下堆栈跟踪:

java.lang.IllegalArgumentException: x + width must be <= bitmap.width()
    at android.graphics.Bitmap.createBitmap(Bitmap.java:667)
    at android.graphics.Bitmap.createBitmap(Bitmap.java:634)
    at de.krissini.server.Player.<init>(Player.java:26)
    at de.krissini.server.GamePanel.surfaceCreated(GamePanel.java:64)

这是提到的代码。的GamePanel:

@Override
    public void surfaceCreated(SurfaceHolder holder) {
        setWillNotDraw(false);
        score = 0;
        fuel = 50;
        bg = new Background(BitmapFactory.decodeResource(getResources(), R.raw.grassbg1), 1000);
        player = new Player(BitmapFactory.decodeResource(getResources(), R.raw.helicopter), 132, 47, 3);  // line 64
        scoreStartTime = System.nanoTime();
        smoke = new ArrayList<Smokepuff>();
        smokestartTime = System.nanoTime();
        missles = new ArrayList<Missle>();
        missleStartTime = System.nanoTime();
        thread = new MainThread(getHolder(), this);

        // start game loop
        thread.setRunning(true);
        thread.start();
    }

播放器:

public Player(Bitmap res, int w, int h, int numFrames) {
        x = 100;
        y = GamePanel.HEIGHT / 2;
        dy = 0;
        score = 0;
        height = h;
        width = w;
        spritesheet = res;
        Bitmap[] image = new Bitmap[numFrames];
        for(int i = 0; i<image.length; i++){
            image[i] = Bitmap.createBitmap(spritesheet, i*width, 0, width, height); // line 62
        }

        animation.setFrames(image);
        animation.setDelay(10);
        startTime = System.nanoTime();
    }

我该怎么做才能摆脱这个错误?我很感激每一个答案!

2 个答案:

答案 0 :(得分:1)

Bitmap.createBitmap(spritesheet, i*width, 0, width, height);

对此幻数 132,47

无效

x + width must be <= bitmap.width()

以来

答案 1 :(得分:0)

这并不完美,但我改变了这一点。的工作原理。

Bitmap[] image = new Bitmap[numFrames];
        try{
        for(int i = 0; i<image.length; i++){
            image[i] = Bitmap.createBitmap(spritesheet, i*width, 0, width, height);
        }
        }catch(Exception e){
            image = new Bitmap[1];
            image[0] = Bitmap.createBitmap(spritesheet, 0, 0, 63, height);
        }

但为什么它只适用于某些设备呢?