Z. Joe
Z. Joe

Reputation: 233

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. 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:

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. 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.

@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:

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

Upvotes: 1

Views: 5283

Answers (2)

Victor Gomes
Victor Gomes

Reputation: 128

I'm not sure if you can speed up 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
                }
            });

Upvotes: 0

rciovati
rciovati

Reputation: 28063

Glide has a dedicated API for preloading images:

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

The Javadoc says:

Preloads the resource into the cache using Target.SIZE_ORIGINAL as the target width and height. Equivalent to calling preload(int, int) with Target.SIZE_ORIGINAL as the width and height.

Upvotes: 4

Related Questions