ImageLoader是款非常好的开源图片加载控件,android中加载图片是一个非常头疼的事,图片列表卡,内存溢出等,项目中自从用了ImageLoder之后这种问题省心多了,尽管给链接给控件图片就加载出来,项目中用了两年了控件了,只知道传两个参数就可以显示出图片,但对具体的实现原理一知半解,花了一些时间能Imageloader的源码进行了研究,发现好多知识都不曾知道。
Imageloder中用提供了四中图片的显示式,圆形,淡入(动画),圆角,修饰,项目中显示圆角,圆形的图片是做了一个白色的中间透明的图片覆盖到原图的上面形成的圆形的效果,很Low,用ImageLoader这些完全不用。
在控件的核心包中有下面的display包里面就是用来显示图片代码
 这六个类的关系简单说一下
 BitmapDisplayer是一个接口,
CircleBitmapDislayer、RoundedBitmapDisplayer、SimpleBitmapDisplayer、FadeInbitmapDisplayer实现了BitmapDisplayer接口,
RoundedVignetteBitmapDispler继承了RoundedBitmapDisplayer.

BitmapDisplayer接口
   
   
  1. public interface BitmapDisplayer {
  2. void display(Bitmap bitmap, ImageAware imageAware, LoadedFrom loadedFrom);
  3. }
中只有一个方法display,
第二个参数IamgeAware是控件中对Imageview的封装,可以理解为IamgeView,
第三方参数 LoadedFrom是一个枚举类,图片是从那加载的,有四种:
 NETWORK,从网络加载
 DISC_CACHE从磁盘缓存中加载
  MEMORY_CACHE  从内存缓存中加载
SimpleBitmapDisplayer是直接显示图片对图片没有进行任何处理
   
   
  1. public final class SimpleBitmapDisplayer implements BitmapDisplayer {
  2. @Override
  3. public void display(Bitmap bitmap, ImageAware imageAware, LoadedFrom loadedFrom) {
  4. imageAware.setImageBitmap(bitmap);
  5. }
  6. }

CircleBitmapDislayer分析
   
   
  1. @Override
  2. public void display(Bitmap bitmap, ImageAware imageAware, LoadedFrom loadedFrom) {
  3. if (!(imageAware instanceof ImageViewAware)) {
  4. throw new IllegalArgumentException("ImageAware should wrap ImageView. ImageViewAware is expected.");
  5. }
  6. imageAware.setImageDrawable(new CircleDrawable(bitmap, strokeColor, strokeWidth));
  7. }
实现了接口BitmapDispalyer接口中的方法display,实现比较简单先判断了用于显示图片的是不是IamgeView,如果不是抛出异常,后面的那一句相当于ImageView中的SetImageDrawable(Drawable图片);
CircleDrawable是一个内部类,继承Drawable.
    
    
  1. public static class CircleDrawable extends Drawable {
  2. protected float radius;
  3. protected final RectF mRect = new RectF();
  4. protected final RectF mBitmapRect;
  5. protected final BitmapShader bitmapShader;
  6. protected final Paint paint;
  7. protected final Paint strokePaint;
  8. protected final float strokeWidth;
  9. protected float strokeRadius;
  10. public CircleDrawable(Bitmap bitmap, Integer strokeColor, float strokeWidth) {
  11. bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);调
  12. mBitmapRect = new RectF(0, 0, bitmap.getWidth(), bitmap.getHeight());
  13. paint = new Paint();
  14. paint.setAntiAlias(true);
  15. paint.setShader(bitmapShader);
  16. paint.setFilterBitmap(true);
  17. paint.setDither(true);
  18. if (strokeColor == null) {
  19. strokePaint = null;
  20. } else {
  21. strokePaint = new Paint();
  22. strokePaint.setStyle(Paint.Style.STROKE);
  23. strokePaint.setColor(strokeColor);
  24. strokePaint.setStrokeWidth(strokeWidth);
  25. strokePaint.setAntiAlias(true);
  26. }
  27. this.strokeWidth = strokeWidth;
  28. strokeRadius = radius - strokeWidth / 2;
  29. }
  30. @Override
  31. protected void onBoundsChange(Rect bounds) {
  32. super.onBoundsChange(bounds);
  33. mRect.set(0, 0, bounds.width(), bounds.height());
  34. radius = Math.min(bounds.width(), bounds.height()) / 2;
  35. strokeRadius = radius - strokeWidth / 2;
  36. // Resize the original bitmap to fit the new bound
  37. Matrix shaderMatrix = new Matrix();
  38. shaderMatrix.setRectToRect(mBitmapRect, mRect, Matrix.ScaleToFit.FILL);
  39. bitmapShader.setLocalMatrix(shaderMatrix);
  40. }
  41. @Override
  42. public void draw(Canvas canvas) {
  43. canvas.drawCircle(radius, radius, radius, paint);
  44. if (strokePaint != null) {
  45. canvas.drawCircle(radius, radius, strokeRadius, strokePaint);
  46. }
  47. }
  48. @Override
  49. public int getOpacity() {
  50. return PixelFormat.TRANSLUCENT;
  51. }
  52. @Override
  53. public void setAlpha(int alpha) {
  54. paint.setAlpha(alpha);
  55. }
  56. @Override
  57. public void setColorFilter(ColorFilter cf) {
  58. paint.setColorFilter(cf);
  59. }
  60. }
