代码:
public static Bitmap readBitmapInSize(Context paramContext, int paramInt,
int width, int height) {
Matrix matrix = new Matrix();
Bitmap bitmap = readBitmap(paramContext, paramInt);
matrix.setScale( width / bitmap.getWidth(), height
/ bitmap.getHeight());
Bitmap resizeBmp = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
bitmap.getHeight(), matrix, true);
return resizeBmp;
}
报错信息:
05-04 13:25:36.422: ERROR/AndroidRuntime(2009): FATAL EXCEPTION: main
05-04 13:25:36.422: ERROR/AndroidRuntime(2009): java.lang.IllegalArgumentException: width and height must be > 0
05-04 13:25:36.422: ERROR/AndroidRuntime(2009): at android.graphics.Bitmap.createBitmap(Bitmap.java:603)
05-04 13:25:36.422: ERROR/AndroidRuntime(2009): at android.graphics.Bitmap.createBitmap(Bitmap.java:551)
05-04 13:25:36.422: ERROR/AndroidRuntime(2009): at com.zoipuus.mypet.util.BitmapOperation.readBitmapInSize(BitmapOperation.java:31)
05-04 13:25:36.422: ERROR/AndroidRuntime(2009): at com.zoipuus.mypet.util.CreateViewUtil.createIconView(CreateViewUtil.java:45)
05-04 13:25:36.422: ERROR/AndroidRuntime(2009): at com.zoipuus.mypet.service.MyWindowService.createIconView(MyWindowService.java:91)
05-04 13:25:36.422: ERROR/AndroidRuntime(2009): at com.zoipuus.mypet.view.MoveImageView.onTouchEvent(MoveImageView.java:155)
05-04 13:25:36.422: ERROR/AndroidRuntime(2009): at android.view.View.dispatchTouchEvent(View.java:5541)
05-04 13:25:36.422: ERROR/AndroidRuntime(2009): at android.view.View.dispatchPointerEvent(View.java:5721)
05-04 13:25:36.422: ERROR/AndroidRuntime(2009): at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:2890)
05-04 13:25:36.422: ERROR/AndroidRuntime(2009): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2466)
05-04 13:25:36.422: ERROR/AndroidRuntime(2009): at android.view.ViewRootImpl.processInputEvents(ViewRootImpl.java:845)
05-04 13:25:36.422: ERROR/AndroidRuntime(2009): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2475)
05-04 13:25:36.422: ERROR/AndroidRuntime(2009): at android.os.Handler.dispatchMessage(Handler.java:99)
05-04 13:25:36.422: ERROR/AndroidRuntime(2009): at android.os.Looper.loop(Looper.java:137)
05-04 13:25:36.422: ERROR/AndroidRuntime(2009): at android.app.ActivityThread.main(ActivityThread.java:4424)
05-04 13:25:36.422: ERROR/AndroidRuntime(2009): at java.lang.reflect.Method.invokeNative(Native Method)
05-04 13:25:36.422: ERROR/AndroidRuntime(2009): at java.lang.reflect.Method.invoke(Method.java:511)
05-04 13:25:36.422: ERROR/AndroidRuntime(2009): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-04 13:25:36.422: ERROR/AndroidRuntime(2009): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-04 13:25:36.422: ERROR/AndroidRuntime(2009): at dalvik.system.NativeStart.main(Native Method)
打了断点看了一下数值都是大于0的,一开始以为bitmap的获取方法不对,换了也一样出错。功夫不负有心人,最后找到解决办法的灵感来自这个网站4楼的回答http://bbs.csdn.net/topics/370255160。自己的猜测:Bitmap的getWidth和getHeight方法返回的是int类型,而Matrix的setScale方法需要的是float类型,所以如果分子小于分母就会得到0,就会报错。检验猜测:把int类型转换为float类型, matrix.setScale(1.0f * width / bitmap.getWidth(), 1.0f * height / bitmap.getHeight());问题终于解决了。如果有错恳请大神指导。