欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android Glide圖片加載(加載監(jiān)聽、加載動(dòng)畫)

 更新時(shí)間:2016年11月29日 14:55:13   作者:zhihui_520  
這篇文章主要為大家詳細(xì)介紹了Android Glide圖片加載的具體實(shí)現(xiàn)方法,包括加載監(jiān)聽、加載動(dòng)畫,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Android Glide圖片加載的具體代碼,供大家參考,具體內(nèi)容如下

1.普通用法

Glide.with(context)
.load(url)
.into(view);

with中可以放context、activity、fragment。。;當(dāng)放activity、fragment時(shí)glide會(huì)根據(jù)生命周期來(lái)加載圖片。推薦使用activity。

2.設(shè)置加載中和加載失敗的圖片

Glide.with(context)
.load(url)
.placeholder(R.drawable.loading) //占位符 也就是加載中的圖片,可放個(gè)gif
.error(R.drawable.failed) //失敗圖片
.into(view);

3.添加圖片淡入加載的效果

.crossFade()

4.用 animate() 自定義動(dòng)畫

從資源中的動(dòng)畫:

回到代碼,第一個(gè)選項(xiàng)是傳一個(gè) Android 資源 id,即動(dòng)畫的資源。一個(gè)簡(jiǎn)單的例子是每個(gè) Android 系統(tǒng)都提供的:slide-in-left(從左滑入)動(dòng)畫, android.R.anim.slide_in_left 。下面這段代碼是這個(gè)動(dòng)畫的 XML 描述:

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

當(dāng)然你可以創(chuàng)建你自己的 XML 動(dòng)畫。比如一個(gè)小的縮放動(dòng)畫,圖片剛開始小的,然后逐漸增大到原尺寸。

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

這兩個(gè)動(dòng)畫都可以用到 Glide 建造者中:

Glide 
  .with( context )
  .load( eatFoodyImages[0] )
  .animate( android.R.anim.slide_in_left ) // or R.anim.zoom_in
  .into( imageView1 );

在圖片從網(wǎng)絡(luò)加載完并準(zhǔn)備好之后將從左邊滑入。

通過(guò)自定義類實(shí)現(xiàn)動(dòng)畫

這個(gè)很簡(jiǎn)單,你只需實(shí)現(xiàn) void animate(View view) 方法。這個(gè)視圖對(duì)象是整個(gè) target 視圖。如果它是一個(gè)自定義的視圖,你要找到你的視圖的子元素,并且做些必要的動(dòng)畫。

來(lái)看個(gè)簡(jiǎn)單的例子。假設(shè)你想要實(shí)現(xiàn)一個(gè)漸現(xiàn)動(dòng)畫,你得需要?jiǎng)?chuàng)建這樣的動(dòng)畫對(duì)象:

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();
  }
};

接下來(lái),你需要在 Glide 請(qǐng)求中去設(shè)置這個(gè)動(dòng)畫:

Glide 
  .with( context )
  .load( eatFoodyImages[1] )
  .animate( animationObject )
  .into( imageView2 );

當(dāng)然,在 animate(View view) 中你的動(dòng)畫對(duì)象方法中, 你可以做任何你想要對(duì)視圖做的事情。自由的用你的動(dòng)畫創(chuàng)建吧。

如果你要在你的自定義視圖中實(shí)現(xiàn),你只需要?jiǎng)?chuàng)建這個(gè)視圖對(duì)象,然后在你的自定義視圖中創(chuàng)建你的自定義方法。

5.添加加載完成監(jiān)聽

Glide.with(ShowImgActivity.this)
   .load(urlString)
   .centerCrop()
   .error(R.drawable.failed)
   .crossFade()
   .into(new GlideDrawableImageViewTarget(imageView) {
 @Override
   public void onResourceReady(GlideDrawable drawable, GlideAnimation anim) {
   super.onResourceReady(drawable, anim);
   //在這里添加一些圖片加載完成的操作
  }
)};

6.圖片緩存機(jī)制

Glide緩存策略

Glide默認(rèn)開啟磁盤緩存和內(nèi)存緩存,當(dāng)然也可以對(duì)單張圖片進(jìn)行設(shè)置特定的緩存策略。
設(shè)置圖片不加入到內(nèi)存緩存

Glide 
  .with( context )
  .load( eatFoodyImages[0] )
  .skipMemoryCache( true )
  .into( imageViewInternet );

設(shè)置圖片不加入到磁盤緩存

Glide 
  .with( context )
  .load( eatFoodyImages[0] )
  .diskCacheStrategy( DiskCacheStrategy.NONE )
  .into( imageViewInternet );

Glide支持多種磁盤緩存策略:

DiskCacheStrategy.NONE :不緩存圖片
DiskCacheStrategy.SOURCE :緩存圖片源文件
DiskCacheStrategy.RESULT:緩存修改過(guò)的圖片
DiskCacheStrategy.ALL:緩存所有的圖片,默認(rèn)

圖片加載優(yōu)先級(jí)

Glide支持為圖片加載設(shè)置優(yōu)先級(jí),優(yōu)先級(jí)高的先加載,優(yōu)先級(jí)低的后加載:

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.加載圓角圖片

/**
 * 圓形圖
 *
 * Created by <lzh> on 2016/7/29.
 */
public class GlideCircleTransform extends BitmapTransformation {
  public GlideCircleTransform(Context context) {
    super(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 be 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();
  }
}


**然后使用的時(shí)候只要加上這句話就行了
.transform(new GlideCircleTransform(context))**

Glide.with(mContext)
        .load(imageUrl)
        .transform(new GlideCircleTransform(mContext))
        .into(holder.imageView);


注意事項(xiàng):

不能直接給要使用glide的imageview設(shè)置tag;

因?yàn)間lide在加載圖片的時(shí)候用到了tag,會(huì)造成沖突,并報(bào)錯(cuò);

當(dāng)要用到tag寫邏輯代碼的時(shí)候,可以這樣

.setTag(R.string.xxx,xxx);并.getTag(R.string.xxx);

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論