Android - Loading Drawables Without Skipping Frames
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
Solution 1:
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) {}
}
}
Post a Comment for "Android - Loading Drawables Without Skipping Frames"