使用Glide加载到Imageview但延迟

时间:2017-09-06 09:13:38

标签: android firebase imageview android-glide

我使用Glide从Firebase加载我的ImageView。

当我运行我的应用程序时,我的ImageView会延迟(就像我的视频中的牙齿), https://www.youtube.com/watch?v=6Mj0Xq3M8n0

所以我想知道为什么会这样,这是我的代码:

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) {

                }
            });
        }
    }

如何解决我的问题?

2 个答案:

答案 0 :(得分:0)

尝试使用内置的FirebaseLoader图像加载器

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

答案 1 :(得分:0)

尝试以下方法添加占位符

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

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

或使用apply而不是setDefaultRequestOptions

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