延迟加载无法正常工作
Lazy Loading not working properly
我正在 recyclerview 中显示联系人列表。我正在显示联系人个人资料图像,首先我从服务器加载这些图像,然后将这些图像存储在外部存储器中。
然后正在从外部存储加载图像。我可以看到加载的图像,但是当我滚动时,我可以看到一些图像一两秒钟,然后它们消失,我可以看到默认图像图标。
public class ContactAdapter extends RecyclerView.Adapter<ContactAdapter.ContactHolder> {
private List<Contact> contactList;
File myDir1;
private Activity mContext;
private Boolean fileExists;
private File file;
private static final int MY_PERMISSIONS_REQUEST_CALL= 20;
public ContactAdapter(Activity context, List<Contact> contactList) {
this.contactList = contactList;
this.mContext = context;
}
@Override
public ContactHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_layout,null);
ContactHolder mh = new ContactHolder(v);
return mh;
}
@Override
public void onBindViewHolder(final ContactHolder contactHolder, int i) {
final Contact contact = contactList.get(i);
// Log.e("Imagename",""+"http://xesoftwares.co.in/contactsapi/profile_images/85368a5bbd6cffba8a3aa202a80563a2.jpg");//+feedItem.getThumbnail());
Target target = new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
// your code here ...
bitmap = Bitmap.createScaledBitmap(bitmap,(int)(bitmap.getWidth()*0.8), (int)(bitmap.getHeight()*0.8), true);
contactHolder.thumbnail.setImageBitmap(bitmap);
Log.e("ProfileImage", contact.getmProfileImage());
SaveImages(bitmap, contact.getmProfileImage());
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
contactHolder.thumbnail.setImageDrawable(errorDrawable);
// do error handling as required
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
contactHolder.thumbnail.setImageDrawable(placeHolderDrawable);
}
};
contactHolder.thumbnail.setTag(target);
String url = ServiceUrl.getBaseUrl() + ServiceUrl.getImageUserUrl() + contact.getmProfileImage();
Log.e("url",url);
if(contact.getmProfileImage().equals(""))
{
file = new File("");
fileExists = file.exists();
contactHolder.thumbnail.setImageDrawable(ContextCompat.getDrawable(mContext, R.drawable.ic_account_circle_black_48dp));
}
else {
file = new File(Environment.getExternalStorageDirectory() + "/ContactProfileImages/" + contact.getmProfileImage());
fileExists = file.exists();
}
if(fileExists)
{
Log.e("fileExists",file.getAbsolutePath());
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeFile(file.getPath(), bmOptions);
contactHolder.thumbnail.setImageBitmap(bitmap);
}
else {
Log.e("Picasso",file.getAbsolutePath());
Picasso.with(mContext).load(url)
.error(R.drawable.ic_account_circle_black_24dp)
.placeholder(R.drawable.ic_account_circle_black_24dp)
.into(target);
}
contactHolder.title.setText(contact.getmUserName());
//feedListRowHolder.genre.setText(Html.fromHtml(feedItem.getGenre()));
}
@Override
public int getItemCount() {
return (null != contactList ? contactList.size() : 0);
}
public void SaveImages(Bitmap bitmap,String profileName)
{
try {
String root = Environment.getExternalStorageDirectory().getPath();
File myDir = new File(root +"/ContactProfileImages");
if (!myDir.exists()) {
myDir.mkdirs();
}
// String name = new Date().toString();=
String name = profileName;
File myDir1 = new File(myDir, name);
if(!myDir1.exists()) {
FileOutputStream out = new FileOutputStream(myDir1);
bitmap.compress(Bitmap.CompressFormat.PNG,100,out);
out.flush();
out.close();
}
} catch(Exception e){
// some action
}
//myDir1= imageFilePath1.getprofile();
}
public class ContactHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
protected CircleImageView thumbnail;
protected TextView title;
public ContactHolder(View view) {
super(view);
this.thumbnail = (CircleImageView) view.findViewById(R.id.thumbnail);
this.title = (TextView) view.findViewById(R.id.title);
view.setOnClickListener(this);
}
@Override
public void onClick(View v) {
final Contact contact = contactList.get(getAdapterPosition());
final Dialog dialog = new Dialog(mContext);
dialog.setCanceledOnTouchOutside(true);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.custom);
final Window window = dialog.getWindow();
WindowManager.LayoutParams wlp =window.getAttributes();
wlp.gravity = Gravity.CENTER_HORIZONTAL|Gravity.TOP;
wlp.y=320;
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
window.setAttributes(wlp);
// set the custom dialog components - text, image and button
TextView txtusername = (TextView) dialog.findViewById(R.id.txtusername);
TextView txtmobile = (TextView) dialog.findViewById(R.id.txtmobile);
TextView txtemail = (TextView) dialog.findViewById(R.id.txtemail);
txtusername.setText(contact.getmUserName());
txtemail.setText(contact.getmEmailId());
txtmobile.setText(contact.getmMobileNo());
SquareImageView image = (SquareImageView) dialog.findViewById(R.id.image);
ImageView image1 = (ImageView) dialog.findViewById(R.id.image1);
ImageView image2 = (ImageView) dialog.findViewById(R.id.image2);
ImageView image3 = (ImageView) dialog.findViewById(R.id.image3);
if(contact.getmProfileImage().equals(""))
{
image.setImageDrawable(ContextCompat.getDrawable(mContext,R.drawable.profile_icon));
}
else {
File file = new File(Environment.getExternalStorageDirectory() + "/ContactProfileImages/" + contact.getmProfileImage());
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeFile(file.getPath(), bmOptions);
image.setImageBitmap(bitmap);
}
image1.setImageResource(R.drawable.ic_call_black_24dp);
image2.setImageResource(R.drawable.ic_textsms_black_24dp);
image3.setImageResource(R.drawable.ic_email_black_24dp);
image2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Uri sms_uri = Uri.parse("smsto:" + contact.getmMobileNo());
Intent sms_intent = new Intent(Intent.ACTION_SENDTO, sms_uri);
mContext.startActivity(sms_intent);
dialog.dismiss();
}
});
image1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + contact.getmMobileNo()));
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
mContext.startActivity(intent);
dialog.dismiss();
}
});
image3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_EMAIL, new String[]{contact.getmEmailId()});
email.setType("message/rfc822");
mContext.startActivity(Intent.createChooser(email, "Choose an Email client :"));
}
});
Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
// if button is clicked, view all information custom dialog
dialogButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
((MainActivity)mContext).finish();
Intent intent = new Intent(mContext,DetailViewActivity.class);
intent.putExtra("contact",contact);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
mContext.startActivity(intent);
dialog.dismiss();
}
});
dialog.show();
}
}
}
编辑:当我们从服务器加载图像时,我搜索了与 picasso 库相同的问题,为此我从 SO 获得了解决方案:
recyclerView.setHasFixedSize(true);
recyclerView.setItemViewCacheSize(20);
recyclerView.setDrawingCacheEnabled(true);
recyclerView.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
这在我从服务器加载图像时有效,但是当我从外部存储加载图像时问题仍然存在。
有人可以帮忙吗?谢谢..
我可能会弄错,但你的问题可能是因为你使用了 Contact
,你通过 layout position 从你的数据集中得到了,但是因为你使用它是异步的在 onBitmapLoaded
回调中,位置可能已经在那个时候改变了。所以你只需要使用适配器位置如下:
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
// your code here ...
Contact c = contactList.get(contactHolder.getAdapterPosition());
bitmap = Bitmap.createScaledBitmap(bitmap,(int)(bitmap.getWidth()*0.8), (int)(bitmap.getHeight()*0.8), true);
contactHolder.thumbnail.setImageBitmap(bitmap);
Log.e("ProfileImage", c.getmProfileImage());
SaveImages(bitmap, c.getmProfileImage());
}
我不确定它是否有帮助,但让我们试试吧,我希望它会是:)
如果你只想加载图像那么为什么你需要存储在外部 storage.if 你必须存储用于任何其他目的然后存储它但是当你在 ImageView 中显示它时请使用任何第三个party api 与 Picasso or Universal Image Loader 一样,它执行延迟加载,还执行所有类型的缓存内存管理,还提供许多显示错误。
希望对您有所帮助
加载图像等操作应在后台线程上使用 AsyncTask 执行。
在您的应用程序中使用此代码class以在整个应用程序中应用设置。
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
// Use 1/8th of the available memory for this memory cache.
final int cacheSize = maxMemory / 8;
Picasso.Builder builder = new Picasso.Builder(getApplicationContext());
builder.memoryCache(new LruCache(cacheSize));
Picasso built = builder.build();
built.setIndicatorsEnabled(true);// it will show indicator where it downloaded from
built.setLoggingEnabled(false);
Picasso.setSingletonInstance(built);
现在将 url 传递给 Picasso 以将其加载到视图中。如果您正在处理大图像,您也可以调整图像大小以加快加载速度。
Picasso
.with(mContext)
.load(url)
.error(R.drawable.on_error)
.placeholder(R.drawable.place_holder)
//.resize(custom_width,custom_height) put custom_height to 0 if you want to maintain aspect ratio
.into(your_view);
检查每个图像有多少MB。例如:如果
图片是 1mb 或 500kb,下载图片需要更多时间
背景和下载后它将存储在 catche.That 意味着
只是第一次下载图片需要一些时间。
第二次,来自catche.It的那些图片将显示所有
几分之一秒内的图像。
使用 Volley - 更快,使用 Glide - 进行内存管理。
Glide
.with(context)
.load(rowItem.getPosteduserpostimage())
.asBitmap()
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.fitCenter()
.placeholder(R.drawable.load_image)
.error(R.drawable.cancel_image)
.into(holder.ivPostedImageNew);
参考
here
如何加载大尺寸 bitmap.You 需要缩小图像。
否则,当我上传每张图片时,我确实使用
library.It 将负责缩小大位图。例如: 5mb
图片将缩小到 180 kb。
检查这个 CropImage
库
缩小更大的尺寸 images.Even 如果用户不裁剪图像,只需
select裁剪时整个部分缩小5mb到
180kb.Image质量也不错
我认为没有裁剪你也可以使用下面的代码缩小image.You可以看到这段代码:
private void startCropImage() {
Intent intent = new Intent(this, CropImage.class);
intent.putExtra(CropImage.IMAGE_PATH, mFileTemp.getPath());
intent.putExtra(CropImage.SCALE, true);
intent.putExtra(CropImage.ASPECT_X, 0); //3 for aspect ratio
intent.putExtra(CropImage.ASPECT_Y, 0); //2 for aspect ratio
startActivityForResult(intent, REQUEST_CODE_CROP_IMAGE);
}
- 并且不要在 recyclerview 中使用嵌套滚动视图。
Then Loading the images from external storage. I can see the images
loaded, but as I scroll I can see some images for a second or two then
they get disappear and I can see the default image icon.
这是因为您在 picasso 中使用了 Target 回调。当您滚动列表时,回调会被调用得有点晚。只需删除目标并将图像视图用于毕加索,它应该可以正常工作。此外,您不需要像 Picasso 那样自行缓存位图。
public void onBindViewHolder(final ContactHolder contactHolder, int i) {
final Contact contact = contactList.get(i);
// Log.e("Imagename",""+"http://xesoftwares.co.in/contactsapi/profile_images/85368a5bbd6cffba8a3aa202a80563a2.jpg");//+feedItem.getThumbnail());
String url = ServiceUrl.getBaseUrl() + ServiceUrl.getImageUserUrl() + contact.getmProfileImage();
Log.e("url",url);
if(contact.getmProfileImage().equals("")) {
file = new File("");
fileExists = file.exists();
contactHolder.thumbnail.setImageDrawable(ContextCompat.getDrawable(mContext, R.drawable.ic_account_circle_black_48dp));
} else {
file = new File(Environment.getExternalStorageDirectory() + "/ContactProfileImages/" + contact.getmProfileImage());
fileExists = file.exists();
}
Log.e("Picasso",file.getAbsolutePath());
Picasso.with(mContext).load(url)
.error(R.drawable.ic_account_circle_black_24dp)
.placeholder(R.drawable.ic_account_circle_black_24dp)
.into(contactHolder.thumbnail);
contactHolder.title.setText(contact.getmUserName());
//feedListRowHolder.genre.setText(Html.fromHtml(feedItem.getGenre()));
}
我正在 recyclerview 中显示联系人列表。我正在显示联系人个人资料图像,首先我从服务器加载这些图像,然后将这些图像存储在外部存储器中。
然后正在从外部存储加载图像。我可以看到加载的图像,但是当我滚动时,我可以看到一些图像一两秒钟,然后它们消失,我可以看到默认图像图标。
public class ContactAdapter extends RecyclerView.Adapter<ContactAdapter.ContactHolder> {
private List<Contact> contactList;
File myDir1;
private Activity mContext;
private Boolean fileExists;
private File file;
private static final int MY_PERMISSIONS_REQUEST_CALL= 20;
public ContactAdapter(Activity context, List<Contact> contactList) {
this.contactList = contactList;
this.mContext = context;
}
@Override
public ContactHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_layout,null);
ContactHolder mh = new ContactHolder(v);
return mh;
}
@Override
public void onBindViewHolder(final ContactHolder contactHolder, int i) {
final Contact contact = contactList.get(i);
// Log.e("Imagename",""+"http://xesoftwares.co.in/contactsapi/profile_images/85368a5bbd6cffba8a3aa202a80563a2.jpg");//+feedItem.getThumbnail());
Target target = new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
// your code here ...
bitmap = Bitmap.createScaledBitmap(bitmap,(int)(bitmap.getWidth()*0.8), (int)(bitmap.getHeight()*0.8), true);
contactHolder.thumbnail.setImageBitmap(bitmap);
Log.e("ProfileImage", contact.getmProfileImage());
SaveImages(bitmap, contact.getmProfileImage());
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
contactHolder.thumbnail.setImageDrawable(errorDrawable);
// do error handling as required
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
contactHolder.thumbnail.setImageDrawable(placeHolderDrawable);
}
};
contactHolder.thumbnail.setTag(target);
String url = ServiceUrl.getBaseUrl() + ServiceUrl.getImageUserUrl() + contact.getmProfileImage();
Log.e("url",url);
if(contact.getmProfileImage().equals(""))
{
file = new File("");
fileExists = file.exists();
contactHolder.thumbnail.setImageDrawable(ContextCompat.getDrawable(mContext, R.drawable.ic_account_circle_black_48dp));
}
else {
file = new File(Environment.getExternalStorageDirectory() + "/ContactProfileImages/" + contact.getmProfileImage());
fileExists = file.exists();
}
if(fileExists)
{
Log.e("fileExists",file.getAbsolutePath());
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeFile(file.getPath(), bmOptions);
contactHolder.thumbnail.setImageBitmap(bitmap);
}
else {
Log.e("Picasso",file.getAbsolutePath());
Picasso.with(mContext).load(url)
.error(R.drawable.ic_account_circle_black_24dp)
.placeholder(R.drawable.ic_account_circle_black_24dp)
.into(target);
}
contactHolder.title.setText(contact.getmUserName());
//feedListRowHolder.genre.setText(Html.fromHtml(feedItem.getGenre()));
}
@Override
public int getItemCount() {
return (null != contactList ? contactList.size() : 0);
}
public void SaveImages(Bitmap bitmap,String profileName)
{
try {
String root = Environment.getExternalStorageDirectory().getPath();
File myDir = new File(root +"/ContactProfileImages");
if (!myDir.exists()) {
myDir.mkdirs();
}
// String name = new Date().toString();=
String name = profileName;
File myDir1 = new File(myDir, name);
if(!myDir1.exists()) {
FileOutputStream out = new FileOutputStream(myDir1);
bitmap.compress(Bitmap.CompressFormat.PNG,100,out);
out.flush();
out.close();
}
} catch(Exception e){
// some action
}
//myDir1= imageFilePath1.getprofile();
}
public class ContactHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
protected CircleImageView thumbnail;
protected TextView title;
public ContactHolder(View view) {
super(view);
this.thumbnail = (CircleImageView) view.findViewById(R.id.thumbnail);
this.title = (TextView) view.findViewById(R.id.title);
view.setOnClickListener(this);
}
@Override
public void onClick(View v) {
final Contact contact = contactList.get(getAdapterPosition());
final Dialog dialog = new Dialog(mContext);
dialog.setCanceledOnTouchOutside(true);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.custom);
final Window window = dialog.getWindow();
WindowManager.LayoutParams wlp =window.getAttributes();
wlp.gravity = Gravity.CENTER_HORIZONTAL|Gravity.TOP;
wlp.y=320;
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
window.setAttributes(wlp);
// set the custom dialog components - text, image and button
TextView txtusername = (TextView) dialog.findViewById(R.id.txtusername);
TextView txtmobile = (TextView) dialog.findViewById(R.id.txtmobile);
TextView txtemail = (TextView) dialog.findViewById(R.id.txtemail);
txtusername.setText(contact.getmUserName());
txtemail.setText(contact.getmEmailId());
txtmobile.setText(contact.getmMobileNo());
SquareImageView image = (SquareImageView) dialog.findViewById(R.id.image);
ImageView image1 = (ImageView) dialog.findViewById(R.id.image1);
ImageView image2 = (ImageView) dialog.findViewById(R.id.image2);
ImageView image3 = (ImageView) dialog.findViewById(R.id.image3);
if(contact.getmProfileImage().equals(""))
{
image.setImageDrawable(ContextCompat.getDrawable(mContext,R.drawable.profile_icon));
}
else {
File file = new File(Environment.getExternalStorageDirectory() + "/ContactProfileImages/" + contact.getmProfileImage());
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeFile(file.getPath(), bmOptions);
image.setImageBitmap(bitmap);
}
image1.setImageResource(R.drawable.ic_call_black_24dp);
image2.setImageResource(R.drawable.ic_textsms_black_24dp);
image3.setImageResource(R.drawable.ic_email_black_24dp);
image2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Uri sms_uri = Uri.parse("smsto:" + contact.getmMobileNo());
Intent sms_intent = new Intent(Intent.ACTION_SENDTO, sms_uri);
mContext.startActivity(sms_intent);
dialog.dismiss();
}
});
image1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + contact.getmMobileNo()));
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
mContext.startActivity(intent);
dialog.dismiss();
}
});
image3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_EMAIL, new String[]{contact.getmEmailId()});
email.setType("message/rfc822");
mContext.startActivity(Intent.createChooser(email, "Choose an Email client :"));
}
});
Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
// if button is clicked, view all information custom dialog
dialogButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
((MainActivity)mContext).finish();
Intent intent = new Intent(mContext,DetailViewActivity.class);
intent.putExtra("contact",contact);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
mContext.startActivity(intent);
dialog.dismiss();
}
});
dialog.show();
}
}
}
编辑:当我们从服务器加载图像时,我搜索了与 picasso 库相同的问题,为此我从 SO 获得了解决方案:
recyclerView.setHasFixedSize(true);
recyclerView.setItemViewCacheSize(20);
recyclerView.setDrawingCacheEnabled(true);
recyclerView.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
这在我从服务器加载图像时有效,但是当我从外部存储加载图像时问题仍然存在。
有人可以帮忙吗?谢谢..
我可能会弄错,但你的问题可能是因为你使用了 Contact
,你通过 layout position 从你的数据集中得到了,但是因为你使用它是异步的在 onBitmapLoaded
回调中,位置可能已经在那个时候改变了。所以你只需要使用适配器位置如下:
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
// your code here ...
Contact c = contactList.get(contactHolder.getAdapterPosition());
bitmap = Bitmap.createScaledBitmap(bitmap,(int)(bitmap.getWidth()*0.8), (int)(bitmap.getHeight()*0.8), true);
contactHolder.thumbnail.setImageBitmap(bitmap);
Log.e("ProfileImage", c.getmProfileImage());
SaveImages(bitmap, c.getmProfileImage());
}
我不确定它是否有帮助,但让我们试试吧,我希望它会是:)
如果你只想加载图像那么为什么你需要存储在外部 storage.if 你必须存储用于任何其他目的然后存储它但是当你在 ImageView 中显示它时请使用任何第三个party api 与 Picasso or Universal Image Loader 一样,它执行延迟加载,还执行所有类型的缓存内存管理,还提供许多显示错误。
希望对您有所帮助
加载图像等操作应在后台线程上使用 AsyncTask 执行。
在您的应用程序中使用此代码class以在整个应用程序中应用设置。
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
// Use 1/8th of the available memory for this memory cache.
final int cacheSize = maxMemory / 8;
Picasso.Builder builder = new Picasso.Builder(getApplicationContext());
builder.memoryCache(new LruCache(cacheSize));
Picasso built = builder.build();
built.setIndicatorsEnabled(true);// it will show indicator where it downloaded from
built.setLoggingEnabled(false);
Picasso.setSingletonInstance(built);
现在将 url 传递给 Picasso 以将其加载到视图中。如果您正在处理大图像,您也可以调整图像大小以加快加载速度。
Picasso
.with(mContext)
.load(url)
.error(R.drawable.on_error)
.placeholder(R.drawable.place_holder)
//.resize(custom_width,custom_height) put custom_height to 0 if you want to maintain aspect ratio
.into(your_view);
检查每个图像有多少MB。例如:如果 图片是 1mb 或 500kb,下载图片需要更多时间 背景和下载后它将存储在 catche.That 意味着 只是第一次下载图片需要一些时间。
第二次,来自catche.It的那些图片将显示所有 几分之一秒内的图像。
使用 Volley - 更快,使用 Glide - 进行内存管理。
Glide .with(context) .load(rowItem.getPosteduserpostimage()) .asBitmap() .diskCacheStrategy(DiskCacheStrategy.SOURCE) .fitCenter() .placeholder(R.drawable.load_image) .error(R.drawable.cancel_image) .into(holder.ivPostedImageNew);
参考 here 如何加载大尺寸 bitmap.You 需要缩小图像。
否则,当我上传每张图片时,我确实使用 library.It 将负责缩小大位图。例如: 5mb 图片将缩小到 180 kb。
检查这个 CropImage 库 缩小更大的尺寸 images.Even 如果用户不裁剪图像,只需 select裁剪时整个部分缩小5mb到 180kb.Image质量也不错
我认为没有裁剪你也可以使用下面的代码缩小image.You可以看到这段代码:
private void startCropImage() {
Intent intent = new Intent(this, CropImage.class);
intent.putExtra(CropImage.IMAGE_PATH, mFileTemp.getPath());
intent.putExtra(CropImage.SCALE, true);
intent.putExtra(CropImage.ASPECT_X, 0); //3 for aspect ratio
intent.putExtra(CropImage.ASPECT_Y, 0); //2 for aspect ratio
startActivityForResult(intent, REQUEST_CODE_CROP_IMAGE);
}
- 并且不要在 recyclerview 中使用嵌套滚动视图。
Then Loading the images from external storage. I can see the images loaded, but as I scroll I can see some images for a second or two then they get disappear and I can see the default image icon.
这是因为您在 picasso 中使用了 Target 回调。当您滚动列表时,回调会被调用得有点晚。只需删除目标并将图像视图用于毕加索,它应该可以正常工作。此外,您不需要像 Picasso 那样自行缓存位图。
public void onBindViewHolder(final ContactHolder contactHolder, int i) {
final Contact contact = contactList.get(i);
// Log.e("Imagename",""+"http://xesoftwares.co.in/contactsapi/profile_images/85368a5bbd6cffba8a3aa202a80563a2.jpg");//+feedItem.getThumbnail());
String url = ServiceUrl.getBaseUrl() + ServiceUrl.getImageUserUrl() + contact.getmProfileImage();
Log.e("url",url);
if(contact.getmProfileImage().equals("")) {
file = new File("");
fileExists = file.exists();
contactHolder.thumbnail.setImageDrawable(ContextCompat.getDrawable(mContext, R.drawable.ic_account_circle_black_48dp));
} else {
file = new File(Environment.getExternalStorageDirectory() + "/ContactProfileImages/" + contact.getmProfileImage());
fileExists = file.exists();
}
Log.e("Picasso",file.getAbsolutePath());
Picasso.with(mContext).load(url)
.error(R.drawable.ic_account_circle_black_24dp)
.placeholder(R.drawable.ic_account_circle_black_24dp)
.into(contactHolder.thumbnail);
contactHolder.title.setText(contact.getmUserName());
//feedListRowHolder.genre.setText(Html.fromHtml(feedItem.getGenre()));
}