简体   繁体   English

如何更快地加载(库)Glide Images? 安卓系统

[英]How to load (libary)Glide Images quicker? Android

How can I make the images download first and place it in cach and when someone pressed button then the image will directly show without delay. 我如何首先下载图像并将其放入缓存中,当有人按下按钮时,图像将立即显示而不会延迟。

Why? 为什么? Everytime I click on the button now I have a delay of a few seconds, because they loading from the internet. 现在,每次我单击按钮时,都会有几秒钟的延迟,因为它们是从Internet加载的。 It takes times, that's why I want them to be loaded and cached first. 这需要时间,这就是为什么我希望它们先被加载和缓存。 So when someone clicked on the button it will show directly in one second. 因此,当有人单击该按钮时,它将在一秒钟内直接显示。

Java: Java:

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.squareup.picasso.Picasso;

import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {

private int a;
ImageView ivImageFromUrl;
@Override

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ivImageFromUrl=(ImageView)findViewById(R.id.iv_image_from_url);
}

public void buttonOnClick(View v)  {
    // do something when the button is clicked
    final Button button = (Button) v;
    button.setEnabled(false);

    Timer buttonTimer = new Timer();
    buttonTimer.schedule(new TimerTask() {

        @Override
        public void run() {
            runOnUiThread(new Runnable() {

Don't look at the delay of 1000 ms, it's not important because I want a delay of 1 second every time someone clicked on the button. 不要看1000 ms的延迟,这并不重要,因为我希望每次有人单击按钮时都延迟1秒。 I did the delay also to let it look like they are loading but it's not good...Sometimes I must wait 5 seconds for the images to get loaded. 我也做了延迟,以使其看起来像正在加载,但这不是很好。有时我必须等待5秒才能加载图像。

@Override
                public void run() {
                    button.setEnabled(true);
                }
            });
        }
    }, 1000);
    ((Button) v).setText("Random image");
    a = (int) (Math.random() * 4);

    switch (a) {
        case 0:

            Glide.with(this).load("imagelink").into(ivImageFromUrl);
            break;
        case 1:
            Glide.with(this).load("imagelink").into(ivImageFromUrl);
            break;
        case 2:
            Glide.with(this).load("imagelink").into(ivImageFromUrl);
            break;
        case 3:
            Glide.with(this).load("imagelink").into(ivImageFromUrl);
            break;
        case 4:
            Glide.with(this).load("imagelink").into(ivImageFromUrl);
            break;
    }
 }
}

XML: XML:

 <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    android:id="@+id/textView2" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Image text"
    android:id="@+id/textView"
    android:layout_below="@+id/textView2"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="82dp" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Image"
    android:id="@+id/button"
    android:onClick="buttonOnClick"
    android:layout_below="@+id/textView"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="41dp" />

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/iv_image_from_url"
    android:layout_alignParentBottom="true"
    android:layout_alignRight="@+id/textView"
    android:layout_alignEnd="@+id/textView"
    android:layout_marginBottom="106dp"
    android:layout_toRightOf="@+id/textView"
    android:layout_toEndOf="@+id/textView" />

</RelativeLayout>

Glide has a dedicated API for preloading images: Glide具有专用于预加载图像的API:

Glide.with(this).load(uri).preload();

The Javadoc says: Javadoc说:

Preloads the resource into the cache using Target.SIZE_ORIGINAL as the target width and height. 使用Target.SIZE_ORIGINAL作为目标宽度和高度将资源Target.SIZE_ORIGINAL到缓存中。 Equivalent to calling preload(int, int) with Target.SIZE_ORIGINAL as the width and height. 等效于使用Target.SIZE_ORIGINAL作为宽度和高度调用preload(int, int)

I'm not sure if you can speed up Glide. 我不确定您是否可以加快Glide的速度。

But if you are displaying a loading to the user, you can try to add a listener and only show the images once all of them are ready. 但是,如果要向用户显示负载,则可以尝试添加侦听器,并仅在所有图像准备就绪后才显示图像。

try this: 尝试这个:

Glide.with(this).load("imagelink").into(new GlideDrawableImageViewTarget(ivImageFromUrl) {
                @Override
                public void onLoadFailed(Exception e, Drawable errorDrawable) {

                  //Handle error
                }

                @Override
                public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> animation) {

                    //Hide loading, show image
                }
            });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM