brgr
brgr

Reputation: 318

App lags with android:layerType="software"

I am making a really simple app (basically a listview with only a few images). It generally performs really well on my testing device (a Motorola Moto G with the stock android that comes with it).

However, I want my ListView to have dotted dividers. Therefore I made a list_divider.xml file defining the divider and set this to be the divider to be used in the ListView. I have tried that out on my Moto G, but it didn't display the line dotted.

So I've googled around and found here a solution to the problem. Disabling hw acceleration for the view solved the error and displayed the divider correctly.

However, now I have another problem: Since disabling hw acceleration the app began to lag when scrolling.

Is there a solution to having a dotted divider and not disabling hw acceleration or at least have the performance nearly as good as before disabling it?

Edit: Here is the adapter code I use:

public class AstronautArrayAdapter extends ArrayAdapter<Astronaut> {

    public static final String TAG = "AstronautArrayAdapter";

    public AstronautArrayAdapter(Context context, List<Astronaut> objects) {
        super(context, R.layout.astronauts_list_row, objects);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        RowViewHolder holder = null;
        if (convertView == null) {
            LayoutInflater inflater = ((Activity) getContext()).getLayoutInflater();
            convertView = inflater.inflate(R.layout.astronauts_list_row, parent, false);

            holder = new RowViewHolder();
                holder.name = (TextView) convertView.findViewById(R.id.nameOfAustronaut);
            holder.daysInSpace = (TextView) convertView.findViewById(R.id.numberOfDaysInSpace);
            holder.country = (ImageView) convertView.findViewById(R.id.countryImage);
            holder.title = (TextView) convertView.findViewById(R.id.titleOfAstronaut);

            convertView.setTag(holder);
        } else {
            holder = (RowViewHolder) convertView.getTag();
        }

        Astronaut astronaut = getItem(position);

        holder.name.setText(astronaut.getName());
        holder.daysInSpace.setText(Integer.toString(astronaut.getDaysInSpace()));
        holder.country.setImageBitmap(astronaut.getCountryImage(getContext()));
        holder.title.setText(astronaut.getTitle());

        return convertView;
    }

    private static class RowViewHolder {
        TextView name;
        TextView daysInSpace;
        ImageView country;
        TextView title;
    }

}

Edit 2: Here are the two layout files in question:

activity_main.xml

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/astronautsList"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:padding="16dp"
    android:dividerHeight="3dp"
    android:divider="@drawable/list_divider"
    android:layerType="software" />

list_divider.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="line" >

    <stroke
        android:width="1px"
        android:color="@android:color/black"
        android:dashWidth="5px"
        android:dashGap="1px" />

    <size android:height="3dp" />

</shape>

Upvotes: 3

Views: 2196

Answers (2)

Budius
Budius

Reputation: 39846

possible answer for the lag you're seeing is:

add this line inside the dependencies section to your build.gradle

compile 'com.squareup.picasso:picasso:2.3.2'

and replace this line

holder.country.setImageBitmap(astronaut.getCountryImage(getContext()));

with

Picasso.with(context).load(/* insert the URL here */).into(holder.country);

and remove the code that is downloading all the images and saving locally. Picasso automagically download/decode the bitmaps and manage disk and ram cache for you.

ps.:

if you're not using Gradle:

  1. You should change to Android-Studio and use Gradle
  2. forget what I said about adding the line to the gradle file and just download the jar file from their website, put into the libs folder and make sure to mess on the project settings to make sure that jar goes inside your APK (I honestly do not remember how those settings work because I abandoned Eclipse long time ago)

Upvotes: 1

Graham Smith
Graham Smith

Reputation: 25757

As you did not mentioned it, have you implemented the ViewHolder pattern?

As the ListView recycles views it has to inflate a layout and insert any text and images in. You normally call findViewById in this section of code which is quite expensive. The ViewHolder pattern solves this and is easy to implement.

See here for a great tutorial by Google

http://developer.android.com/training/improving-layouts/smooth-scrolling.html

Upvotes: 0

Related Questions