`
龙哥IT
  • 浏览: 252920 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
文章分类
社区版块
存档分类
最新评论

框架Glide实现图片加载

 
阅读更多

学习了图片加载的几个框架,在这里把自己遇到的写下来

首先Glide加载图片

01

如果没啥特殊要求,图片处理代码如下:

 

Glide.with(this).load(imgurl)
					.placeholder(R.drawable.logo_s)//默认图
					.error(R.drawable.logo_s)//发生错误的默认图
					.into(coupon_ad_iv);

 02

 

加载圆形图,需要自定义封装一个圆形类

 

Glide.with(ShequActivity.this).load(faceurl).placeholder(R.drawable.xyzd).bitmapTransform(new GlideCircleTransform(this)).into(userbut);

 03

 

加载圆角图,也需要封装一个圆角的类

 

这里的DiskCacheStrategy.ALL表示四个角都是圆角

 

Glide.with(mContext)
					.load(t.getCover().toString())
					.placeholder(R.drawable.logo_s)
					.error(R.drawable.logo_s)
					.transform(new CenterCrop(mContext),
							new GlideRoundTransform(mContext))
					.diskCacheStrategy(DiskCacheStrategy.ALL).crossFade()
					.into(coupon_ad_iv);

 

Glide.with(mContext)
						.load(mHotList.get(position).getPic().toString())
						.placeholder(R.drawable.logo_s).error(R.drawable.logo_s)
						.bitmapTransform(
								new CenterCrop(mContext),
								new RoundedCornersTransformation(
										mContext, 20, 0,CornerType.TOP))
						.crossFade(1000).into(holder.coupon_ad_iv);

这里的CornerType.TOP表示四个角其中的上面两个是圆角

 

下面是自定义封装的类:

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

}

 

 * Copyright (C) 2017 Wasabeef

import android.content.Context;

public class RoundedCornersTransformation implements Transformation<Bitmap> {

  public enum CornerType {
    ALL,
    TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT,
    TOP, BOTTOM, LEFT, RIGHT,
    OTHER_TOP_LEFT, OTHER_TOP_RIGHT, OTHER_BOTTOM_LEFT, OTHER_BOTTOM_RIGHT,
    DIAGONAL_FROM_TOP_LEFT, DIAGONAL_FROM_TOP_RIGHT
  }

  private BitmapPool mBitmapPool;
  private int mRadius;
  private int mDiameter;
  private int mMargin;
  private CornerType mCornerType;

  public RoundedCornersTransformation(Context context, int radius, int margin) {
    this(context, radius, margin, CornerType.ALL);
  }

  public RoundedCornersTransformation(BitmapPool pool, int radius, int margin) {
    this(pool, radius, margin, CornerType.ALL);
  }

  public RoundedCornersTransformation(Context context, int radius, int margin,
      CornerType cornerType) {
    this(Glide.get(context).getBitmapPool(), radius, margin, cornerType);
  }

  public RoundedCornersTransformation(BitmapPool pool, int radius, int margin,
      CornerType cornerType) {
    mBitmapPool = pool;
    mRadius = radius;
    mDiameter = mRadius * 2;
    mMargin = margin;
    mCornerType = cornerType;
  }

  @Override
  public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {
    Bitmap source = resource.get();

    int width = source.getWidth();
    int height = source.getHeight();

    Bitmap bitmap = mBitmapPool.get(width, height, Bitmap.Config.ARGB_8888);
    if (bitmap == null) {
      bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    }

    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setShader(new BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
    drawRoundRect(canvas, paint, width, height);
    return BitmapResource.obtain(bitmap, mBitmapPool);
  }

  private void drawRoundRect(Canvas canvas, Paint paint, float width, float height) {
    float right = width - mMargin;
    float bottom = height - mMargin;

    switch (mCornerType) {
      case ALL:
        canvas.drawRoundRect(new RectF(mMargin, mMargin, right, bottom), mRadius, mRadius, paint);
        break;
      case TOP_LEFT:
        drawTopLeftRoundRect(canvas, paint, right, bottom);
        break;
      case TOP_RIGHT:
        drawTopRightRoundRect(canvas, paint, right, bottom);
        break;
      case BOTTOM_LEFT:
        drawBottomLeftRoundRect(canvas, paint, right, bottom);
        break;
      case BOTTOM_RIGHT:
        drawBottomRightRoundRect(canvas, paint, right, bottom);
        break;
      case TOP:
        drawTopRoundRect(canvas, paint, right, bottom);
        break;
      case BOTTOM:
        drawBottomRoundRect(canvas, paint, right, bottom);
        break;
      case LEFT:
        drawLeftRoundRect(canvas, paint, right, bottom);
        break;
      case RIGHT:
        drawRightRoundRect(canvas, paint, right, bottom);
        break;
      case OTHER_TOP_LEFT:
        drawOtherTopLeftRoundRect(canvas, paint, right, bottom);
        break;
      case OTHER_TOP_RIGHT:
        drawOtherTopRightRoundRect(canvas, paint, right, bottom);
        break;
      case OTHER_BOTTOM_LEFT:
        drawOtherBottomLeftRoundRect(canvas, paint, right, bottom);
        break;
      case OTHER_BOTTOM_RIGHT:
        drawOtherBottomRightRoundRect(canvas, paint, right, bottom);
        break;
      case DIAGONAL_FROM_TOP_LEFT:
        drawDiagonalFromTopLeftRoundRect(canvas, paint, right, bottom);
        break;
      case DIAGONAL_FROM_TOP_RIGHT:
        drawDiagonalFromTopRightRoundRect(canvas, paint, right, bottom);
        break;
      default:
        canvas.drawRoundRect(new RectF(mMargin, mMargin, right, bottom), mRadius, mRadius, paint);
        break;
    }
  }

  private void drawTopLeftRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
    canvas.drawRoundRect(new RectF(mMargin, mMargin, mMargin + mDiameter, mMargin + mDiameter),
        mRadius, mRadius, paint);
    canvas.drawRect(new RectF(mMargin, mMargin + mRadius, mMargin + mRadius, bottom), paint);
    canvas.drawRect(new RectF(mMargin + mRadius, mMargin, right, bottom), paint);
  }

  private void drawTopRightRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
    canvas.drawRoundRect(new RectF(right - mDiameter, mMargin, right, mMargin + mDiameter), mRadius,
        mRadius, paint);
    canvas.drawRect(new RectF(mMargin, mMargin, right - mRadius, bottom), paint);
    canvas.drawRect(new RectF(right - mRadius, mMargin + mRadius, right, bottom), paint);
  }

  private void drawBottomLeftRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
    canvas.drawRoundRect(new RectF(mMargin, bottom - mDiameter, mMargin + mDiameter, bottom),
        mRadius, mRadius, paint);
    canvas.drawRect(new RectF(mMargin, mMargin, mMargin + mDiameter, bottom - mRadius), paint);
    canvas.drawRect(new RectF(mMargin + mRadius, mMargin, right, bottom), paint);
  }

  private void drawBottomRightRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
    canvas.drawRoundRect(new RectF(right - mDiameter, bottom - mDiameter, right, bottom), mRadius,
        mRadius, paint);
    canvas.drawRect(new RectF(mMargin, mMargin, right - mRadius, bottom), paint);
    canvas.drawRect(new RectF(right - mRadius, mMargin, right, bottom - mRadius), paint);
  }

  private void drawTopRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
    canvas.drawRoundRect(new RectF(mMargin, mMargin, right, mMargin + mDiameter), mRadius, mRadius,
        paint);
    canvas.drawRect(new RectF(mMargin, mMargin + mRadius, right, bottom), paint);
  }

  private void drawBottomRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
    canvas.drawRoundRect(new RectF(mMargin, bottom - mDiameter, right, bottom), mRadius, mRadius,
        paint);
    canvas.drawRect(new RectF(mMargin, mMargin, right, bottom - mRadius), paint);
  }

  private void drawLeftRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
    canvas.drawRoundRect(new RectF(mMargin, mMargin, mMargin + mDiameter, bottom), mRadius, mRadius,
        paint);
    canvas.drawRect(new RectF(mMargin + mRadius, mMargin, right, bottom), paint);
  }

  private void drawRightRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
    canvas.drawRoundRect(new RectF(right - mDiameter, mMargin, right, bottom), mRadius, mRadius,
        paint);
    canvas.drawRect(new RectF(mMargin, mMargin, right - mRadius, bottom), paint);
  }

  private void drawOtherTopLeftRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
    canvas.drawRoundRect(new RectF(mMargin, bottom - mDiameter, right, bottom), mRadius, mRadius,
        paint);
    canvas.drawRoundRect(new RectF(right - mDiameter, mMargin, right, bottom), mRadius, mRadius,
        paint);
    canvas.drawRect(new RectF(mMargin, mMargin, right - mRadius, bottom - mRadius), paint);
  }

  private void drawOtherTopRightRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
    canvas.drawRoundRect(new RectF(mMargin, mMargin, mMargin + mDiameter, bottom), mRadius, mRadius,
        paint);
    canvas.drawRoundRect(new RectF(mMargin, bottom - mDiameter, right, bottom), mRadius, mRadius,
        paint);
    canvas.drawRect(new RectF(mMargin + mRadius, mMargin, right, bottom - mRadius), paint);
  }

  private void drawOtherBottomLeftRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
    canvas.drawRoundRect(new RectF(mMargin, mMargin, right, mMargin + mDiameter), mRadius, mRadius,
        paint);
    canvas.drawRoundRect(new RectF(right - mDiameter, mMargin, right, bottom), mRadius, mRadius,
        paint);
    canvas.drawRect(new RectF(mMargin, mMargin + mRadius, right - mRadius, bottom), paint);
  }

  private void drawOtherBottomRightRoundRect(Canvas canvas, Paint paint, float right,
      float bottom) {
    canvas.drawRoundRect(new RectF(mMargin, mMargin, right, mMargin + mDiameter), mRadius, mRadius,
        paint);
    canvas.drawRoundRect(new RectF(mMargin, mMargin, mMargin + mDiameter, bottom), mRadius, mRadius,
        paint);
    canvas.drawRect(new RectF(mMargin + mRadius, mMargin + mRadius, right, bottom), paint);
  }

  private void drawDiagonalFromTopLeftRoundRect(Canvas canvas, Paint paint, float right,
      float bottom) {
    canvas.drawRoundRect(new RectF(mMargin, mMargin, mMargin + mDiameter, mMargin + mDiameter),
        mRadius, mRadius, paint);
    canvas.drawRoundRect(new RectF(right - mDiameter, bottom - mDiameter, right, bottom), mRadius,
        mRadius, paint);
    canvas.drawRect(new RectF(mMargin, mMargin + mRadius, right - mDiameter, bottom), paint);
    canvas.drawRect(new RectF(mMargin + mDiameter, mMargin, right, bottom - mRadius), paint);
  }

  private void drawDiagonalFromTopRightRoundRect(Canvas canvas, Paint paint, float right,
      float bottom) {
    canvas.drawRoundRect(new RectF(right - mDiameter, mMargin, right, mMargin + mDiameter), mRadius,
        mRadius, paint);
    canvas.drawRoundRect(new RectF(mMargin, bottom - mDiameter, mMargin + mDiameter, bottom),
        mRadius, mRadius, paint);
    canvas.drawRect(new RectF(mMargin, mMargin, right - mRadius, bottom - mRadius), paint);
    canvas.drawRect(new RectF(mMargin + mRadius, mMargin + mRadius, right, bottom), paint);
  }

  @Override public String getId() {
    return "RoundedTransformation(radius=" + mRadius + ", margin=" + mMargin + ", diameter="
        + mDiameter + ", cornerType=" + mCornerType.name() + ")";
  }
}

 

 另外

上面这些方法虽然可以实现处理图片效果,但是,加载图片会变形,必须要添加必要的处理才行

添加类:

public class MyBitmapImageViewTarget extends BitmapImageViewTarget {

	 public MyBitmapImageViewTarget(ImageView view) {
	      super(view);
	   }

	   @Override
	   public void onResourceReady(Bitmap bitmap, GlideAnimation anim) {
	      if (bitmap != null && view.getScaleType() != ScaleType.FIT_XY) {
	         view.setScaleType(ScaleType.FIT_XY);
	      }
	      super.onResourceReady(bitmap, anim);
	   }

	   @Override
	   protected void setResource(Bitmap resource) {
	      super.setResource(resource);
	   }

	   @Override
	   public void onLoadFailed(Exception e, Drawable errorDrawable) {
	      if (errorDrawable != null && view != null && view.getScaleType() != ScaleType.CENTER_CROP) {
	         view.setScaleType(ScaleType.CENTER_CROP);
	      }
	      super.onLoadFailed(e, errorDrawable);
	   }

	   @Override
	   public void onLoadStarted(Drawable placeholder) {
	      if (placeholder != null && placeholder != null && view != null && view.getScaleType() != ScaleType.CENTER_CROP) {
	         view.setScaleType(ScaleType.CENTER_CROP);
	      }
	      super.onLoadStarted(placeholder);
	   }

	   @Override
	   public void onLoadCleared(Drawable placeholder) {
	      if (placeholder != null && placeholder != null && view != null && view.getScaleType() != ScaleType.CENTER_CROP) {
	         view.setScaleType(ScaleType.CENTER_CROP);
	      }
	      super.onLoadCleared(placeholder);
	   }

}

 

然后放入Glide中去:

例如:此方法是实现正常加载图片以及圆形图片,然后就不变形了

public static void glideDisplayImg(Context mContext, String imgurl,
			ImageView img, int drawable, boolean isCircle) {
		if (isCircle) {
			Glide.with(mContext).load(imgurl).asBitmap().placeholder(drawable)
					.error(drawable).centerCrop()
					.transform(new GlideCircleTransform(mContext))
					.into(new MyBitmapImageViewTarget(img));
		} else {
			Glide.with(mContext).load(imgurl).asBitmap().placeholder(drawable).centerCrop()
					.error(drawable).into(new MyBitmapImageViewTarget(img));
		}
	}

 

分享到:
评论

相关推荐

    图片加载框架glide-3.7.0.jar

    - **图片加载**:Glide支持从网络、本地存储、资源文件等多种来源加载图片,通过URL或文件路径即可轻松实现。 - **自动处理**:Glide会自动处理图片的缩放、裁剪和格式转换,避免因图片过大导致的内存问题。 - **...

    glide加载图片有进度显示

    标题“glide加载图片有进度显示”指的是在使用Glide加载图片时,我们可以通过自定义监听器或者其他的手段获取到加载进度,并在界面上进行显示,为用户提供更好的交互体验。以下是一些关于如何在Glide中实现图片加载...

    Glide框架时序图-加载网络图片

    Glide框架时序图-加载网络图片,Glide框架时序图-加载网络图片。

    glide框架加载gif图片

    Glide加载GIF图片的过程相对简单。通常,你可以在一个`ImageView`中加载GIF。以下是一个基本示例: ```java // 在Activity或Fragment中初始化Glide Glide.with(this) // 或者 Glide.with(getSupportFragmentManager...

    第三方框架Glide显示图片

    1. **高效加载**:Glide采用了一种叫做“资源绑定”的机制,只有当ImageView可见时才会加载图片,避免了不必要的内存消耗。 2. **智能缓存**:Glide内置了两级缓存——内存缓存和磁盘缓存,能够快速响应用户请求,...

    google图片加载框架glide.jar

    google图片加载框架glide.jar

    Android图片框架Glide-3.7.0(最新,很强大)

    Android图片框架Glide-3.7.0(最新,很强大),超好用的图片框架(包含jar和源码) Glide 是一个高效、开源、 Android设备上的媒体管理框架,它遵循BSD、MIT以及Apache 2.0协议发布。Glide具有获取、解码和展示视频...

    Glide 图片加载模块 jar 最新

    这段代码表示从指定的URL加载图片,加载失败时显示错误资源,加载成功前显示占位符资源,最后将图片加载到ImageView中。 总之,“Glide 图片加载模块 jar 最新”为开发者提供了高效、便捷的图片管理工具,无论是在...

    90分钟搞定图片加载框架Glide,面试实战一条龙.wmv

    90分钟搞定图片加载框架Glide,面试实战一条龙 1.Glide架构思想解析 2.手写实现Glide基本框架 3.为框架搭载高并发引擎 4.如何处理Glide的缓存机制

    Android glide框架加载gif图片.rar

    通过以上介绍,你应该对使用Glide加载GIF图片有了全面的理解。在实际项目中,可以根据需求灵活运用这些知识点,提高应用的图片加载性能和用户体验。如果你需要更深入的了解,可以查阅Glide的官方文档和示例代码,...

    图片加载glide 框架damo

    "GldieDemo-master"中的代码实例可能包含多个使用Glide加载图片的场景,包括不同的加载方式、变换、缓存策略等。通过研究这些示例,我们可以更好地了解Glide的用法,并将其应用到自己的项目中。 7. **优化实践** ...

    Android中Glide获取图片Path、Bitmap用法详解

    软件开发网在此之前给大家介绍过图片加载框架Glide的基本用法介绍,大家可以先参考一下,本篇内容更加深入的分析了Glide获取图片Path、Bitmap用法,以及实现的代码分析。 1. 获取Bitmap: 1)在图片下载缓存好之后...

    Android Glide图片加载(加载监听、加载动画)

    通过以上的方法,开发者可以根据需求灵活地控制Glide加载图片的行为,提供更好的用户体验。Glide的加载监听功能可以让开发者在图片加载的各个阶段执行特定的操作,而丰富的动画机制则可以为应用增添动态效果,提升...

    Android图片加载框架

    本篇文章将深入探讨“Android图片加载框架”,特别是与Glide相关的知识。 Glide是一款由Google推荐的Android图片加载库,它专为Android平台设计,用于在ListView、GridView等滚动视图中加载和显示图片。Glide的出现...

    Glide框架时序图-加载缓存图片

    Glide框架时序图-加载缓存图片,Glide框架时序图-加载缓存图片。

    手写图片加载Glide框架2

    在Android应用开发中,图片加载库是不...通过阅读源码,你可以了解每个部分的具体实现细节,这对于理解和掌握自定义Glide加载图片的方法非常有帮助。在实际开发中,灵活运用这些技巧可以提升图片加载的效率和用户体验。

    Android图片框架Glide-3.7.0(最新,很强大),超好用的图片框架(包含jar和源码

    Android图片框架Glide-3.7.0(最新,很强大),超好用的图片框架(包含jar和源码[注:本内容来自网络,在此分享仅为帮助有需要的网友,如果侵犯了您的权利,麻烦联系我,我会第一时间删除,谢谢您。]

    Android中图片加载框架Glide解析1----基本用法

    在Android应用开发中,图片加载是一项至关重要的任务,因为它直接影响到应用的性能和用户体验。Glide作为一款流行的图片加载库,被广泛应用于各种项目中,...在实践中学习,你将更加熟练地掌握这个强大的图片加载框架。

    Android图片加载框架Glide的基本用法介绍

    加载图片是Glide的核心功能。在XML布局文件中,你可以创建一个ImageView来展示图片,例如`activity_main.xml`: ```xml xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width=...

    glide4.0安卓图片加载框架

    glide4.0是最新的谷歌提供的安卓加载图片的jar包,功能强大,使用方便。

Global site tag (gtag.js) - Google Analytics