我正在尝试在画布中绘制位图,但在 logact 中出现错误。日志是这样的:
09-22 16:41:02.081: D/dalvikvm(26969): Late-enabling CheckJNI
09-22 16:41:02.191: D/ActivityThread(26969): setTargetHeapUtilization:0.25
09-22 16:41:02.191: D/ActivityThread(26969): setTargetHeapIdealFree:8388608
09-22 16:41:02.191: D/ActivityThread(26969): setTargetHeapConcurrentStart:2097152
09-22 16:41:03.141: D/AndroidRuntime(26969): Shutting down VM
09-22 16:41:03.141: W/dalvikvm(26969): threadid=1: thread exiting with uncaught exception (group=0x41bb5438)
09-22 16:41:03.171: E/AndroidRuntime(26969): FATAL EXCEPTION: main
09-22 16:41:03.171: E/AndroidRuntime(26969): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.prova1/com.example.prova1.MoveBalloon}: java.lang.IllegalArgumentException: width and height must be > 0
09-22 16:41:03.171: E/AndroidRuntime(26969): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2114)
09-22 16:41:03.171: E/AndroidRuntime(26969): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2139)
09-22 16:41:03.171: E/AndroidRuntime(26969): at android.app.ActivityThread.access$700(ActivityThread.java:143)
09-22 16:41:03.171: E/AndroidRuntime(26969): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
09-22 16:41:03.171: E/AndroidRuntime(26969): at android.os.Handler.dispatchMessage(Handler.java:99)
09-22 16:41:03.171: E/AndroidRuntime(26969): at android.os.Looper.loop(Looper.java:137)
09-22 16:41:03.171: E/AndroidRuntime(26969): at android.app.ActivityThread.main(ActivityThread.java:4963)
09-22 16:41:03.171: E/AndroidRuntime(26969): at java.lang.reflect.Method.invokeNative(Native Method)
09-22 16:41:03.171: E/AndroidRuntime(26969): at java.lang.reflect.Method.invoke(Method.java:511)
09-22 16:41:03.171: E/AndroidRuntime(26969): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
09-22 16:41:03.171: E/AndroidRuntime(26969): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
09-22 16:41:03.171: E/AndroidRuntime(26969): at dalvik.system.NativeStart.main(Native Method)
09-22 16:41:03.171: E/AndroidRuntime(26969): Caused by: java.lang.IllegalArgumentException: width and height must be > 0
09-22 16:41:03.171: E/AndroidRuntime(26969): at android.graphics.Bitmap.createBitmap(Bitmap.java:667)
09-22 16:41:03.171: E/AndroidRuntime(26969): at android.graphics.Bitmap.createBitmap(Bitmap.java:615)
09-22 16:41:03.171: E/AndroidRuntime(26969): at com.example.prova1.MoveBalloon.getResizedBitmap(MoveBalloon.java:158)
09-22 16:41:03.171: E/AndroidRuntime(26969): at com.example.prova1.MoveBalloon.onCreate(MoveBalloon.java:99)
09-22 16:41:03.171: E/AndroidRuntime(26969): at android.app.Activity.performCreate(Activity.java:5184)
09-22 16:41:03.171: E/AndroidRuntime(26969): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
09-22 16:41:03.171: E/AndroidRuntime(26969): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2078)
09-22 16:41:03.171: E/AndroidRuntime(26969): ... 11 more
09-22 16:41:16.471: I/Process(26969): Sending signal. PID: 26969 SIG: 9
发生错误的类如下:
package com.example.prova1;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Point;
import android.hardware.SensorManager;
import android.os.Build;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.WindowManager;
public class MoveBalloon extends Activity {
Bitmap balloon;
Bitmap background;
Bitmap _background;
DrawBalloon myView;
float x,y,sensorX, sensorY;
SensorManager sm;
Microphone mic;
int width, height;
public class DrawBalloon extends SurfaceView implements Runnable {
SurfaceHolder ourHolder ;
Thread ourThread = null;
boolean isRunning=true;
public DrawBalloon(Context context) {
super(context);
ourHolder= getHolder();
}
public void pause() {
isRunning=false;
while(true){
try{
ourThread.join();
} catch (InterruptedException e){
e.printStackTrace();
}
break;
}
ourThread=null;
}
public void resume(){
isRunning=true;
ourThread = new Thread(this);
ourThread.start();
}
@Override
public void run (){
while(isRunning){
if(!ourHolder.getSurface().isValid())
continue;
Canvas canvas = ourHolder.lockCanvas();
updateMic();
canvas.drawBitmap(background,0,0,null);
canvas.drawBitmap(balloon, sensorX, sensorY,null);
ourHolder.unlockCanvasAndPost(canvas);
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
/*sm= (SensorManager) getSystemService(Context.SENSOR_SERVICE);
if(sm.getSensorList(Sensor.TYPE_ACCELEROMETER).size()!=0){
Sensor s = sm.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);
sm.registerListener(this,s ,SensorManager.SENSOR_DELAY_NORMAL);
}*/
mic = new Microphone();
balloon = BitmapFactory.decodeResource(getResources(), R.drawable.images);
background = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
sensorX=150;
sensorY=350;
//x=y=sensorX=sensorY=0;
getSize(width,height);
_background = getResizedBitmap(background,height,width);
myView= new DrawBalloon (this);
myView.resume();
setContentView(myView);
}
/*@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
try {
Thread.sleep(20);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sensorX=event.values[0];
sensorY=event.values[1];
}
*/
public void updateMic(){
int level = mic.getLevel();
sensorY-=level;
}
@Override
public void onBackPressed() {
finish();
}
public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// Create a matrix for the manipulation
Matrix matrix = new Matrix();
// Resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
// Recreate the new Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
return resizedBitmap;
}
public void getSize(int x, int y){
WindowManager w = this.getWindowManager();
Display d = w.getDefaultDisplay();
DisplayMetrics metrics = new DisplayMetrics();
d.getMetrics(metrics);
// since SDK_INT = 1;
int widthPixels = metrics.widthPixels;
int heightPixels = metrics.heightPixels;
if (Build.VERSION.SDK_INT >= 14 && Build.VERSION.SDK_INT < 17){
try {
widthPixels = (Integer) Display.class.getMethod("getRawWidth").invoke(d);
heightPixels = (Integer) Display.class.getMethod("getRawHeight").invoke(d);
} catch (Exception ignored) {
}
}
// includes window decorations (statusbar bar/menu bar)
if (Build.VERSION.SDK_INT >= 17){
try {
Point realSize = new Point();
Display.class.getMethod("getRealSize", Point.class).invoke(d, realSize);
widthPixels = realSize.x;
heightPixels = realSize.y;
} catch (Exception ignored) {
}
}
x=widthPixels;
y=heightPixels;
}
}
第 99 行是:
_background = getResizedBitmap(background,height,width);
第 158 行是:
Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
谁能帮我 ?谢谢!