ImageView.setImageResource is the culprit for my stuttering ListView, what now?

530 Views Asked by At

So my ListView was stuttering on my phone, so I used TraceView to determine the culprit.

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        View view = inflater.inflate(R.layout.menu_row, parent, false);
        ViewHolder holder = new ViewHolder();
        // setting other Views.
        holder.icon = (ImageView) view.findViewById(R.id.icon);
        view.setTag(holder);
        return view;
    }

    @Override
    public void bindView(View v, Context context, Cursor c) {
        ViewHolder holder = (ViewHolder) v.getTag();
        // getting other Views

        String imgUrl = c.getString(6);

        int resource = getResources().getIdentifier(imgUrl, "drawable", getActivity().getPackageName());
        if (resource != 0) {
            holder.icon.setImageResource(resource);
            return;
        }
   }

As expected newView and bindView were taking the most time from all of my method calls.

  • newView() is spending all of it's time in the inflater.inflate(R.layout.menu_row, parent, false); call
  • bindView() is spending all of it's time in holder.icon.setImageResource(resource);

I've dug deeper into setImageResource and the call hierarchy was this: (all of these methods were 99% of the cpu time):

  1. ImageView.setImageResource
  2. ImageView.resolveUri
  3. Resources.getDrawable
  4. Resources.loadDrawable
  5. Drawable.createFromResourceStream
  6. BitmapFactory.decodeResourceStream
  7. BitmapFactory.decodeStream
  8. BitmapFactory.nativeDecodeAsset (which is the method whose self is causing the most cpu time).

What can I do to optimize my ListView? (all of my images are not more than 15 KB in disk size).

This is the ImageView I'm setting xml:

<ImageView
        android:id="@+id/icon"
        android:layout_width="0dp"
        android:layout_height="85dp"
        android:layout_gravity="center_vertical"
        android:layout_marginRight="10dp"
        android:layout_weight="25"
        android:adjustViewBounds="true"
        android:contentDescription="@string/description_item"
        android:minHeight="85dp"
        android:minWidth="85dp"
        android:scaleType="fitCenter"
         />

EDIT: I've changed the ImageView xml to:

<ImageView
        android:id="@+id/icon"
        android:layout_width="85dp"
        android:layout_height="85dp"
        android:scaleType="center"
        android:contentDescription="@string/description_item"
        />

Didn't provide any performance gain.

0

There are 0 best solutions below