一個圖片加載的小工具Glide

AdjustBitmap

public class GlideUtils {

private static Stringhttp ="http:";

? ? private static Stringhttps ="https:";

? ? /**

? ? * 驗證配置

? ? *

? ? * @param imageUrl? 地址

? ? * @param imageView 控件

? ? * @return boolean

*/

? ? private static boolean initConfig(String imageUrl, ImageView imageView) {

if (TextUtils.isEmpty(imageUrl) || imageView ==null) {

return false;

? ? ? ? }

return true;

? ? }

public static StringgetImageUrl(String imageUrl) {

if (!imageUrl.contains(http) && !imageUrl.contains(https)) {

imageUrl =http + imageUrl;

? ? ? ? }

return imageUrl;

? ? }

/**

? ? * 加載裁剪頂部的原型照片

? ? *

? ? * @param context

? ? * @param imageUrl

? ? * @param imageView

? ? */

? ? public static void adjustImage(Context context, String imageUrl, final ImageView imageView) {

if (!initConfig(imageUrl, imageView)) {

return;

? ? ? ? }

imageUrl =getImageUrl(imageUrl);

? ? ? ? Glide.with(context)

.load(imageUrl)

.placeholder(R.mipmap.ic_default_circular)

.error(R.mipmap.ic_default_circular)

.into(new SimpleTarget() {

@Override

? ? ? ? ? ? ? ? ? ? public void onResourceReady(GlideDrawable resource, GlideAnimation glideAnimation) {

Bitmap bitmap = AdjustBitmap.drawableToBitmap(resource);

? ? ? ? ? ? ? ? ? ? ? ? imageView.setImageBitmap(AdjustBitmap.getCircleBitmap(bitmap));

? ? ? ? ? ? ? ? ? ? }

});

? ? }

/**

? ? * 加載裁剪頂部的原型照片

? ? *

? ? * @param context

? ? * @param imageUrl

? ? * @param imageView

? ? */

? ? public static void adjustImage(Context context, String imageUrl, final ImageView imageView, final int scaleW, final int scaleH) {

if (!initConfig(imageUrl, imageView)) {

return;

? ? ? ? }

imageUrl =getImageUrl(imageUrl);

? ? ? ? Glide.with(context)

.load(imageUrl)

.placeholder(R.mipmap.ic_default_circular)

.error(R.mipmap.ic_default_circular)

.into(new SimpleTarget() {

@Override

? ? ? ? ? ? ? ? ? ? public void onResourceReady(GlideDrawable resource, GlideAnimation glideAnimation) {

Bitmap bitmap = AdjustBitmap.drawableToBitmap(resource);

? ? ? ? ? ? ? ? ? ? ? ? imageView.setImageBitmap(AdjustBitmap.cropBitmap(bitmap,scaleW,scaleH));

? ? ? ? ? ? ? ? ? ? }

});

? ? }

/**

? ? * 1.網絡加載圖片到ImageView中

? ? */

? ? public static void loadImage(Context context, String imageUrl, ImageView imageView) {

if (!initConfig(imageUrl, imageView)) {

return;

? ? ? ? }

imageUrl =getImageUrl(imageUrl);

? ? ? ? Glide.with(context).load(imageUrl).into(imageView);

? ? }

/**

? ? * 1.網絡加載圖片到ImageView中

? ? */

? ? public static void loadImage(Context context, String imageUrl, ImageView imageView,int width,int height) {

if (!initConfig(imageUrl, imageView)) {

return;

? ? ? ? }

imageUrl =getImageUrl(imageUrl);

? ? ? ? Glide.with(context)

.load(imageUrl)

.placeholder(R.mipmap.ic_default_rectangle)

.error(R.mipmap.ic_default_rectangle)

.override(width,height)

.into(imageView);

? ? }

/**

? ? * 加載長方形圖片

? ? */

? ? public static void rectangleImage(Context context, String imageUrl, ImageView imageView) {

if (!initConfig(imageUrl, imageView)) {

return;

? ? ? ? }

imageUrl =getImageUrl(imageUrl);

? ? ? ? Glide.with(context).load(imageUrl)

.placeholder(R.mipmap.ic_default_rectangle)

.error(R.mipmap.ic_default_rectangle)

.into(imageView);

? ? }

/**

? ? * 加載長方形圖片

? ? */

? ? public static void rectangleImage(Context context, String imageUrl, ImageView imageView,int width,int height) {

if (!initConfig(imageUrl, imageView)) {

return;

? ? ? ? }

imageUrl =getImageUrl(imageUrl);

? ? ? ? Glide.with(context).load(imageUrl)

.placeholder(R.mipmap.ic_default_rectangle)

.error(R.mipmap.ic_default_rectangle)

.override(width,height)

.into(imageView);

? ? }

/**

? ? *? 加載長方形圓角圖片

? ? */

? ? public static void rectangleRoundImage(Context context, String imageUrl, ImageView imageView) {

if (!initConfig(imageUrl, imageView)) {

return;

? ? ? ? }

imageUrl =getImageUrl(imageUrl);

? ? ? ? Glide.with(context).load(imageUrl)

.placeholder(R.mipmap.ic_default_rectangle)

.error(R.mipmap.ic_default_rectangle)

.transform(new GlideRoundTransform(context))

.into(imageView);

? ? }

/**

? ? *? 加載長方形圓角圖片

? ? */

? ? public static void rectangleRoundImage(Context context, String imageUrl, ImageView imageView,int width,int height) {

if (!initConfig(imageUrl, imageView)) {

return;

? ? ? ? }

imageUrl =getImageUrl(imageUrl);

? ? ? ? Glide.with(context).load(imageUrl)

.placeholder(R.mipmap.ic_default_rectangle)

.error(R.mipmap.ic_default_rectangle)

.override(width,height)

.transform(new GlideRoundTransform(context))

.into(imageView);

? ? }

/**

? ? *? 加載長方形圓角圖片

? ? */

? ? public static void rectangleRoundImage(Context context, String imageUrl, ImageView imageView, int mRadius) {

if (!initConfig(imageUrl, imageView)) {

return;

? ? ? ? }

imageUrl =getImageUrl(imageUrl);

? ? ? ? Glide.with(context).load(imageUrl)

.placeholder(R.mipmap.ic_default_rectangle)

.error(R.mipmap.ic_default_rectangle)

.transform(new GlideRoundTransform(context, mRadius))

.into(imageView);

? ? }

/**

? ? *? 加載長方形圓角圖片

? ? */

? ? public static void rectangleRoundImage(Context context, String imageUrl, ImageView imageView, int mRadius,int width,int height) {

if (!initConfig(imageUrl, imageView)) {

return;

? ? ? ? }

imageUrl =getImageUrl(imageUrl);

? ? ? ? Glide.with(context).load(imageUrl)

.placeholder(R.mipmap.ic_default_rectangle)

.error(R.mipmap.ic_default_rectangle)

.override(width,height)

.transform(new GlideRoundTransform(context, mRadius))

.into(imageView);

? ? }

/**

? ? * 加載正方形圖片

? ? */

? ? public static void squareImage(Context context, String imageUrl, ImageView imageView) {

if (!initConfig(imageUrl, imageView)) {

return;

? ? ? ? }

imageUrl =getImageUrl(imageUrl);

? ? ? ? Glide.with(context).load(imageUrl)

.placeholder(R.mipmap.ic_default_square)

.error(R.mipmap.ic_default_square)

.into(imageView);

? ? }

/**

? ? *? 加載正方形圓角圖片

? ? */

? ? public static void squareRoundImage(Context context, String imageUrl, ImageView imageView) {

if (!initConfig(imageUrl, imageView)) {

return;

? ? ? ? }

imageUrl =getImageUrl(imageUrl);

? ? ? ? Glide.with(context).load(imageUrl)

.placeholder(R.mipmap.ic_default_square)

.error(R.mipmap.ic_default_square)

.transform(new GlideRoundTransform(context)).into(imageView);

? ? }

/**

? ? *? 加載正方形圓角圖片

? ? */

? ? public static void squareRoundImage(Context context, String imageUrl, ImageView imageView, int mRadius) {

if (!initConfig(imageUrl, imageView)) {

return;

? ? ? ? }

imageUrl =getImageUrl(imageUrl);

? ? ? ? Glide.with(context).load(imageUrl)

.placeholder(R.mipmap.ic_default_square)

.error(R.mipmap.ic_default_square)

.transform(new GlideRoundTransform(context, mRadius)).into(imageView);

? ? }

/**

? ? * 11 加載圓形圖片

? ? */

? ? public static void loadCircleImg(Context context, String imageUrl, ImageView imageView) {

if (!initConfig(imageUrl, imageView)) {

return;

? ? ? ? }

imageUrl =getImageUrl(imageUrl);

? ? ? ? Glide.with(context).load(imageUrl).transform(new GlideCircleTransform(context)).into(imageView);

? ? }

/**

? ? * 11 加載圓形圖片

? ? */

? ? public static void loadCircleImg(Context context, String imageUrl, ImageView imageView, int width, int height) {

if (!initConfig(imageUrl, imageView)) {

return;

? ? ? ? }

imageUrl =getImageUrl(imageUrl);

? ? ? ? Glide.with(context).load(imageUrl)

.override(width,height)

.transform(new GlideCircleTransform(context)).into(imageView);

? ? }

/**

4.圖片的縮放,centerCrop()和fitCenter():

*/

/**

? ? * 1)使用centerCrop是利用圖片圖填充ImageView設置的大小,如果ImageView的Height是match_parent則圖片就會被拉伸填充

? ? */

? ? public static void loadCenterCrop(Context context, String imageUrl, ImageView imageView) {

if (!initConfig(imageUrl, imageView)) {

return;

? ? ? ? }

imageUrl =getImageUrl(imageUrl);

? ? ? ? Glide.with(context).load(imageUrl).centerCrop().into(imageView);

? ? }

/**

? ? * 2)使用fitCenter即縮放圖像讓圖像都測量出來等于或小于 ImageView 的邊界范圍,該圖像將會完全顯示,但可能不會填滿整個ImageView。

? ? */

? ? public static void loadFitCenter(Context context, String imageUrl, ImageView imageView) {

if (!initConfig(imageUrl, imageView)) {

return;

? ? ? ? }

imageUrl =getImageUrl(imageUrl);

? ? ? ? Glide.with(context).load(imageUrl).fitCenter().into(imageView);

? ? }

/**

? ? * 5.加載gif動畫

? ? */

? ? public static void loadAsGif(Context context, String imageUrl, ImageView imageView) {

if (!initConfig(imageUrl, imageView)) {

return;

? ? ? ? }

imageUrl =getImageUrl(imageUrl);

? ? ? ? Glide.with(context).load(imageUrl).asGif().into(imageView);

? ? }

/**

? ? * 5.加載gif動畫

? ? */

? ? public static void loadAsGif(Context context, int gifId, ImageView imageView) {

try {

pl.droidsonroids.gif.GifDrawable drawable=new pl.droidsonroids.gif.GifDrawable(context.getResources(),gifId);

? ? ? ? ? ? imageView.setImageDrawable(drawable);

? ? ? ? }catch (IOException e) {

e.printStackTrace();

? ? ? ? }

}

/**

? ? * 6.顯示本地視頻

? ? */

? ? public static void loadVideo(Context context, String imageUrl, ImageView imageView) {

if (!initConfig(imageUrl, imageView)) {

return;

? ? ? ? }

imageUrl =getImageUrl(imageUrl);

? ? ? ? imageUrl ="/storage/emulated/0/Pictures/example_video.mp4";

? ? ? ? // Glide.with( context ).load(Uri.fromFile( new File(imageUrl) ) ).into(imageViewGifAsBitmap );

? ? }

/**7.緩存策略

*

*? DiskCacheStrategy.NONE 什么都不緩存

DiskCacheStrategy.SOURCE 僅僅只緩存原來的全分辨率的圖像

DiskCacheStrategy.RESULT 僅僅緩存最終的圖像,即降低分辨率后的(或者是轉換后的)

DiskCacheStrategy.ALL 緩存所有版本的圖像(默認行為)

*/

/**

? ? * 跳過內存緩存

? ? *

? ? * @param context

? ? * @param imageUrl

? ? * @param imageView

? ? */

? ? public static void loadSkipMemoryCache(Context context, String imageUrl, ImageView imageView) {

if (!initConfig(imageUrl, imageView)) {

return;

? ? ? ? }

imageUrl =getImageUrl(imageUrl);

? ? ? ? Glide.with(context).load(imageUrl).skipMemoryCache(true).into(imageView);

? ? }

/**

? ? * 跳過硬盤緩存

? ? *

? ? * @param context

? ? * @param imageUrl

? ? * @param imageView

? ? */

? ? public static void loadDiskCacheStrategy(Context context, String imageUrl, ImageView imageView) {

if (!initConfig(imageUrl, imageView)) {

return;

? ? ? ? }

imageUrl =getImageUrl(imageUrl);

? ? ? ? Glide.with(context).load(imageUrl).diskCacheStrategy(DiskCacheStrategy.NONE).into(imageView);

? ? }

/**

? ? * 8.優先級,設置圖片加載的順序:

? ? */

? ? public static void loadPriority(Context context, String imageUrl, ImageView imageView) {

if (!initConfig(imageUrl, imageView)) {

return;

? ? ? ? }

imageUrl =getImageUrl(imageUrl);

? ? ? ? Glide.with(context).load(imageUrl).priority(Priority.HIGH).into(imageView);

? ? }

/**

? ? * 9.獲取Bitmap,設置CircleImageVIew可以使用這個ImageView庫

? ? */

? ? public static void loadBitmap(Context context, String imageUrl, ImageView imageView) {

if (!initConfig(imageUrl, imageView)) {

return;

? ? ? ? }

imageUrl =getImageUrl(imageUrl);

? ? ? ? Glide.with(context)

.load(imageUrl)

.placeholder(R.drawable.ic_loading_rotate)

.into(new SimpleTarget() {

@Override

? ? ? ? ? ? ? ? ? ? public void onResourceReady(GlideDrawable resource, GlideAnimation glideAnimation) {

}

});

? ? }

//圓形圖片

? ? public static class GlideCircleTransformextends BitmapTransformation {

public GlideCircleTransform(Context context) {

super(context);

? ? ? ? }

@Override

? ? ? ? protected Bitmaptransform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {

return circleCrop(pool, toTransform);

? ? ? ? }

private BitmapcircleCrop(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 StringgetId() {

return getClass().getName();

? ? ? ? }

}

//圓角圖片

? ? public static class GlideRoundTransformextends BitmapTransformation {

private float radius =0f;

? ? ? ? public GlideRoundTransform(Context context) {

this(context, 4);

? ? ? ? }

public GlideRoundTransform(Context context, int dp) {

super(context);

? ? ? ? ? ? this.radius = Resources.getSystem().getDisplayMetrics().density * dp;

? ? ? ? }

@Override

? ? ? ? protected Bitmaptransform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {

return roundCrop(pool, toTransform);

? ? ? ? }

private BitmaproundCrop(BitmapPool pool, Bitmap source) {

if (source ==null) {

return null;

? ? ? ? ? ? }

Bitmap result = pool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);

? ? ? ? ? ? if (result ==null) {

result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);

? ? ? ? ? ? }

Canvas canvas =new Canvas(result);

? ? ? ? ? ? Paint paint =new Paint();

? ? ? ? ? ? paint.setShader(new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));

? ? ? ? ? ? paint.setAntiAlias(true);

? ? ? ? ? ? RectF rectF =new RectF(0f, 0f, source.getWidth(), source.getHeight());

? ? ? ? ? ? canvas.drawRoundRect(rectF, radius, radius, paint);

? ? ? ? ? ? return result;

? ? ? ? }

@Override

? ? ? ? public StringgetId() {

return getClass().getName() + Math.round(radius);

? ? ? ? }

}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 一、簡介 在泰國舉行的谷歌開發者論壇上,谷歌為我們介紹了一個名叫Glide的圖片加載庫,作者是bumptech。這...
    天天大保建閱讀 7,539評論 2 28
  • 導入Picasso和Glide都在jcenter上。在項目中添加依賴非常簡單:Picassodependencie...
    瀚海江天閱讀 4,484評論 0 2
  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,837評論 18 139
  • 作為一個離開 wifi 不能生存的網蟲,我把每天都會上去看個幾分鐘的網站做了一個 daily 的文件夾扔在瀏覽器里...
    HybridRbt閱讀 569評論 0 1
  • 動動手指 寫詩玩 動動手指 賺錢樂 寫詩我高雅 賺錢我低俗 雅俗共賞是我友 暢聊至天明 世界這么大 有緣是天意 2...
    觀海聽濤_6ae8閱讀 104評論 0 0