Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Picasso Images are loading slow in android, why?

The images from the Picasso libery are loading in the emulater after a few clicks and very slow. Why are they loading so slow? And what do I have to do to make them load faster. I tried it with png files and jpg.

Java code

 private int a;
 ImageView ivImageFromUrl;


 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
    Button button = (Button) v;

    a = (byte) (Math.random() * 5);

    switch (a) {
        case 1:
            Picasso.with(getApplicationContext()).load("http://icons.iconarchive.com/icons/crountch/one-piece-jolly-roger/72/Luffys-flag-2-icon.png").into(ivImageFromUrl);
          break;
        case 2:
            Picasso.with(getApplicationContext()).load("http://i40.tinypic.com/2i8xait.jpg").into(ivImageFromUrl);
            break;
        case 3:
            Picasso.with(getApplicationContext()).load("http://i41.tinypic.com/2i8xahh.jpg").into(ivImageFromUrl);
            break;
        case 4:
            Picasso.with(getApplicationContext()).load("http://i42.tinypic.com/2i8xahk.jpg").into(ivImageFromUrl);
            break;
        case 5:
            Picasso.with(getApplicationContext()).load("http://i40.tinypic.com/2i8xagp.jpg").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" />

like image 413
Z. Joe Avatar asked May 23 '16 19:05

Z. Joe


People also ask

How does Picasso work on Android?

Picasso is open source and one of the widely used image download libraries in Android. It is created and maintained by Square. It is among the powerful image download and caching library for Android. Picasso simplifies the process of loading images from external URLs and displays them on your application.

How do I load images from Firebase to Picasso?

StorageReference load = getImage(id); load. getDownloadUrl(). addOnSuccessListener(new OnSuccessListener<Uri>() { @Override public void onSuccess(Uri uri) { downloadUri = uri; } }). addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { Log.


1 Answers

If your images are too large, then it may take some time.

If you need to show profile picture, whose size in your layout is 350x350, then why load full image. Re-size the image as per your requirement.

Picasso.with(getApplicationContext()).load(filePath).resize(400,400).centerCrop().into(imageView);
like image 62
Anuj Sharma Avatar answered Sep 25 '22 18:09

Anuj Sharma