Android-加载可绘制对象而不跳过框架 [英] Android - Loading drawables without skipping frames
问题描述
好的,所以我在活动中使用了可绘制的PNG(1200 x 1920,30kb)作为背景.我的XML代码的片段如下所示.我的问题是应用程序正在跳过帧并具有滞后的响应.谁能告诉我为什么会这样,或者是解决我的问题的好方法吗?我仍然希望能够将此PNG文件用作背景.谢谢
Okay, so I am using a drawable PNG (1200 x 1920, 30kb) as a background in an activity. A snippet of my XML code is shown below. My problem is the application is skipping frames and has lagged responses. Could anybody tell me why this is happening, or a good way to solve my problem? I still would like to be able to use this PNG file as the background. Thanks
XML代码段:
<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:background="@drawable/MY_PNG_BACKGROUND"
tools:context=".MainActivity">
Logcat消息:
I/Choreographer: Skipped 53 frames! The application may be doing too much work on its main thread.
我看过的东西:
- Imageview跳帧(如果有人可以告诉我该怎么做,这可能对我有用XML,而不是Java中的编程语言)
- 导致跳过帧的背景图像
- Imageview skipped frames (This may work for me if someone can tell me how to do it in XML instead of programmatically in Java)
- background images causing frames to be skipped
推荐答案
在注释中感谢CommonsWare的帮助,我得以弄清楚这一点.我使用AsyncTask在后台在Java类中预加载了drawable.这是一个代码示例:
Thanks to some help from CommonsWare in the comments, I was able to figure this out. I pre-loaded the drawable in my java class in the background using AsyncTask. Here is a code example:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Calling the task to be done in the background, in this case loading the drawable
new LoadDrawable().execute();
}
private class LoadDrawable extends AsyncTask<Drawable, Void, Drawable> {
@Override
protected Drawable doInBackground(Drawable... params) {
//Loading the drawable in the background
final Drawable image = getResources().getDrawable(R.drawable.my_drawable);
//After the drawable is loaded, onPostExecute is called
return image;
}
@Override
protected void onPostExecute(Drawable loaded) {
//Hide the progress bar
ProgressBar progress = (ProgressBar) findViewById(R.id.progress_bar);
progress.setVisibility(View.GONE);
//Set the layout background with your loaded drawable
RelativeLayout layout = (RelativeLayout) findViewById(R.id.my_layout);
layout.setBackgroundDrawable(loaded);
}
@Override
protected void onPreExecute() {}
@Override
protected void onProgressUpdate(Void... values) {}
}
}
这篇关于Android-加载可绘制对象而不跳过框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!