-3

我不能从变量中绘制位图。当我为它加注星标时,我的应用程序崩溃了。

public class krou extends View {


    public static int Width = aktivita.width;
    public static int Height = aktivita.height;



    final Paint mMalovani;
    public static Bitmap mBitmap;
    public static Bitmap rc;


    public void draw(Canvas canvas) {

        Paint p = new Paint();

        p.setColor(Color.RED);

        int ods = (Height - Width) / 2;



        Bitmap imageOne = BitmapFactory.decodeResource(getResources(), R.drawable.ri50_z);
        Bitmap ImageTwo = BitmapFactory.decodeResource(getResources(), R.drawable.ri50);

        rc = ImageTwo;



        Bitmap scaledBitmap = Bitmap.createScaledBitmap(rc, Width, Width, true);

        Rect dstRectForRender = new Rect(0, odstup, Width, Width + ods);
        canvas.drawBitmap(scaledBitmap, null, dstRectForRender, null);

如果我不使用rc变量,而我简单地使用imageOneor ImageTwo,一切都很好。

谢谢你的建议。

我的日志猫:

09-01 19:29:16.197: E/dalvikvm-heap(1472): Out of memory on a 5760016-byte allocation.
09-01 19:29:16.197: I/dalvikvm(1472): "main" prio=5 tid=1 RUNNABLE
09-01 19:29:16.197: I/dalvikvm(1472):   | group="main" sCount=0 dsCount=0 obj=0x40a729a0 self=0x2a00bba8
09-01 19:29:16.197: I/dalvikvm(1472):   | sysTid=1472 nice=0 sched=0/0 cgrp=apps handle=1073849308
09-01 19:29:16.207: I/dalvikvm(1472):   | state=R schedstat=( 3712781811 1124880434 230 ) utm=328 stm=43 core=0
09-01 19:29:16.207: I/dalvikvm(1472):   at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
09-01 19:29:16.207: I/dalvikvm(1472):   at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:502)
09-01 19:29:16.207: I/dalvikvm(1472):   at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:355)
09-01 19:29:16.207: I/dalvikvm(1472):   at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:378)
09-01 19:29:16.207: I/dalvikvm(1472):   at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:408)
09-01 19:29:16.207: I/dalvikvm(1472):   at org.vkedco.mobappdev.draw_touch_drive_00001.krouzky.draw(krouzky.java:71)
09-01 19:29:16.217: I/dalvikvm(1472):   at android.view.View.draw(View.java:13596)
09-01 19:29:16.217: I/dalvikvm(1472):   at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
09-01 19:29:16.217: I/dalvikvm(1472):   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
09-01 19:29:16.217: I/dalvikvm(1472):   at android.view.View.draw(View.java:13715)
09-01 19:29:16.217: I/dalvikvm(1472):   at android.view.View.draw(View.java:13596)
09-01 19:29:16.217: I/dalvikvm(1472):   at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
09-01 19:29:16.217: I/dalvikvm(1472):   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
09-01 19:29:16.217: I/dalvikvm(1472):   at android.view.View.draw(View.java:13594)
09-01 19:29:16.227: I/dalvikvm(1472):   at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
09-01 19:29:16.227: I/dalvikvm(1472):   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
09-01 19:29:16.237: I/dalvikvm(1472):   at android.view.View.draw(View.java:13594)
09-01 19:29:16.237: I/dalvikvm(1472):   at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
09-01 19:29:16.237: I/dalvikvm(1472):   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
09-01 19:29:16.237: I/dalvikvm(1472):   at android.view.View.draw(View.java:13715)
09-01 19:29:16.237: I/dalvikvm(1472):   at android.widget.FrameLayout.draw(FrameLayout.java:467)
09-01 19:29:16.237: I/dalvikvm(1472):   at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2211)
09-01 19:29:16.237: I/dalvikvm(1472):   at android.view.ViewRootImpl.drawSoftware(ViewRootImpl.java:2281)
09-01 19:29:16.237: I/dalvikvm(1472):   at android.view.ViewRootImpl.draw(ViewRootImpl.java:2177)
09-01 19:29:16.237: I/dalvikvm(1472):   at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2045)
09-01 19:29:16.248: I/dalvikvm(1472):   at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1854)
09-01 19:29:16.248: I/dalvikvm(1472):   at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
09-01 19:29:16.248: I/dalvikvm(1472):   at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
09-01 19:29:16.248: I/dalvikvm(1472):   at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
09-01 19:29:16.248: I/dalvikvm(1472):   at android.view.Choreographer.doCallbacks(Choreographer.java:562)
09-01 19:29:16.248: I/dalvikvm(1472):   at android.view.Choreographer.doFrame(Choreographer.java:532)
09-01 19:29:16.248: I/dalvikvm(1472):   at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
09-01 19:29:16.248: I/dalvikvm(1472):   at android.os.Handler.handleCallback(Handler.java:725)
09-01 19:29:16.248: I/dalvikvm(1472):   at android.os.Handler.dispatchMessage(Handler.java:92)
09-01 19:29:16.248: I/dalvikvm(1472):   at android.os.Looper.loop(Looper.java:137)
09-01 19:29:16.248: I/dalvikvm(1472):   at android.app.ActivityThread.main(ActivityThread.java:5041)
09-01 19:29:16.248: I/dalvikvm(1472):   at java.lang.reflect.Method.invokeNative(Native Method)
09-01 19:29:16.248: I/dalvikvm(1472):   at java.lang.reflect.Method.invoke(Method.java:511)
09-01 19:29:16.248: I/dalvikvm(1472):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-01 19:29:16.248: I/dalvikvm(1472):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-01 19:29:16.248: I/dalvikvm(1472):   at dalvik.system.NativeStart.main(Native Method)
09-01 19:29:16.267: D/skia(1472): --- allocation failed for scaled bitmap
09-01 19:29:16.267: D/AndroidRuntime(1472): Shutting down VM
09-01 19:29:16.298: W/dalvikvm(1472): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
09-01 19:29:16.348: E/AndroidRuntime(1472): FATAL EXCEPTION: main
09-01 19:29:16.348: E/AndroidRuntime(1472): java.lang.OutOfMemoryError
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:502)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:355)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:378)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:408)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at org.vkedco.mobappdev.draw_touch_drive_00001.krouzky.draw(krouzky.java:71)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.View.draw(View.java:13596)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.View.draw(View.java:13715)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.View.draw(View.java:13596)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.View.draw(View.java:13594)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.View.draw(View.java:13594)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.View.draw(View.java:13715)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.widget.FrameLayout.draw(FrameLayout.java:467)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2211)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.ViewRootImpl.drawSoftware(ViewRootImpl.java:2281)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.ViewRootImpl.draw(ViewRootImpl.java:2177)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2045)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1854)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.Choreographer.doFrame(Choreographer.java:532)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.os.Handler.handleCallback(Handler.java:725)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.os.Looper.loop(Looper.java:137)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.app.ActivityThread.main(ActivityThread.java:5041)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at java.lang.reflect.Method.invokeNative(Native Method)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at java.lang.reflect.Method.invoke(Method.java:511)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at dalvik.system.NativeStart.main(Native Method)
09-01 19:29:24.396: I/Process(1472): Sending signal. PID: 1472 SIG: 9
4

1 回答 1

0

正如我之前回答的那样 -在设备上创建位图时内存不足

您收到OutOfMemory错误是因为您的位图非常重它们是over 57MB 通过这种方法缩放您的所有位图 -

  Bitmap imageOne = decodeSampledBitmapFromResource(getResources(), R.drawable.ri50_z,height,width);//might be your screen hight and width

方法的实现是-

      public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
    int reqWidth, int reqHeight) {

// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);

// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}


 public static int calculateInSampleSize(
        BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;

if (height > reqHeight || width > reqWidth) {

    // Calculate ratios of height and width to requested height and width
    final int heightRatio = Math.round((float) height / (float) reqHeight);
    final int widthRatio = Math.round((float) width / (float) reqWidth);

    // Choose the smallest ratio as inSampleSize value, this will guarantee
    // a final image with both dimensions larger than or equal to the
    // requested height and width.
    inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}

return inSampleSize;
}

并缩放您的位图 src 参考上面的链接-

这将解决您的问题。

于 2013-09-02T07:50:28.153 回答