すべてに入る前に、まず何をしているのか説明させてください
基本的に、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>
はQuick Contact Badge
画像を保持し、画像が設定されていない場合のデフォルトの画像を設定しています。
では、どうすればこれを修正できますか?
私がやろうとしていることの例は、アルバムリストにすべてのアルバムアバターがあり、スクロールに問題がないGoogleミュージックアプリです。それらは必要に応じて表示されますが、画像があるため明らかに少し異なりますサーバーからダウンロードされた可能性があります