我在Universal Image Loader
中使用GridView
来显示图片。我把我的图像放在drawable文件夹中。但是当我滚动它时它非常迟钝!我尝试PauseOnScrollListener
但滚动时仍然很慢......
这是我的GridView片段和适配器:
public class SubCategoryFragment extends Fragment {
TypedArray imgIDs;
int[] imgResIds;
String subCatTitle[];
DisplayImageOptions options;
ImageLoaderConfiguration config;
ImageLoader imageLoader;
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
options = new DisplayImageOptions.Builder()
.cacheInMemory(true)
.cacheOnDisk(true)
.bitmapConfig(Bitmap.Config.RGB_565)
.imageScaleType(ImageScaleType.EXACTLY)
.build();
config = new ImageLoaderConfiguration.Builder(getActivity())
.memoryCacheSize(41943040)
.discCacheSize(104857600)
.threadPoolSize(5)
.build();
imageLoader = ImageLoader.getInstance();
imageLoader.init(config);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.sub_category_fragment, container, false);
ImageLoader.getInstance().init(config);
//if(getArguments().getString("cat") == "choose"){
subCatTitle = getResources().getStringArray(R.array.choose);
imgIDs = getResources().obtainTypedArray(R.array.chooseImgIDs);
//}
imgResIds = new int[imgIDs.length()];
for(int i = 0; i < imgIDs.length(); i++)
imgResIds[i] = imgIDs.getResourceId(i, -1);
imgIDs.recycle();
((GridView) view.findViewById(R.id.sub_category_gridview)).setOnScrollListener(new PauseOnScrollListener(imageLoader, false, true));
((GridView) view.findViewById(R.id.sub_category_gridview)).setAdapter(new CustomGrid(getActivity()));
return view;
}
public class CustomGrid extends BaseAdapter{
private Context mContext;
public CustomGrid(Context c) {
mContext = c;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return subCatTitle.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder viewHolder;
if(convertView == null){
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.sub_category_view, parent, false);
viewHolder = new ViewHolder();
viewHolder.subCatView = (LinearLayout) convertView.findViewById(R.id.sub_category_view);
viewHolder.subCatImg = (ImageView) convertView.findViewById(R.id.sub_category_img);
viewHolder.subCatTitle = (TextView) convertView.findViewById(R.id.sub_category_name);
convertView.setTag(viewHolder);
}
else{
//imgIDs.recycle();
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.subCatTitle.setText(subCatTitle[position]);
imageLoader.displayImage("drawable://"+imgResIds[position], viewHolder.subCatImg, options);
return convertView;
}
}
public class ViewHolder{
public TextView subCatTitle;
public ImageView subCatImg;
public LinearLayout subCatView;
}
}
这是我的GridView项目布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:id="@+id/sub_category_view">
<ir.motorel.carbuyinstruduction.SquareLinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/button">
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:maxWidth="250dp"
android:maxHeight="250dp"
android:src="@drawable/picture1"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:layout_margin="10dp"
android:id="@+id/sub_category_img"/>
</ir.motorel.carbuyinstruduction.SquareLinearLayout>
<TextView
android:layout_marginTop="5dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello"
android:gravity="center"
android:textColor="#C36518"
android:id="@+id/sub_category_name"/>
</LinearLayout>
有什么问题?
答案 0 :(得分:1)
可能这不是原因,但你应该用本机方式加载你的drawables看看这个笔记(在库git页面上): 注意:使用drawable://只有在你真的需要的时候!始终考虑加载drawables的本机方式 - ImageView.setImageResource(...)而不是使用ImageLoader。
其次我会尝试删除包装布局(SquareLinearLayout)并查看它是如何进行的。它也可能是由您的图像大小引起的,请确保您使用的是缩略图图像。