如何在android中缩放位图

时间:2014-07-11 15:18:30

标签: android bitmap

我使用此代码来扩展android中的位图,但不起作用,App停止。我附上了LOG CAT日志 请说出问题所在!?

screen=Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(),screenID),gameSurface.width,gameSurface.height,false);

Log Cat ::

    07-11 19:13:19.878: E/AndroidRuntime(22770): FATAL EXCEPTION: main
07-11 19:13:19.878: E/AndroidRuntime(22770): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.game/com.example.game.GameScreen}: java.lang.IllegalArgumentException: width and height must be > 0
07-11 19:13:19.878: E/AndroidRuntime(22770):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
07-11 19:13:19.878: E/AndroidRuntime(22770):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
07-11 19:13:19.878: E/AndroidRuntime(22770):    at android.app.ActivityThread.access$600(ActivityThread.java:127)
07-11 19:13:19.878: E/AndroidRuntime(22770):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
07-11 19:13:19.878: E/AndroidRuntime(22770):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-11 19:13:19.878: E/AndroidRuntime(22770):    at android.os.Looper.loop(Looper.java:137)
07-11 19:13:19.878: E/AndroidRuntime(22770):    at android.app.ActivityThread.main(ActivityThread.java:4441)
07-11 19:13:19.878: E/AndroidRuntime(22770):    at java.lang.reflect.Method.invokeNative(Native Method)
07-11 19:13:19.878: E/AndroidRuntime(22770):    at java.lang.reflect.Method.invoke(Method.java:511)
07-11 19:13:19.878: E/AndroidRuntime(22770):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-11 19:13:19.878: E/AndroidRuntime(22770):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-11 19:13:19.878: E/AndroidRuntime(22770):    at dalvik.system.NativeStart.main(Native Method)
07-11 19:13:19.878: E/AndroidRuntime(22770): Caused by: java.lang.IllegalArgumentException: width and height must be > 0
07-11 19:13:19.878: E/AndroidRuntime(22770):    at android.graphics.Bitmap.createBitmap(Bitmap.java:603)
07-11 19:13:19.878: E/AndroidRuntime(22770):    at android.graphics.Bitmap.createBitmap(Bitmap.java:551)
07-11 19:13:19.878: E/AndroidRuntime(22770):    at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:437)
07-11 19:13:19.878: E/AndroidRuntime(22770):    at com.example.game.Map1.<init>(Map1.java:21)
07-11 19:13:19.878: E/AndroidRuntime(22770):    at com.example.game.gameSurface.<init>(gameSurface.java:52)
07-11 19:13:19.878: E/AndroidRuntime(22770):    at com.example.game.GameScreen.onCreate(GameScreen.java:37)
07-11 19:13:19.878: E/AndroidRuntime(22770):    at android.app.Activity.performCreate(Activity.java:4465)
07-11 19:13:19.878: E/AndroidRuntime(22770):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
07-11 19:13:19.878: E/AndroidRuntime(22770):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
07-11 19:13:19.878: E/AndroidRuntime(22770):    ... 11 more

3 个答案:

答案 0 :(得分:0)

在您的LOG CAT中,您可以看到:

Caused by: java.lang.IllegalArgumentException: width and height must be > 0

因此您可能已将gameSurface.widthgameSurface.height设为0。

答案 1 :(得分:0)

尝试在你的onCreate中使用它

Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);

        screenWidth = size.x;
        screenHeight = size.y;

然后使用那些宽度和高度..我的猜测是,当你调用宽度和高度时,这些值为0,因为它们没有完成绘图或其他东西

答案 2 :(得分:0)

public static Bitmap  createScaledBitmap (Bitmap src, int dstWidth, int dstHeight, boolean filter) 

您收到IllegalArgumentException,因为您的宽度为&lt; = 0,或者height为&lt; = 0。