In a nutshell, I’m trying to display an animated GIF in a webview. This webview is in a list item. So, there is a listview of different gifs.
I’m setting the gifs on the listview like so:
View v = convertView;
if (v == null || position == 1) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.routine_exercise_item, parent, false);
}
TextView t = (TextView)v.findViewById(R.id.routine_exercise_num);
t.setText(getResources().getString(R.string.other_exercise) + " " + (position));
t = (TextView)v.findViewById(R.id.routine_exercise_description);
t.setText(mitems.get(position).description);
WebView anim = (WebView)v.findViewById(R.id.routine_exercise_gif);
anim.loadUrl("file:///android_asset/" + mitems.get(position).imagePath);
anim.setBackgroundColor(0);
I’m doing this inside getView of an ArrayAdapter<> subclass.
Everything loads just fine and the gif works… However, as soon as it’s displayed, my heap grows like crazy. It only takes 30-45 seconds for the heap to hit 32MB on my EVO 3d and crash.
I’m not sure what I’m doing wrong for such a memory leak to happen.
Any tips?
Oof. A
WebViewperListViewitem? Yeah, they’re heavy. I don’t necessarily see anything that would cause the behavior you’re seeing, but utilizing aWebViewjust to show a GIF is a bit overkill. Take a look at this tutorial on aViewsubclass that usesandroid.graphics.movieto display animated GIFs. Might be lighter than WebViews.