我在Eclipse上工作的Android应用程序以启动画面开始(在完成一些研究后怀疑内存泄漏)。启动画面进入主菜单,其中包含7个按钮,这些按钮通向一些Java Applet。整个应用程序运行良好,直到我将7个虚拟(ImageButton)png图像更改为最终的7 png图像。这些png图像平均为10KB,并且不认为它们是问题的原因(因为它们很小),即使在我更改了ImageButtons的这些png图像后这个问题也开始了。
老实说,除了重新调整图像大小之外,我不知道从哪里开始,因为它们的设计有点大像素(不是内存)以适应不同的设备。但我认为这个问题有另一个解决方案,我作为一个初学者无法弄清楚。我希望有人可以帮我解决这个问题:)
以下是代码:
清单。
<?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>
Splash 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;
}
}
MainActivity 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>
答案 0 :(得分:1)
实际上像素是大的问题,因为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