创建缩放的Bitmap时会出现错误:java.lang.IllegalArgumentException:width和height必须是> 0

时间:2017-05-26 06:06:19

标签: android

我是Android的新手,我正在写一个简单的游戏 - Tic tac toe game。当我尝试使用onSizechanged()的宽度和高度创建图像的缩放位图并在我的设备上测试时,应用程序停止工作。这是我的TitleView:

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;

public class TitleView extends View {

private Bitmap title;
private Bitmap playButton;
private Bitmap background;
private int screenW;
private int screenH;
private int scale;
private Paint text;

public TitleView(Context context){
    super(context);
    Context mycontext = context;
    title = BitmapFactory.decodeResource(mycontext.getResources(), R.drawable.title);
    playButton = BitmapFactory.decodeResource(mycontext.getResources(), R.drawable.button_normal);
    background = BitmapFactory.decodeResource(mycontext.getResources(), R.drawable.bg);
    scale = (int)mycontext.getResources().getDisplayMetrics().density;
    title = Bitmap.createScaledBitmap(title, screenW, screenW, false);
    playButton = Bitmap.createScaledBitmap(playButton, 100, 25, false);
    background = Bitmap.createScaledBitmap(background, 1080, 1920, false );
}

public void onSizeChanged(int w, int h, int oldw, int oldh){
    super.onSizeChanged(w, h, oldw, oldh);
    screenW = w;
    screenH = h;
}

public void onDraw(Canvas canvas){
    canvas.drawBitmap(background, 0, 0, null);
    canvas.drawBitmap(title, screenW/2-title.getWidth()/2, 10, null);
}
}

这是错误:

05-26 12:33:38.049 17441-17441/com.game.neitstudio.tictactoe E/AndroidRuntime: FATAL EXCEPTION: main
                                                                           Process: com.game.neitstudio.tictactoe, PID: 17441
                                                                           java.lang.RuntimeException: Unable to start activity ComponentInfo{com.game.neitstudio.tictactoe/com.game.neitstudio.tictactoe.MainActivity}: java.lang.IllegalArgumentException: width and height must be > 0
                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3320)
                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3416)
                                                                               at android.app.ActivityThread.access$1100(ActivityThread.java:230)
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1822)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                               at android.os.Looper.loop(Looper.java:148)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:7409)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
                                                                            Caused by: java.lang.IllegalArgumentException: width and height must be > 0
                                                                               at android.graphics.Bitmap.createBitmap(Bitmap.java:967)
                                                                               at android.graphics.Bitmap.createBitmap(Bitmap.java:946)
                                                                               at android.graphics.Bitmap.createBitmap(Bitmap.java:877)
                                                                               at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:753)
                                                                               at com.game.neitstudio.tictactoe.TitleView.<init>(TitleView.java:28)
                                                                               at com.game.neitstudio.tictactoe.MainActivity.onCreate(MainActivity.java:13)
                                                                               at android.app.Activity.performCreate(Activity.java:6904)
                                                                               at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)
                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3267)
                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3416) 
                                                                               at android.app.ActivityThread.access$1100(ActivityThread.java:230) 
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1822) 
                                                                               at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                               at android.os.Looper.loop(Looper.java:148) 
                                                                               at android.app.ActivityThread.main(ActivityThread.java:7409) 
                                                                               at java.lang.reflect.Method.invoke(Native Method) 
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

在这一行:

title = Bitmap.createScaledBitmap(title, screenW, screenW, false);

我更换了两个变量&screen;&#39; screenW&#39;有两个整数,它工作。我尝试在屏幕上方打印两个变量,它们是:&#39; 1080&#39;和&#39; 1920&#39;。它怎么可能是零?我已经阅读了一些关于这个的问题,但没有说明onSizechanged()。请为我解释一下。

1 个答案:

答案 0 :(得分:0)

  

在此视图的大小发生更改时,在布局期间调用View.onSizeChanged()。如果您刚刚添加到视图层次结构中,则使用旧值0调用。

你不应该在那里初始化大小。在您创建视图时, screenW screenH 都会初始化为值0.首先尝试查看它是否有效。

变化:

private int screenW;
private int screenH;


public void onSizeChanged(int w, int h, int oldw, int oldh){
    super.onSizeChanged(w, h, oldw, oldh);
    screenW = w;
    screenH = h;
}

要:

private int screenW = 100;
private int screenH = 100;


public void onSizeChanged(int w, int h, int oldw, int oldh){
    super.onSizeChanged(w, h, oldw, oldh);
}

或者你可以完全删除方法 onSizeChanged()