gpt4 book ai didi

android - 内存不足 : Heap Size Error after adding 10KB ImageButtons

转载 作者:行者123 更新时间:2023-11-30 03:09:20 25 4
gpt4 key购买 nike

我正在 Eclipse 中开发一个以启动画面开始的 android 应用程序(在完成一些研究后怀疑此处存在内存泄漏)。初始屏幕进入主菜单,该菜单仅包含 7 个按钮,这些按钮通向某些 Java Applet。整个应用程序运行完美,直到我将 7 个虚拟 (ImageButton) png 图像更改为最终的 7 个 png 图像。这些 png 图像平均有 10KB 并且不认为它们是问题的原因(因为它们这么小),即使这个问题是在我更改 ImageButtons 的这些 png 图像之后开始的。

老实说,除了再次调整图像大小之外,我不知道从哪里开始,因为它们被设计得有点大,以像素为单位(不是内存)以适应不同的设备。但我认为这个问题还有另一种解决方案,我作为初学者无法弄清楚。我希望有人能帮我解决这个问题:)

代码如下:

list 。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.letsfly.tryp"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:debuggable="true" >
<activity
android:name=".Splash"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="com.letsfly.tryp.MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Testing"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="com.letsfly.tryp.TESTING" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".trypOne"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.letsfly.tryp.TRYPONE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".trypTwo"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.letsfly.tryp.TRYPTWO" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".trypThree"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.letsfly.tryp.TRYPTHREE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".trypFour"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.letsfly.tryp.TRYPFOUR" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".trypFive"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.letsfly.tryp.TRYPFIVE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>


</manifest>

启动 XML。

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"

>

<ImageView
android:id="@+id/imageView1"
android:layout_width="600dp"
android:layout_height="800dp"
android:layout_gravity="center"
android:layout_marginTop="-180px"
android:contentDescription="@string/button1"
android:padding="0px"
android:src="@drawable/trypsplash" />

</LinearLayout>

启动 Java。

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

public class Splash extends Activity {


@Override
protected void onCreate(Bundle waitFiveSeconds) {
// TODO Auto-generated method stub

requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

super.onCreate(waitFiveSeconds);
setContentView(R.layout.splash);

Thread timer = new Thread(){
public void run(){
try{
sleep(5000);
}catch(InterruptedException e){
e.printStackTrace();

}finally{
Intent openMenu = new

Intent("com.letsfly.tryp.MAINACTIVITY");
startActivity(openMenu);

}
}
};
timer.start();
}
}

MainActivity Java(按钮所在的位置)。

package com.letsfly.tryp;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;

public class MainActivity extends Activity {

ImageButton button1;
ImageButton button2;
ImageButton button3;
ImageButton button4;
ImageButton button5;
ImageButton button6;
ImageButton button7;

@Override
protected void onCreate(Bundle savedInstanceState) {




//replace yourActivity.this with your own activity or if you declared a context you can write context.getSystemService(Context.VIBRATOR_SERVICE);

requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


button1 = (ImageButton) findViewById(R.id.imageButton1);
button2 = (ImageButton) findViewById(R.id.imageButton2);
button3 = (ImageButton) findViewById(R.id.imageButton3);
button4 = (ImageButton) findViewById(R.id.imageButton4);
button5 = (ImageButton) findViewById(R.id.imageButton5);
button6 = (ImageButton) findViewById(R.id.imageButton6);
button7 = (ImageButton) findViewById(R.id.imageButton7);




button1.setOnClickListener(new View.OnClickListener() {


@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent goToTrypOne = new Intent("com.letsfly.tryp.TRYPONE");
startActivity(goToTrypOne);

}
});
button2.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent goToTrypTwo = new Intent("com.letsfly.tryp.TRYPTWO");
startActivity(goToTrypTwo);


}
});
button3.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent goToTrypThree = new Intent("com.letsfly.tryp.TRYPTHREE");
startActivity(goToTrypThree);

}
});
button4.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent goToTrypTwo = new Intent("com.letsfly.tryp.TRYPFOUR");
startActivity(goToTrypTwo);

}
});
button5.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent goToTrypFive = new Intent("com.letsfly.tryp.TRYPFIVE");
startActivity(goToTrypFive);

}
});
button6.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

}

});
button7.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

}

});

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

主 Activity XML。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"

tools:context=".MainActivity" >

<ImageButton
android:id="@+id/imageButton1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:contentDescription="@string/button1"
android:src="@drawable/button1"
android:layout_weight="1"
android:layout_gravity="center"/>
<ImageButton
android:id="@+id/imageButton2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:contentDescription="@string/button2"
android:src="@drawable/button2"
android:layout_weight="1"
android:layout_gravity="center" />
<ImageButton
android:id="@+id/imageButton3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:contentDescription="@string/button3"
android:src="@drawable/button3"
android:layout_weight="1"
android:layout_gravity="center" />
<ImageButton
android:id="@+id/imageButton4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:contentDescription="@string/button4"
android:src="@drawable/button4"
android:layout_weight="1"
android:layout_gravity="center" />
<ImageButton
android:id="@+id/imageButton5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/button5"
android:contentDescription="@string/button5"
android:layout_weight="1"
android:layout_gravity="center" />
<ImageButton
android:id="@+id/imageButton6"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:contentDescription="@string/button6"
android:src="@drawable/button6"
android:layout_weight="1"
android:layout_gravity="center" />
<ImageButton
android:id="@+id/imageButton7"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:contentDescription="@string/button7"
android:src="@drawable/button7"
android:layout_weight="1"
android:layout_gravity="center" />

</LinearLayout>

最佳答案

像素过大实际上是个问题,因为 Android 需要将压缩的 png 放大为内存中的未压缩位图才能显示它们。

与其制作按比例缩小以适应不同尺寸/密度设备的大型 png,不如使用具有不同限定符的可绘制文件夹:

Images for mdpi devices: /res/drawable-mdpi
Images for hdpi devices: /res/drawable-hdpi
Images for xhdpi devices: /res/drawable-xhdpi
Images for xxhdpi devices: /res/drawable-xxhdpi

当您请求图像(例如,R.drawable.myImage)时,它会在具有与设备最匹配的密度限定符的文件夹中查找 myImage 文件。这使您可以自动确保小密度设备使用的图像占用的内存更小。

更多信息在这里: http://developer.android.com/guide/practices/screens_support.html

关于android - 内存不足 : Heap Size Error after adding 10KB ImageButtons,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21262919/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com