【发布时间】:2015-03-03 04:51:49
【问题描述】:
我有一个非常简单的应用程序,在 RelativeLayout 中只有一个 TextView 和一个 Button(带有背景图片):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
android:background="@drawable/background_image" >
<!-- text in top left of background -->
<TextView android:text="App" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<!-- primary button that outputs audio -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:onClick="trueButtonClick" />
</RelativeLayout>
onClick 事件只是播放一些音频:
public void trueButtonClick(View v) {
//create media player
MediaPlayer mp = MediaPlayer.create(this, R.raw.audio_test);
//make noise
mp.start();
}
在 logcat 中,我收到以下消息:
03-02 23:41:40.063 1910-1910/com.example.mohammad.trueapp E/MediaPlayer﹕ Should have subtitle controller already set
03-02 23:41:40.063 1910-1910/com.example.mohammad.trueapp I/Choreographer﹕ Skipped 210 frames! The application may be doing too much work on its main thread.
03-02 23:41:43.961 1910-1910/com.example.mohammad.trueapp I/Choreographer﹕ Skipped 232 frames! The application may be doing too much work on its main thread.
03-02 23:45:05.506 1910-1910/com.example.mohammad.trueapp I/Choreographer﹕ Skipped 115 frames! The application may be doing too much work on its main thread.
03-02 23:45:07.279 1910-1910/com.example.mohammad.trueapp E/MediaPlayer﹕ Should have subtitle controller already set
03-02 23:45:07.323 1910-1910/com.example.mohammad.trueapp I/Choreographer﹕ Skipped 108 frames! The application may be doing too much work on its main thread.
03-02 23:45:10.490 1910-1910/com.example.mohammad.trueapp E/MediaPlayer﹕ Should have subtitle controller already set
当我删除背景图像@drawable/background_image 时,应用程序会加速。但是,在添加背景图像后,应用程序非常缓慢,以至于从用户的角度来看似乎已损坏。背景图像尺寸为 1040x851。有没有办法可以加快速度以防止严重滞后?
【问题讨论】:
-
您是否在 AndroidManifest 文件中添加了硬件加速=true?
-
@Rustam 我就是这么做的。没有太大的区别。另外,据我了解,这对较旧的 Android 手机没有帮助。
-
您的图像是否在
drawable-?dpi文件夹中缩放为不同的分辨率?这将优化图像加载。 -
@Lamorak 我没有。我必须手动调整它们的大小吗?我必须选择在 Java 端还是 XML 端使用哪个图像?
-
当您引用
@drawable/image时,android 会自动选择当前分辨率的最佳图像。如果最好的图像不匹配,如果提供了比例,android 将调整它的大小。 (不要存储在drawable或drawable-nodpi):) 见this documantation