我只需要加载一张图片,有没有能直接一行代码就可以调用,其他的设置全部默认?
rayzeng0221 opened this issue · 1 comments
rayzeng0221 commented
我只需要加载一张图片,有没有能直接一行代码就可以调用,其他的设置全部默认?
iielse commented
框架需要考虑的是易用性和拓展性。
比如Glide.你肯定也会二次封装啊
fun ImageView.load(url: String?, placeHolder: Int? = null, callback:(Boolean)->Unit) {
val requestOptions =
if (placeHolder != null) RequestOptions().placeholder(placeHolder).error(placeHolder)
else RequestOptions()
Glide.with(this)
.load(url)
.apply(requestOptions)
.addListener(object : RequestListener<Drawable> {
override fun onLoadFailed(
e: GlideException?,
model: Any?,
target: Target<Drawable>?,
isFirstResource: Boolean
): Boolean {
callback(false)
return false
}
override fun onResourceReady(
resource: Drawable?,
model: Any?,
target: Target<Drawable>?,
dataSource: DataSource?,
isFirstResource: Boolean
): Boolean {
callback(true)
return false
}
})
.into(this)
}
一行代码使用
loadingView.isVisible = true
imageView.load("abc") {
loadingView.isVisible = false
}
所以一样的你自己封装个方法再调用不就都是一行代码吗
fun ImageView.showViewer(clickedData: Photo) { //
val dataList: List<Photo> = listOf(clickedData)
val builder = ImageViewerBuilder(
context = this.context,
dataProvider = SimpleDataProvider(clickedData, dataList),
imageLoader = SimpleImageLoader(),
transformer = object: Transformer() { getView() = this },
)
builder.show()
}
后面用的时候就
imageView.showViewer(data)
这不就一行代码了嘛?