我正在使用glide 3.7从Firebase存储中的recyclerview中加载图像。当将新图像添加到recyclerview并调用notifiydatasetchanged时,recyclerview中的所有现有图像都会闪烁并再次重新加载。调用notifydatasetchanged时如何避免图像闪烁。 这是我的适配器类
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> {
private static final String TAG = "RecyclerAdapter";
private Context mContext;
private List<ModelGridItems> gridItemsList;
private boolean fromService;
public RecyclerAdapter(Context context, List<ModelGridItems> list, boolean fromservice) {
gridItemsList = list;
mContext = context;
fromService = fromservice;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.grid_items, parent, false);
return new RecyclerAdapter.MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
try {
holder.bind(gridItemsList.get(position));
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType(int position) {
return position;
}
@Override
public int getItemCount() {
if (gridItemsList == null) {
return 0;
}
return gridItemsList.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
ImageView btnAction;
TextView tvParticipantName;
ImageView ivParticipantImage;
TextView tvConnection;
MyViewHolder(View itemView) {
super(itemView);
ivParticipantImage = itemView.findViewById(R.id.profile_image);
btnAction = itemView.findViewById(R.id.iv_more);
tvParticipantName = itemView.findViewById(R.id.tv_username);
tvParticipantName.setTypeface(BaseActivity.typefaceCabinBold);
tvConnection = itemView.findViewById(R.id.tv_status);
tvConnection.setTypeface(BaseActivity.typefaceCabinBold);
}
void bind(final ModelGridItems item) {
tvParticipantName.setText(item.getParticipantName());
if (item.getIsConnecting()) {
tvConnection.setVisibility(View.VISIBLE);
} else {
tvConnection.setVisibility(View.GONE);
}
if (item.getParticipantId().equals(BaseActivity.mUniqueId)) {
btnAction.setVisibility(View.INVISIBLE);
}
if (BaseActivity.mUniqueId.equals(item.getParticipantId())) {
loadProfileImage();
} else {
StorageReference storageReference = BaseActivity.storageReference.child(AppConfig.profileBucket + item.getParticipantId() + AppConfig.ImageExtension);
SharedMethods.loadFromCloudCircular(mContext, ivParticipantImage, storageReference);
}
btnAction.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showAnchorMenu(btnAction, item.getParticipantId());
}
});
ivParticipantImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (fromService) {
Intent groupCallIntent = new Intent(mContext, GroupCall.class);
groupCallIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
groupCallIntent.putParcelableArrayListExtra("GridItems", (ArrayList<? extends Parcelable>) gridItemsList);
mContext.startActivity(groupCallIntent);
} else {
SharedMethods.showLargeImageFromCloud(mContext, item.getParticipantId());
}
}
});
}
private void loadProfileImage() {
if (SharedMethods.isPermissionGranted(SharedMethods.PERMISSION_WRITE_STORAGE, mContext)) {
SharedMethods.loadFromLocalStorageCircular((Activity) mContext, ivParticipantImage, SharedMethods.profileImagePath());
} else {
StorageReference storageReference = BaseActivity.storageReference.child(AppConfig.profileBucket + BaseActivity.mUniqueId + AppConfig.ImageExtension);
SharedMethods.loadFromCloudCircular(mContext, ivParticipantImage, storageReference);
}
}
}
}
此方法会从Firebase存储中提取图像
public static void loadFromCloudCircular(final Context context, final ImageView imageView, StorageReference storageReference) {
Glide.with(context)
.using(new FirebaseImageLoader())
.load(storageReference)
.skipMemoryCache(true)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.placeholder(R.drawable.unknown_image)
.transform(new CircularTransform(context))
.into(new SimpleTarget<GlideDrawable>() {
@Override
public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
imageView.setImageDrawable(resource);
Dashboard.isCircularLoaded = true;
}
});
}
我尝试了诸如setHasStableIds(true)之类的其他选项,但是这将项目添加到现有项目的顶部,并将动画添加到其位置。还尝试了如下所示的itemanimator,但图像始终闪烁。有帮助吗?
ItemAnimator animator = rv.getItemAnimator();
if (animator instanceof SimpleItemAnimator) {
((SimpleItemAnimator) animator).setSupportsChangeAnimations(false);
}
答案 0 :(得分:0)
建议1:使用毕加索代替Glide Picasso Square Image Viewer
我不喜欢Glide,而毕加索的语法与此类似。
建议2:请勿使用1MB以上的图片
如果在手机的250dp高度查看图像,它将占用大量内存,并且对图像质量没有影响。如果您是从应用本身上传的图片,最好减小图像的大小并将其发送到Firebase。 如果您需要缩小图像尺寸的帮助,请查看此链接。Codota link to compress images to required size
重要建议:暂时不要使用动画,并检查动画是否仍然闪烁
尝试这些建议,并检查是否有帮助。