Use Glide load into Imageview but delay

2019-03-04 02:52发布

问题:

I use Glide to load my ImageView from Firebase.

When I am running my application, my ImageView will delay(like the tooth in my video), https://www.youtube.com/watch?v=6Mj0Xq3M8n0

so I want to know why is this so,and this is my code:

private void setTooth() {
        for(int j=0;j<tooth.length;j++) {
            final int finalJ = j;
            toothRef.child(j+1+"").addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    if(dataSnapshot.exists()){
                        if(dataSnapshot.getValue().toString().trim().equals("b"))
                        {
                            imageRef.child("tooth_dirty").child(finalJ%16+1+"").addValueEventListener(new ValueEventListener() {
                                @Override
                                public void onDataChange(final DataSnapshot dataSnapshot) {
                                    Glide.with(Home_Activity.this)
                                            .load(Uri.parse(dataSnapshot.getValue().toString()))
                                            .diskCacheStrategy(DiskCacheStrategy.ALL)
                                            .into(tooth[finalJ]);
                                }

                                @Override
                                public void onCancelled(FirebaseError firebaseError) {

                                }
                            });
                        }
                        else
                        {
                            imageRef.child("tooth_clean").child(finalJ%16+1+"").addValueEventListener(new ValueEventListener() {
                                @Override
                                public void onDataChange(DataSnapshot dataSnapshot) {
                                    Glide.with(Home_Activity.this)
                                            .load(Uri.parse(dataSnapshot.getValue().toString()))
                                            .diskCacheStrategy(DiskCacheStrategy.ALL)
                                            .into(tooth[finalJ]);
                                }

                                @Override
                                public void onCancelled(FirebaseError firebaseError) {

                                }
                            });
                        }
                    }
                    else
                    {
                        for(int i=0;i<tooth.length;i++)
                            toothRef.child(i+1+"").setValue("g");
                        setTooth();
                    }
                }

                @Override
                public void onCancelled(FirebaseError firebaseError) {

                }
            });
        }
    }

How can I solve my problem:(

回答1:

try this use placeholder() to display a temparay image whole loading image using glide

Glide.with(Home_Activity.this)
  .dontAnimate()
  .placeholder(R.drawable.ic_placeholder)
  .load(Uri.parse(dataSnapshot.getValue().toString()))
  .diskCacheStrategy(DiskCacheStrategy.ALL)
  .into(tooth[finalJ]);


回答2:

try using the inbuilt FirebaseLoader image loader

StorageReference storageReference = //make a Reference to your Image
Glide.with(context)
   .using(new FirebaseImageLoader())
   .load(storageReference)
   .into(ImageView);


回答3:

Try the following to add placeholder

RequestOptions requestOptions = new RequestOptions();
requestOptions.placeholder(R.mipmap.ic_launcher);
requestOptions.error(R.drawable.error_img);

Glide.with(this)
            .setDefaultRequestOptions(requestOptions)
            .load("")
            .into(imageViewPlaceholder);

or use apply instead of setDefaultRequestOptions

Glide.with(MainActivity.this)
        .load(url)
        .apply(requestOptions)
        .into(imageview);