我正在制作一个非常简单的应用程序(基本上是一个只有几张图片的 ListView )。它通常在我的测试设备(摩托罗拉 Moto G 和随附的 android 设备)上表现非常好。
但是,我希望我的 ListView 有虚线分隔符。因此,我制作了一个定义分隔线的 list_divider.xml 文件,并将其设置为要在 ListView 中使用的分隔线。我已经在我的 Moto G 上试过了,但它没有显示虚线。
所以我用谷歌搜索并找到了 here问题的解决方案。为 View 禁用硬件加速解决了错误并正确显示了分隔线。
但是,现在我遇到了另一个问题:自从禁用硬件加速后,应用程序在滚动时开始出现延迟。
是否有一种解决方案可以解决使用虚线分隔符而不禁用硬件加速或至少使性能与禁用它之前几乎一样好的问题?
编辑:这是我使用的适配器代码:
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;
}
}
编辑 2:这是有问题的两个布局文件:
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>
最佳答案
您看到的延迟的可能答案是:
将 dependencies
部分中的这一行添加到您的 build.gradle
compile 'com.squareup.picasso:picasso:2.3.2'
并替换这一行
holder.country.setImageBitmap(astronaut.getCountryImage(getContext()));
与
Picasso.with(context).load(/* insert the URL here */).into(holder.country);
并删除下载所有图像并保存在本地的代码。 Picasso 会自动下载/解码位图并为您管理磁盘和内存缓存。
附:
如果您不使用 Gradle:
- 你应该改用 Android-Studio 并使用 Gradle
- 忘记我所说的将行添加到 gradle 文件中,只需从他们的 website 下载 jar 文件即可。 ,放入
libs
文件夹并确保搞乱项目设置以确保 jar 进入你的 APK(老实说,我不记得这些设置是如何工作的,因为我很久以前就放弃了 Eclipse)
关于android - 应用程序滞后于 android :layerType ="software",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24587164/