UniversalImageLoader 源码解析 -1.enam(枚举)使用

闲来无事,今天又重新打开了UniversalImageLoader源码,以下简称为UIL。

由于前几天做了一个基于SDK开发的项目,也就是给第三开发者用到的一个Library库。所以就打开了UIL源码用到的自己封装的库源码,来看下他们是怎么封装的,要来学习一下。

一、先来po一张图。


因为UIL库主要作用是用一款图片加载引擎库。所以涉及到图片核心操作代码core包,下载完毕图片一定是要进行缓存的,进行性能优化,所以添加了一个cache包,最后就是一些使用到的工具类了再添加一个utils包。


二、在我们开发过程中有可能会遇到这样的需求,就是通过一个字符串传给一个方法最后返回一个类型,这个类型就可以知道是什么东西了

之前我们的做法可能都是If else 然后把传过来的字符串进行匹配最后返回一个想要的类型。现在看UIL是怎么实现的


UIL需求:传入一个字符串返回是http https file 或者更多


调用方式是这样的

                String uri = "http://image.com/1.png";
		Scheme result = Scheme.ofUri(uri);
		Scheme expected = Scheme.HTTP;
		Assertions.assertThat(result).isEqualTo(expected);


他是通过一个Scheme内部类的ofUril方法最后返回Scheme

public interface ImageDownloader {
	/**
	 * Retrieves {@link InputStream} of image by URI.
	 *
	 * @param imageUri Image URI
	 * @param extra    Auxiliary object which was passed to {@link DisplayImageOptions.Builder#extraForDownloader(Object)
	 *                 DisplayImageOptions.extraForDownloader(Object)}; can be null
	 * @return {@link InputStream} of image
	 * @throws IOException                   if some I/O error occurs during getting image stream
	 * @throws UnsupportedOperationException if image URI has unsupported scheme(protocol)
	 */
	InputStream getStream(String imageUri, Object extra) throws IOException;

	/** Represents supported schemes(protocols) of URI. Provides convenient methods for work with schemes and URIs. */
	public enum Scheme {
		HTTP("http"), HTTPS("https"), FILE("file"), CONTENT("content"), ASSETS("assets"), DRAWABLE("drawable"), UNKNOWN("");

		private String scheme;
		private String uriPrefix;

		Scheme(String scheme) {
			this.scheme = scheme;
			uriPrefix = scheme + "://";
		}

		/**
		 * Defines scheme of incoming URI
		 *
		 * @param uri URI for scheme detection
		 * @return Scheme of incoming URI
		 */
		public static Scheme ofUri(String uri) {
			if (uri != null) {
				for (Scheme s : values()) {
					if (s.belongsTo(uri)) {
						return s;
					}
				}
			}
			return UNKNOWN;
		}

		private boolean belongsTo(String uri) {
			return uri.toLowerCase(Locale.US).startsWith(uriPrefix);
		}

		/** Appends scheme to incoming path */
		public String wrap(String path) {
			return uriPrefix + path;
		}

		/** Removed scheme part ("scheme://") from incoming URI */
		public String crop(String uri) {
			if (!belongsTo(uri)) {
				throw new IllegalArgumentException(String.format("URI [%1$s] doesn't have expected scheme [%2$s]", uri, scheme));
			}
			return uri.substring(uriPrefix.length());
		}
	}
}

通过枚举内部类来遍历,然后通过belongsTo方法来判断是否是想要的值

2.该内部类还有一个方法
wrap方法,
使用如下:
Scheme.FILE.wrap(targetFile.getAbsolutePath())
通过此方法只需要传入本地路径,就直接能得出file://+...这样的图片路径,是不是很方便。好了,今天就先分享到这里,我们下期再见

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序邦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值