我想制作一个Android List Widget并且工作正常,但速度非常慢,因为包含imgages并且图像加载非常慢。我想要一个asyncron图像加载,但这不起作用。
Image Asyncron Loading class:
public class LoadBitmap extends AsyncTask<String, Void, Bitmap> {
private RemoteViews mViews;
public LoadBitmap(RemoteViews views){
this.mViews = views;
}
@Override
protected Bitmap doInBackground(String... params) {
try {
return decodeSampledBitmapFromResource(params[0], 1080, 1920);
//NOTE: it is not thread-safe to set the ImageView from inside this method. It must be done in onPostExecute()
} catch (Exception e) {
Log.e("ImageDownload", "Download failed: " + e.getMessage());
}
return null;
}
@Override
public void onPostExecute(Bitmap bitmap){
super.onPostExecute(bitmap);
if(bitmap != null) {
mViews.setImageViewBitmap(R.id.widget_image, bitmap);
}
}
private Bitmap decodeSampledBitmapFromResource(String path, int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
options.inSampleSize = 4;
BitmapFactory.decodeFile(path);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(path, options);
}
private int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) >= reqHeight
&& (halfWidth / inSampleSize) >= reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
}
ListWidgetFactory类whitch构建ListView项:
public class ListWidgetFactory implements RemoteViewsService.RemoteViewsFactory {
private ArrayList<Note> mNotes;
private Context mContext = null;
private SimpleDateFormat mDateFormat;
private Colors mColor;
private int labelID;
public ListWidgetFactory(Context context, Intent intent) {
this.mContext = context;
//int mAppWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
labelID = intent.getIntExtra(ListAppWidget.LIST_WIDGET_LABEL_ID, -1);
mNotes = new DatabaseManager(context).getSortedNotes(0, 0, labelID);
mColor = new Colors(context);
mDateFormat = new SimpleDateFormat(TIME_FORMAT, Locale.getDefault());
}
@Override
public void onCreate() {
// no-op
}
@Override
public void onDestroy() {
// no-op
}
@Override
public int getCount() {
return (mNotes.size());
}
@Override
public RemoteViews getViewAt(int position) {
RemoteViews card = new RemoteViews(mContext.getPackageName(), R.layout.item_widget_note);
if(mNotes != null && position < mNotes.size()) {
Note note = mNotes.get(position);
card.setInt(R.id.widget_card, "setBackgroundColor", mColor.getCardBackgroundColor(note.getBgColor()));
if (!note.getTitle().isEmpty()) {
card.setViewVisibility(R.id.widget_title, VISIBLE);
card.setTextViewText(R.id.widget_title, note.getTitle());
card.setTextColor(R.id.widget_title, mColor.getPrimaryTextColor(note.getTextColor()));
} else {
card.setViewVisibility(R.id.widget_title, GONE);
}
if (!note.getText().getText().isEmpty()) {
card.setViewVisibility(R.id.widget_content, VISIBLE);
card.setTextViewText(R.id.widget_content, note.getText().getText());
card.setTextColor(R.id.widget_content, mColor.getSecondaryTextColor(note.getTextColor()));
} else {
card.setViewVisibility(R.id.widget_content, GONE);
}
if (!note.getImagesList().getList().isEmpty()) {
card.setViewVisibility(R.id.widget_image, VISIBLE);
/*Bitmap bitmap= BitmapFactory.decodeFile(note.getImagesList().getElement(0).getSrc().split(":")[1]);
if (bitmap != null){
card.setImageViewBitmap(R.id.widget_image, bitmap);
}*/
new LoadBitmap(card).execute(note.getImagesList().getElement(0).getSrc().split(":")[1]);
} else {
card.setViewVisibility(R.id.widget_image, GONE);
}
if (!note.getRecord().getText().isEmpty()) {
card.setViewVisibility(R.id.widget_record, VISIBLE);
String recDate = note.getRecord().getText().split("REC_")[1];
card.setTextViewText(R.id.widget_record, recDate.substring(0, 4) + "." + recDate.substring(4, 6) + "." + recDate.substring(6, 8));
card.setTextColor(R.id.widget_record, mColor.getSecondaryTextColor(note.getTextColor()));
} else {
card.setViewVisibility(R.id.widget_record, GONE);
}
if (note.getList() != null && !note.getList().getList().isEmpty()) {
card.setViewVisibility(R.id.widget_list, VISIBLE);
card.removeAllViews(R.id.widget_list);
int db = 0;
ArrayList<NoteChecked> chList = note.getList().getList();
for (int i = 0; i < chList.size() && db < 6; ++i) {
if (!chList.get(i).getChecked()) {
RemoteViews listElement = new RemoteViews(mContext.getPackageName(), R.layout.list_group);
listElement.setTextViewText(R.id.list_title, chList.get(i).getLabel());
listElement.setTextColor(R.id.list_title, mColor.getSecondaryTextColor(note.getTextColor()));
listElement.setTextViewCompoundDrawablesRelative(R.id.list_title, R.drawable.ic_check_box_unchecked_grey_16dp, 0, 0, 0);
card.addView(R.id.widget_list, listElement);
++db;
}
}
for (int i = 0; i < chList.size() && db < 6; ++i) {
if (chList.get(i).getChecked()) {
RemoteViews listElement = new RemoteViews(mContext.getPackageName(), R.layout.list_group);
listElement.setTextViewText(R.id.list_title, chList.get(i).getLabel());
listElement.setTextColor(R.id.list_title, mColor.getSecondaryTextColor(note.getTextColor()));
listElement.setTextViewCompoundDrawablesRelative(R.id.list_title, R.drawable.ic_check_box_grey_16dp, 0, 0, 0);
card.addView(R.id.widget_list, listElement);
++db;
}
}
} else {
card.setViewVisibility(R.id.widget_list, GONE);
}
if (note.getImagesList().getSize() > 0) {
card.setViewVisibility(R.id.widget_image, VISIBLE);
} else {
card.setViewVisibility(R.id.widget_image, GONE);
}
try {
if (note.getReminder().charAt(0) == 'T') {
card.setViewVisibility(R.id.widget_time, VISIBLE);
card.setTextViewText(R.id.widget_time, mDateFormat.format(DatabaseManager.mFormat.parse(note.getReminder().substring(1))));
card.setTextColor(R.id.widget_time, mColor.getSecondaryTextColor(note.getTextColor()));
} else {
card.setViewVisibility(R.id.widget_time, GONE);
}
} catch (ParseException e) {
e.printStackTrace();
}
Intent i = new Intent();
i.putExtra(ListAppWidget.EXTRA_WORD, mNotes.get(position).getID());
card.setOnClickFillInIntent(R.id.widget_card, i);
}
return (card);
}
@Override
public RemoteViews getLoadingView() {
return (null);
}
@Override
public int getViewTypeCount() {
return (1);
}
@Override
public long getItemId(int position) {
return (position);
}
@Override
public boolean hasStableIds() {
return (true);
}
@Override
public void onDataSetChanged() {
mNotes = new DatabaseManager(mContext).getSortedNotes(0, 0, labelID);
}
}
答案 0 :(得分:-1)
您应该使用Picasso。它是Android的强大库。您无需下载图像,只需将链接传递给lib,如下所示:
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);