gpt4 book ai didi

android - 在 ListView 中加载图像后, ListView 滚动真的很慢

转载 作者:搜寻专家 更新时间:2023-11-01 09:06:10 25 4
gpt4 key购买 nike

在我进入所有内容之前,让我先解释一下我在做什么

基本上,我给用户一个选项,让他们从 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");
}

我有一个自定义适配器,可以调整图像大小、设置 TextView 等。

@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>

Quick Contact Badge 包含图像,如果没有图像集,我有一个默认图像集。

那么我该如何解决这个问题呢?

我正在尝试做的一个例子是谷歌音乐应用程序,其中专辑列表包含所有这些专辑头像并且滚动没有问题,它们似乎根据需要出现,但显然它有点不同,因为图片可能是从服务器下载的

最佳答案

关于android - 在 ListView 中加载图像后, ListView 滚动真的很慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11768522/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com