1

我的应用程序中有一个正确显示的 jpg 图像。在我的列表视图中,我想让图像更透明,以便更容易看到文本。我将图像更改为 png 格式并在 GIMP 中更改了它的不透明度。

现在新图像位于应用程序可绘制文件夹中。我收到以下错误。为什么?

   09-28 09:24:07.560: I/global(20140): call socket shutdown, tmpsocket=Socket[address=/178.250.50.40,port=80,localPort=35172]
09-28 09:24:07.570: I/global(20140): call socket shutdown, tmpsocket=Socket[address=/212.169.27.217,port=84,localPort=55656]
09-28 09:24:07.690: D/dalvikvm(20140): GC_FOR_ALLOC freed 113K, 4% free 38592K/39907K, paused 32ms
09-28 09:24:07.690: I/dalvikvm-heap(20140): Forcing collection of SoftReferences for 28072816-byte allocation
09-28 09:24:07.740: D/dalvikvm(20140): GC_BEFORE_OOM freed 9K, 4% free 38582K/39907K, paused 43ms
09-28 09:24:07.740: E/dalvikvm-heap(20140): Out of memory on a 28072816-byte allocation.
09-28 09:24:07.740: I/dalvikvm(20140): "main" prio=5 tid=1 RUNNABLE
09-28 09:24:07.740: I/dalvikvm(20140):   | group="main" sCount=0 dsCount=0 obj=0x40a57490 self=0x1b6e9a8
09-28 09:24:07.740: I/dalvikvm(20140):   | sysTid=20140 nice=0 sched=0/0 cgrp=default handle=1074361640
09-28 09:24:07.740: I/dalvikvm(20140):   | schedstat=( 2289118000 760844000 2121 ) utm=195 stm=33 core=1
09-28 09:24:07.740: I/dalvikvm(20140):   at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
09-28 09:24:07.740: I/dalvikvm(20140):   at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:486)
09-28 09:24:07.740: I/dalvikvm(20140):   at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:773)
09-28 09:24:07.740: I/dalvikvm(20140):   at android.content.res.Resources.loadDrawable(Resources.java:2042)
09-28 09:24:07.740: I/dalvikvm(20140):   at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
09-28 09:24:07.740: I/dalvikvm(20140):   at android.view.View.<init>(View.java:2812)
09-28 09:24:07.740: I/dalvikvm(20140):   at android.view.ViewGroup.<init>(ViewGroup.java:410)
09-28 09:24:07.740: I/dalvikvm(20140):   at android.widget.LinearLayout.<init>(LinearLayout.java:174)
09-28 09:24:07.740: I/dalvikvm(20140):   at android.widget.LinearLayout.<init>(LinearLayout.java:170)
09-28 09:24:07.740: I/dalvikvm(20140):   at java.lang.reflect.Constructor.constructNative(Native Method)
09-28 09:24:07.740: I/dalvikvm(20140):   at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
09-28 09:24:07.740: I/dalvikvm(20140):   at android.view.LayoutInflater.createView(LayoutInflater.java:586)
09-28 09:24:07.740: I/dalvikvm(20140):   at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
09-28 09:24:07.740: I/dalvikvm(20140):   at android.view.LayoutInflater.onCreateView(LayoutInflater.java:653)
09-28 09:24:07.740: I/dalvikvm(20140):   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:678)
09-28 09:24:07.740: I/dalvikvm(20140):   at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
09-28 09:24:07.740: I/dalvikvm(20140):   at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
09-28 09:24:07.740: I/dalvikvm(20140):   at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
09-28 09:24:07.740: I/dalvikvm(20140):   at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:278)
09-28 09:24:07.740: I/dalvikvm(20140):   at android.app.Activity.setContentView(Activity.java:1897)
09-28 09:24:07.740: I/dalvikvm(20140):   at com.carefreegroup.ShowMoreDetails.onCreate(ShowMoreDetails.java:26)
09-28 09:24:07.740: I/dalvikvm(20140):   at android.app.Activity.performCreate(Activity.java:4543)
09-28 09:24:07.740: I/dalvikvm(20140):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
09-28 09:24:07.740: I/dalvikvm(20140):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2181)
09-28 09:24:07.740: I/dalvikvm(20140):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2260)
09-28 09:24:07.740: I/dalvikvm(20140):   at android.app.ActivityThread.access$600(ActivityThread.java:139)
09-28 09:24:07.740: I/dalvikvm(20140):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1277)
09-28 09:24:07.740: I/dalvikvm(20140):   at android.os.Handler.dispatchMessage(Handler.java:99)
09-28 09:24:07.740: I/dalvikvm(20140):   at android.os.Looper.loop(Looper.java:156)
09-28 09:24:07.740: I/dalvikvm(20140):   at android.app.ActivityThread.main(ActivityThread.java:5045)
09-28 09:24:07.740: I/dalvikvm(20140):   at java.lang.reflect.Method.invokeNative(Native Method)
09-28 09:24:07.740: I/dalvikvm(20140):   at java.lang.reflect.Method.invoke(Method.java:511)
09-28 09:24:07.740: I/dalvikvm(20140):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-28 09:24:07.740: I/dalvikvm(20140):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-28 09:24:07.740: I/dalvikvm(20140):   at dalvik.system.NativeStart.main(Native Method)
09-28 09:24:07.740: E/dalvikvm(20140): Out of memory: Heap Size=46115KB, Allocated=38582KB, Limit=65536KB
09-28 09:24:07.740: E/dalvikvm(20140): Extra info: Footprint=39907KB, Allowed Footprint=46115KB, Trimmed=892KB
09-28 09:24:07.740: E/Bitmap_JNI(20140): Create Bitmap Failed.
09-28 09:24:07.740: A/libc(20140): Fatal signal 11 (SIGSEGV) at 0x00000004 (code=1)
09-28 09:24:09.750: I/dalvikvm(20367): Turning on JNI app bug workarounds for target SDK version 10...
09-28 09:24:09.940: D/dalvikvm(20367): GC_CONCURRENT freed 864K, 21% free 3797K/4771K, paused 2ms+2ms

