在进入之前,让我先解释一下我在做什么
基本上,我给用户一个从SD卡中选择图像的选项,将所选图像的Uri存储在我的数据库中,并将其作为列表项目的化身,但具有化身的列表项目越多,列表的滚动就越慢
我用Loader
加载列表
@Override
public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
return new CursorLoader(getActivity(),BowlersDB.CONTENT_URI,
new String[] {BowlersDB.ID,BowlersDB.FIRST_NAME,BowlersDB.LAST_NAME,BowlersDB.PHOTO_URI},null,null,BowlersDB.LAST_NAME + " COLLATE LOCALIZED ASC");
}
我有一个自定义适配器,可以调整图像大小、设置文本视图等。
@Override
public void bindView(final View view,final Context context,final Cursor cursor){
final int id = cursor.getInt(0);;
final QuickContactBadge image = (QuickContactBadge)view.findViewById(R.id.quickContactBadge1);
image.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
if(!fromGame){
bowlerID = id;
Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i,1);
}
}
});
String uri = cursor.getString(3);
if(uri != null){
InputStream input=null;
try {
input = getActivity().getContentResolver().openInputStream(Uri.parse(cursor.getString(3)));
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = false;
Bitmap og = BitmapFactory.decodeStream(input,null,options);
int height = options.outHeight;
int width = options.outWidth;
BitmapFactory.decodeResource(getResources(), R.drawable.ic_contact_picture, options);
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
float scaledHeight = ((float)imageHeight)/height;
float scaledWidth = ((float)imageWidth)/width;
Matrix matrix = new Matrix();
matrix.postScale(scaledWidth, scaledHeight);
final Bitmap resized = Bitmap.createBitmap(og, 0, 0, width, height, matrix, true);
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
image.setImageBitmap(resized);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}else{
image.setImageResource(R.drawable.ic_contact_picture);
}
TextView first = (TextView)view.findViewById(R.id.bListTextView);
TextView last = (TextView)view.findViewById(R.id.bListTextView2);
first.setText(cursor.getString(1));
last.setText(cursor.getString(2));
}
}
现在我知道可能是图像的大小导致它如此缓慢,但当我把那部分代码放在一个单独的线程中时,这没有任何效果,但我认为LoaderManager
无论如何都会在后台加载所有内容
这是我的行xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minHeight="60dp"
android:id="@+id/names_layout"
android:background="@drawable/list_item_state">
<QuickContactBadge
android:id="@+id/quickContactBadge1"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/ic_contact_picture"
style="?android:attr/quickContactBadgeStyleWindowLarge" />
<TextView
android:id="@+id/bListTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/quickContactBadge1"
android:background="@drawable/list_item_state"
android:paddingLeft="20dp"
android:paddingTop="2dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="20dp"
android:textColor="?android:attr/textColorPrimary" />
<TextView
android:id="@+id/bListTextView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/quickContactBadge1"
android:layout_below="@+id/bListTextView"
android:paddingLeft="20dp"
android:background="@drawable/list_item_state"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="15dp"
android:textColor="?android:attr/textColorSecondary"
android:layout_alignBottom="@+id/quickContactBadge1"/>
</RelativeLayout>
CCD_ 3保持图像并且如果没有图像设置则我具有默认图像设置。
那么我该如何解决这个问题呢?
我尝试做的一个例子是谷歌音乐应用程序,其中专辑列表中有所有的专辑头像,滚动没有问题,它们会根据需要出现,这似乎有点不同,因为图像可能是从服务器下载的
您可以lazyLoad列表视图的图像。这里有一个完整的例子:
http://codehenge.net/blog/2011/06/android-development-tutorial-asynchronous-lazy-loading-and-caching-of-listview-images/
您在bindView线程中对图像进行流式处理,这通常不是一个好主意。我通常在asyncTask中下载和构建位图,或者更确切地说,在RoboAsyncTask中。我将ImageView和图像URL一起传递,因此在asynctask的onSuccess()中,图像是在下载或位图创建完成后设置的。您在屏幕上看到的是在其他内容之后不久出现的图像。您还可以将占位符资源传递给异步任务,以便在下载完成时显示。
在您的活动或片段中:
mediaManager.setImage(imageUrl, imageView, R.drawable.placeholder_image);
在MediaManager:中
@Override
public void setImage(String imageUrl, ImageView imageView, int placeholderResource) {
new SetImageTask(context, imageUrl, imageView, placeholderResource).execute();
}
@Override
public Bitmap getImage(String imageUrl) throws IllegalStateException, IOException, SQLException {
// put your code to fetch image here....
}
在SetImageTask(扩展RoboAsyncTask)中:
@Override
public void onPreExecute() {
imageView.setImageResource(placeholderResource);
}
@Override
public Bitmap call() throws Exception {
return mediaManager.getImage(imageUrl);
}
@Override
public void onSuccess(Bitmap bitmap) {
imageView.setImageBitmap(bitmap);
}