Universal-Image-Loader源码阅读(30)-CircleBitmapDisplayer

原创 2016年08月29日 17:07:39

展示圆形图片。把原始图片剪切成圆形图片进行展示。

就是把图片转变成原型图片画在展示器中(ImageAware)。

源码:

/**
 * Can display bitmap cropped by a circle. This implementation works only with ImageViews wrapped
 * in ImageViewAware.
 * <br />
 * If this implementation doesn't meet your needs then consider
 * <a href="https://github.com/vinc3m1/RoundedImageView">RoundedImageView</a> or
 * <a href="https://github.com/Pkmmte/CircularImageView">CircularImageView</a> projects for usage.
 *
 * @author Qualtagh, Sergey Tarasevich (nostra13[at]gmail[dot]com)
 * @since 1.9.5
 */
public class CircleBitmapDisplayer implements BitmapDisplayer {

	protected final Integer strokeColor;//边线颜色
	protected final float strokeWidth;//边线宽度

	public CircleBitmapDisplayer() {
		this(null);
	}

	public CircleBitmapDisplayer(Integer strokeColor) {
		this(strokeColor, 0);
	}

	public CircleBitmapDisplayer(Integer strokeColor, float strokeWidth) {
		this.strokeColor = strokeColor;
		this.strokeWidth = strokeWidth;
	}

	@Override
	public void display(Bitmap bitmap, ImageAware imageAware, LoadedFrom loadedFrom) {
		if (!(imageAware instanceof ImageViewAware)) {
			throw new IllegalArgumentException("ImageAware should wrap ImageView. ImageViewAware is expected.");
		}

		imageAware.setImageDrawable(new CircleDrawable(bitmap, strokeColor, strokeWidth));
	}

	public static class CircleDrawable extends Drawable {//圆形图片

		protected float radius;//整个图片半径

		protected final RectF mRect = new RectF();
		protected final RectF mBitmapRect;
		protected final BitmapShader bitmapShader;
		protected final Paint paint;//画图片
		protected final Paint strokePaint;//画边线
		protected final float strokeWidth;
		protected float strokeRadius;//不包括边框在内的图片半径

		public CircleDrawable(Bitmap bitmap, Integer strokeColor, float strokeWidth) {
			radius = Math.min(bitmap.getWidth(), bitmap.getHeight()) / 2;

			bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
			mBitmapRect = new RectF(0, 0, bitmap.getWidth(), bitmap.getHeight());

			paint = new Paint();
			paint.setAntiAlias(true);
			paint.setShader(bitmapShader);
			paint.setFilterBitmap(true);
			paint.setDither(true);

			if (strokeColor == null) {
				strokePaint = null;
			} else {
				strokePaint = new Paint();
				strokePaint.setStyle(Paint.Style.STROKE);
				strokePaint.setColor(strokeColor);
				strokePaint.setStrokeWidth(strokeWidth);
				strokePaint.setAntiAlias(true);
			}
			this.strokeWidth = strokeWidth;
			strokeRadius = radius - strokeWidth / 2;
		}

		@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(mBitmapRect, mRect, Matrix.ScaleToFit.FILL);
			bitmapShader.setLocalMatrix(shaderMatrix);
		}

		@Override
		public void draw(Canvas canvas) {
			canvas.drawCircle(radius, radius, radius, paint);//画图
			if (strokePaint != null) {//画边线
				canvas.drawCircle(radius, radius, strokeRadius, strokePaint);
			}
		}

		@Override
		public int getOpacity() {
			return PixelFormat.TRANSLUCENT;
		}

		@Override
		public void setAlpha(int alpha) {
			paint.setAlpha(alpha);
		}

		@Override
		public void setColorFilter(ColorFilter cf) {
			paint.setColorFilter(cf);
		}
	}
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

【Android框架】Android-universal-image-loader源码阅读(一)

图片加载是个非常常见的APP功能,而如今框架横行,在图片加载方面出现了很多图片加载框架:Android-universal-image-loader,pacasso,Fresco,Glide等等图片加...

Android Universal Image Loader 源码分析

Android Universal Image Loader 源码分析 本文为 Android 开源项目源码解析 中 Android Universal Image Loader 部分 项目地...

Android开源框架Universal-Image-Loader源码——关于LinkedHashMap的加载因子和初始容量分配

今天上午在CSDN的论坛里看到有朋友提的问题如下: /** @param maxSize Maximum sum of the sizes of the Bitmaps in this cache *...

图片加载库Android-Universal-Image-Loader关闭Log及源码分析

项目中用到了Android-Universal-Image-Loader作为图片加载框架,但是在使用的过程中,发现如果图片加载失败就会在LogCat中打印出一大段异常的信息,如下图所示: 如果这...

(源码分析)Android-Universal-Image-Loader (图片异步加载缓存库)对Bitmap的优化处理

通过前两篇,我们了解了 UIL的使用配置,UIL将服务器上的一张图片保存到本地,加载到内存的过程,以及UIL对DiscCache和MemoryCache的策略,但是还有一部分比较重要,因为它是我们的开...

Android-Universal-Image-Loader 源码 浅析

Android-Universal-Image-Loader 解读过年回来,工作不是很忙,想着花些时间把之前用到的/看过的 一些知识整理下,就从一个比较早期的图片加载框架 Android-Unive...

Android Universal Image Loader 源码分析

转自:http://a.codekk.com/detail/Android/huxian99/Android%20Universal%20Image%20Loader%20%E6%BA%90%E7%A...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)