这个内部的作用就是用一个矩形的Bitmap显示成一个圆形的Drawable
     
     
  1. radius = Math.min(bitmap.getWidth(), bitmap.getHeight()) / 2;
长和宽中最小的一半做为圆图的半径
     
     
  1. bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
  1. 这个方法来产生一个画有一个位图的渲染器(Shader)
  2. TileMode:(一共有三种)
     
    CLAMP  :如果渲染器超出原始边界范围,会复制范围内边缘染色。
     
    REPEAT :横向和纵向的重复渲染器图片,平铺。
     
    MIRROR :横向和纵向的重复渲染器图片,这个和REPEAT 重复方式不一样,他是以镜像方式平铺。

onBoundsChange
     
     
  1. @Override
            protected void onBoundsChange(Rect bounds) {
                super.onBoundsChange(bounds);
                mRect.set(0, 0, bounds.width(), bounds.height());
                radius = Math.min(bounds.width(), bounds.height()) / 2;
                strokeRadius = radius - strokeWidth / 2;
                // Resize the original bitmap to fit the new bound
                Matrix shaderMatrix = new Matrix();
                shaderMatrix.setRectToRect(mBitmapRectmRect, Matrix.ScaleToFit.FILL);
                bitmapShader.setLocalMatrix(shaderMatrix);
            }
  shaderMatrix .setRectToRect( mBitmapRect mRect , Matrix.ScaleToFit. FILL );
mBitmapRect 坐标变换前的矩形
mRect 坐标变换后的矩形
Matrix . ScaleToFit . FILL 矩形缩放选项
由于提供坐标变换前后的参数可为任意矩形,这样的话,变换前后矩形的长宽比不一定一样,提供指定Matrix.ScaleToFit选项来确定缩放选项。Matrix.ScaleToFit定义了四种选项:
 
CENTER: 保持坐标变换前矩形的长宽比,并最大限度的填充变换后的矩形。至少有一边和目标矩形重叠。
END:保持坐标变换前矩形的长宽比,并最大限度的填充变换后的矩形。至少有一边和目标矩形重叠。END提供右下对齐。
FILL: 可能会变换矩形的长宽比,保证变换和目标矩阵长宽一致。
START:保持坐标变换前矩形的长宽比,并最大限度的填充变换后的矩形。至少有一边和目标矩形重叠。START提供左上对齐。

     
     
  1. @Override
  2. public void draw(Canvas canvas) {
  3. canvas.drawCircle(radius, radius, radius, paint);
  4. if (strokePaint != null) {
  5. canvas.drawCircle(radius, radius, strokeRadius, strokePaint);
  6. }
  7. }

在onDraw方法中

