Android Glide圖片加載(加載監(jiān)聽、加載動畫)
本文實例為大家分享了Android Glide圖片加載的具體代碼,供大家參考,具體內容如下
1.普通用法
Glide.with(context) .load(url) .into(view);
with中可以放context、activity、fragment。。;當放activity、fragment時glide會根據(jù)生命周期來加載圖片。推薦使用activity。
2.設置加載中和加載失敗的圖片
Glide.with(context) .load(url) .placeholder(R.drawable.loading) //占位符 也就是加載中的圖片,可放個gif .error(R.drawable.failed) //失敗圖片 .into(view);
3.添加圖片淡入加載的效果
.crossFade()
4.用 animate() 自定義動畫
從資源中的動畫:
回到代碼,第一個選項是傳一個 Android 資源 id,即動畫的資源。一個簡單的例子是每個 Android 系統(tǒng)都提供的:slide-in-left(從左滑入)動畫, android.R.anim.slide_in_left 。下面這段代碼是這個動畫的 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>
當然你可以創(chuàng)建你自己的 XML 動畫。比如一個小的縮放動畫,圖片剛開始小的,然后逐漸增大到原尺寸。
<?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>
這兩個動畫都可以用到 Glide 建造者中:
Glide .with( context ) .load( eatFoodyImages[0] ) .animate( android.R.anim.slide_in_left ) // or R.anim.zoom_in .into( imageView1 );
在圖片從網(wǎng)絡加載完并準備好之后將從左邊滑入。
通過自定義類實現(xiàn)動畫
這個很簡單,你只需實現(xiàn) void animate(View view) 方法。這個視圖對象是整個 target 視圖。如果它是一個自定義的視圖,你要找到你的視圖的子元素,并且做些必要的動畫。
來看個簡單的例子。假設你想要實現(xiàn)一個漸現(xiàn)動畫,你得需要創(chuàng)建這樣的動畫對象:
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(); } };
接下來,你需要在 Glide 請求中去設置這個動畫:
Glide .with( context ) .load( eatFoodyImages[1] ) .animate( animationObject ) .into( imageView2 );
當然,在 animate(View view) 中你的動畫對象方法中, 你可以做任何你想要對視圖做的事情。自由的用你的動畫創(chuàng)建吧。
如果你要在你的自定義視圖中實現(xiàn),你只需要創(chuàng)建這個視圖對象,然后在你的自定義視圖中創(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.圖片緩存機制
Glide緩存策略
Glide默認開啟磁盤緩存和內存緩存,當然也可以對單張圖片進行設置特定的緩存策略。
設置圖片不加入到內存緩存
Glide .with( context ) .load( eatFoodyImages[0] ) .skipMemoryCache( true ) .into( imageViewInternet );
設置圖片不加入到磁盤緩存
Glide .with( context ) .load( eatFoodyImages[0] ) .diskCacheStrategy( DiskCacheStrategy.NONE ) .into( imageViewInternet );
Glide支持多種磁盤緩存策略:
DiskCacheStrategy.NONE :不緩存圖片
DiskCacheStrategy.SOURCE :緩存圖片源文件
DiskCacheStrategy.RESULT:緩存修改過的圖片
DiskCacheStrategy.ALL:緩存所有的圖片,默認
圖片加載優(yōu)先級
Glide支持為圖片加載設置優(yōu)先級,優(yōu)先級高的先加載,優(yōu)先級低的后加載:
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(); } }
**然后使用的時候只要加上這句話就行了
.transform(new GlideCircleTransform(context))**
Glide.with(mContext) .load(imageUrl) .transform(new GlideCircleTransform(mContext)) .into(holder.imageView);
注意事項:
不能直接給要使用glide的imageview設置tag;
因為glide在加載圖片的時候用到了tag,會造成沖突,并報錯;
當要用到tag寫邏輯代碼的時候,可以這樣
.setTag(R.string.xxx,xxx);并.getTag(R.string.xxx);
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Android筆記之:App自動化之使用Ant編譯項目多渠道打包的使用詳解
本篇文章介紹了,Android筆記之:App自動化之使用Ant編譯項目多渠道打包的使用詳解。需要的朋友參考下2013-04-04Android編程實現(xiàn)文件瀏覽功能的方法【類似于FileDialog的功能】
這篇文章主要介紹了Android編程實現(xiàn)文件瀏覽功能的方法,可實現(xiàn)類似于FileDialog的功能,涉及Android針對文件與目錄操作的相關技巧,需要的朋友可以參考下2016-11-11Android中導航組件Navigation的實現(xiàn)原理
大家好,本篇文章主要講的是Android中導航組件Navigation的實現(xiàn)原理,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下2022-02-02Android編程中TextView寬度過大導致Drawable無法居中問題解決方法
這篇文章主要介紹了Android編程中TextView寬度過大導致Drawable無法居中問題解決方法,以實例形式較為詳細的分析了TextView設置及xml布局與調用技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10Android Glide圖片加載(加載監(jiān)聽、加載動畫)
這篇文章主要為大家詳細介紹了Android Glide圖片加載的具體實現(xiàn)方法,包括加載監(jiān)聽、加載動畫,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-11-11Android手機開發(fā) 控件 TextView文字居中
本文主要介紹Android手機開發(fā)TextView居中的方法,希望能幫到大家。2016-05-05