Android - 加载drawables而不跳帧

时间:2016-01-30 22:22:24

标签: android xml drawable

好的,所以我使用可绘制的PNG(1200 x 1920,30kb)作为活动的背景。我的XML代码片段如下所示。我的问题是应用程序正在跳帧并且响应滞后。谁能告诉我为什么会这样,或者解决我的问题的好方法?我仍然希望能够使用此PNG文件作为背景。感谢

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.

我看过的事情:

1 个答案:

答案 0 :(得分:0)

感谢CommonsWare在评论中的一些帮助,我能够解决这个问题。我使用AsyncTask在后台预加载了我的java类中的drawable。这是一个代码示例:

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) {}
    }
}