谢谢。

[更新]

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.showmoredetailslayout);
        actualCallTime = (TextView)findViewById(R.id.actualcalltime);
        doubleUp = (TextView)findViewById(R.id.doubleupcallid);
        needName = (TextView)findViewById(R.id.needname);
        needNameLabel = (TextView)findViewById(R.id.neednamelabel);
        getRotaDetails = (Button)findViewById(R.id.buttongetrotadetails);
        intent = this.getIntent();
        String actualTimeIn = intent.getStringExtra("actTimeIn");
        String actualTimeOut = intent.getStringExtra("actTimeOut");
        String doubleUpValue = intent.getStringExtra("doubleUpValue");
        String needNameWithCommas = intent.getStringExtra("needNameWithCommas");
        callID = intent.getStringExtra("callID");
        String[] needs = needNameWithCommas.split(",");



        actualCallTime.setText("This call was completed at " + actualTimeIn + " -" + actualTimeOut);
        if( ! doubleUpValue.equalsIgnoreCase("") || doubleUpValue.equalsIgnoreCase("]")){
        doubleUp.setText("This call was not a double up ");
        }else{

            doubleUp.setText("This call was  a double up " + doubleUpValue);
        }

        needNameLabel.setText("Purpose of Call: ");

        for (int i = 0; i < needs.length; i++){
        needName.append( needs[i] + "\n");
        }

        getRotaDetails.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent intent = new Intent(ShowMoreDetails.this, GetRotaDetails.class);
                intent.putExtra("callIDExtra", callID);
                startActivity(intent);

            }
        });
    }

}
4

3 回答 3

2

您的活动窗口的默认PixelFormat值为RGB_565

当您使用JPEG图像时,它正在RGB_565 (16-bit)中读取。

当您使用PNG具有透明度(alpha 值)的图像时,它是在RGBA_8888 (32-bit)中读取的。

因此,相同的图像尺寸(如尺寸)需要更多的内存。

于 2012-09-28T08:55:41.310 回答
2

正如@Benito 所说,PNG 需要两倍于 jpg 的内存。因此,您的选择是:

  1. 减小图像的大小。

  2. 使用 JPG,并为其使用 alpha。不幸的是,您不能在 xml 中设置 alpha,因此您应该以编程方式进行(见下文)。

即使您选择选项 2,我也建议减小图像大小,因为即使它可以在您的手机上使用,其他型号的内存也可能会更少。从日志看来,您的图像大约为 2K*3K,这可能比所有屏幕都大。

View view = findViewById(R.id.background);
Drawable background = view.getBackground();
background.setAlpha(80);
于 2012-09-28T09:51:43.677 回答
1

试试下面的链接,在这里你会得到一些相关的答案。

Android VM 内存不足错误

或者

Android png图像在内存中很大

这可能会对您有所帮助。

于 2012-09-28T08:46:28.400 回答