Android Glide picture Load (load listener, load animation) _android

Source: Internet
Author: User
Tags int size xmlns

This example for you to share the Android glide image loading code for your reference, the specific content as follows

1. General usage

Glide.with
. Load (URL)
. into (view);

With in can put the context, activity, fragment. When the activity, fragment, glide will load the picture according to the life cycle. Activity is recommended for use.

2. Set a picture of load and load failure

Glide.with (context)
. Load (URL)
. Placeholder (r.drawable.loading)//placeholder is a picture in the load, you can put a GIF
. Error ( r.drawable.failed)//Failed picture
. into (view);

3. Add a picture fade effect

. Crossfade ()

4. Custom animation with Animate ()

Animations from a resource:

Back to the code, the first option is to pass an Android resource ID, an animation resource. A simple example is provided by every Android system: Slide-in-left (slide from left) animation, Android. R.anim.slide_in_left. The following code is the XML description of the animation:

<?xml version= "1.0" encoding= "Utf-8"?> <set xmlns:android= 
"http://schemas.android.com/apk/res/" Android "> 
  <translate android:fromxdelta=" -50%p "android:toxdelta=" 0 "
      android:duration=" @android: Integer/config_mediumanimtime "/> <alpha android:fromalpha= 0.0" android:toalpha= "1.0"
      android: Duration= "@android: Integer/config_mediumanimtime"/>
</set>

Of course you can create your own XML animation. For example, a small zoom animation, the picture just started small, and then gradually increased to the original size.

<?xml version= "1.0" encoding= "Utf-8"?> <set xmlns:android= 
"http://schemas.android.com/apk/res/" Android " 
   android:fillafter=" true ">

  <scale
    android:duration=" @android: Integer/config_ Longanimtime "
    android:fromxscale=" 0.1 "
    android:fromyscale=
    " 0.1 "android:pivotx=" 50% " Android:pivoty= "50%"
    android:toxscale= "1"
    android:toyscale= "1"/>
</set> 

These two animations are available to Glide builders:

Glide. With 
  (context).
  load (eatfoodyimages[0])
  . Animate (Android. R.anim.slide_in_left)//or r.anim.zoom_in
  . into (ImageView1);

When the picture is loaded from the network and ready to go, it slides from the left.

Animate by Custom class

This is simple, you only need to implement the void animate (View view) method. This view object is the entire target view. If it's a custom view, you need to find the child elements of your view and do the necessary animations.

Let's look at a simple example. Suppose you want to implement a progressive animation, you need to create such an animated object:

Viewpropertyanimation.animator animationobject = new Viewpropertyanimation.animator () { 
  @Override
  public void animate (View view) {
    //If it ' s a custom View class, cast it here
    //Then find subviews and do the animations
    //Here, we just use the entire view for the Fade animation
    view.setalpha (0f);

    Objectanimator Fadeanim = objectanimator.offloat (view, "Alpha", 0f, 1f);
    Fadeanim.setduration (2500);
    Fadeanim.start ();
  }
;

Next, you need to set the animation in the Glide request:

Glide. With 
  (context)
  . Load (eatfoodyimages[1])
  . Animate (Animationobject)
  . into (IMAGEVIEW2);

Of course, in animate (view view), you can do anything you want to do with the image in your animation object method. Be free to use your animation to create it.

If you want to implement in your custom view, you only need to create this view object and then create your own custom method in your custom view.

5. Add Load Complete monitor

Glide.with (showimgactivity.this).
   load (urlstring).
   centercrop ().
   error (r.drawable.failed)
   . Crossfade ()
   . into (The new Glidedrawableimageviewtarget (ImageView) {
 @Override public
   void Onresourceready ( Glidedrawable drawable, Glideanimation anim) {
   super.onresourceready (drawable, anim);
   Add some pictures here to load the completed operation
  }
)};

6. Picture caching mechanism

Glide caching Policy

Glide the disk cache and memory cache are turned on by default, and of course you can set a specific caching policy for a single picture.
Set picture not added to memory cache

Glide. With 
  (context)
  . Load (Eatfoodyimages[0])
  . Skipmemorycache (True)
  . Imageviewinternet);

Set pictures not to be added to the disk cache

Glide. With 
  (context).
  load (eatfoodyimages[0])
  . Diskcachestrategy (Diskcachestrategy.none)
  . Into (imageviewinternet);

Glide supports multiple disk caching policies:

Diskcachestrategy.none: Do not cache pictures
Diskcachestrategy.source: Caching Picture source files
Diskcachestrategy.result: Cached Modified picture
Diskcachestrategy.all: Caching All the pictures, default

Picture Load Priority

Glide supports setting priority for picture loading, high priority loading first, low priority after loading:

private void Loadimagewithhighpriority () { 
  Glide
    . With (context)
    . Load ( Usageexamplelistviewadapter.eatfoodyimages[0])
    . Priority (Priority.high)
    . into (Imageviewhero);

private void Loadimageswithlowpriority () { 
  Glide
    . With (context)
    . Load ( USAGEEXAMPLELISTVIEWADAPTER.EATFOODYIMAGES[1])
    . Priority (Priority.low)
    . into (imageviewlowprioleft);

  Glide. With
    (context)
    . Load (usageexamplelistviewadapter.eatfoodyimages[2])
    . Priority ( Priority.low)
    . into (imageviewlowprioright);
}

7. Load Fillet picture

/** * Round chart * Created by <lzh> on 2016/7/29. */public class Glidecircletransform extends Bitmaptransformation {public Glidecircletransform (context context) {s
  Uper (context);  @Override protected Bitmap transform (Bitmappool pool, Bitmap totransform, int outwidth, int outheight) {return
  Circlecrop (pool, totransform);
    private static Bitmap Circlecrop (Bitmappool pool, Bitmap source) {if (Source = null) return null;
    int size = Math.min (Source.getwidth (), Source.getheight ());
    int x = (Source.getwidth ()-size)/2;
    int y = (source.getheight ()-size)/2;
    TODO this could is acquired from the pool too Bitmap squared = Bitmap.createbitmap (source, x, y, size, size);
    Bitmap result = pool.get (size, size, Bitmap.Config.ARGB_8888);
    if (result = = null) {result = Bitmap.createbitmap (size, size, Bitmap.Config.ARGB_8888);
    } Canvas Canvas = new Canvas (result);
    Paint Paint = new Paint (); Paint.setshader (New Bitmapshader (squared, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
    Paint.setantialias (TRUE);
    float r = size/2f;
    Canvas.drawcircle (R, R, R, paint);
  return result;
  @Override public String getId () {return getclass (). GetName ();


 }
}

* * and then use it just add the words
. Transform (new glidecircletransform) * *

Glide.with (mcontext).
        load (IMAGEURL).
        Transform (new Glidecircletransform (Mcontext)). into
        ( Holder.imageview);


Precautions:

Can not be directly to the use of glide imageview set tag;

Because glide in loading pictures when the use of tag, will cause conflict, and error;

When you want to use the tag to write the logical code, you can do this

. Settag (r.string.xxx,xxx); Gettag (r.string.xxx);

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.