画出一个半径为 radius原图,圆心在原来图片的中心 ,再画出一个以坐标x=radius,y=radius为圆心,宽度strokeRadius的圆环。
RoundedBitmapDisplayer圆角图片的实现原理和圆形的基本一样,只是在内部类中把Birmap构建一个圆角的图片。看一下RoundedBitmapDisplayer的onDraw方法

     
     
  1. @Override
  2. public void draw(Canvas canvas) {
  3. canvas.drawRoundRect(mRect, cornerRadius, cornerRadius, paint);
  4. }

参数说明
mRect:矩形对象。
cornerRadius:x方向上的圆角半径。
cornerRadius:y方向上的圆角半径。
paint:绘制时所使用的画笔。

RoundedVignetteBitmapDispler继承了RoundedBitmapDisplayer. 是在圆角的基础上对图片进行了修饰。
内部类继承了 RoundedBitmapDisplayer中 RoundedDrawable类, 用了父类的构造方法,
   
   
  1. RoundedVignetteDrawable(Bitmap bitmap, int cornerRadius, int margin) {
  2. super(bitmap, cornerRadius, margin);
  3. }

所以实际执行了 RoundedBitmapDisplayer类中 的内部方法,
   
   
  1. @Override
  2. protected void onBoundsChange(Rect bounds) {
  3. super.onBoundsChange(bounds);
  4. RadialGradient vignette = new RadialGradient(
  5. mRect.centerX(), mRect.centerY() * 1.0f / 0.7f, mRect.centerX() * 1.3f,
  6. new int[]{0, 0, 0x7f000000}, new float[]{0.0f, 0.7f, 1.0f},
  7. Shader.TileMode.CLAMP);
  8. Matrix oval = new Matrix();
  9. oval.setScale(1.0f, 0.7f);
  10. vignette.setLocalMatrix(oval);
  11. paint.setShader(new ComposeShader(bitmapShader, vignette, PorterDuff.Mode.SRC_OVER));
  12. }
圆形渲染器
RadialGradient vignette = new RadialGradient(
                    mRect.centerX(), mRect.centerY() * 1.0f / 0.7f, mRect.centerX() * 1.3f,
                    new int[]{0, 0, 0x7f000000}, new float[]{0.0f, 0.7f, 1.0f},
                    Shader.TileMode.CLAMP);   
mRect.centerX():  圆心X坐标
mRect.centerY() * 1.0f / 0.7f:  圆心Y坐标
mRect.centerX() * 1.3f: 半径
new   int []{0, 0, 0x7f000000} :  渲染颜色数组
new   float []{0.0f, 0.7f, 1.0f} : 相对位置数组,可为null,  若为null,可为null,颜色沿渐变线均匀分布
Shader.TileMode. CLAMP :渲染器平铺模式
后面的代码和 RoundedBitmapDisplayer中内部类的代码一样。

FadeInbitmapDisplayer类是一个淡入的效果,原理是在图片显示的时候加入了下个淡入的动画。
      
      
  1. @Override
  2. public void display(Bitmap bitmap, ImageAware imageAware, LoadedFrom loadedFrom) {
  3. imageAware.setImageBitmap(bitmap);
  4. if ((animateFromNetwork && loadedFrom == LoadedFrom.NETWORK) ||
  5. (animateFromDisk && loadedFrom == LoadedFrom.DISC_CACHE) ||
  6. (animateFromMemory && loadedFrom == LoadedFrom.MEMORY_CACHE)) {
  7. animate(imageAware.getWrappedView(), durationMillis);
  8. }
  9. }
在图片显示的时候调用了了animate方法
       
       
  1. public static void animate(View imageView, int durationMillis) {
  2. if (imageView != null) {
  3. AlphaAnimation fadeImage = new AlphaAnimation(0, 1);
  4. fadeImage.setDuration(durationMillis);
  5. fadeImage.setInterpolator(new DecelerateInterpolator());
  6. imageView.startAnimation(fadeImage);
  7. }
  8. }
animate方法执行了一个动画。
了解了这几个显示方式的原理,我们可以继承这几个类实现自己的图片显示形式。
接下来我看来看一下如何使用。

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