我有一个ImageView
类的扩展函数。我已经实现了一些关于如何根据传递的参数加载图像的逻辑。但是在这里我被卡住了。
这些fit()
,centerCrop()
等返回毕加索的RequestCreator
,我什至无法构造(它具有程序包专用的构造函数)以便以后修改它(基于参数)。
我只是不知道该怎么做。我设法做到这一点的唯一方法是如下所示(警告:眼睛会开始流血)。我找不到做到这一点的“正常”,“良好”方式。
所以我问你:我应该怎么做呢?
fun ImageView.load(resId: Int, centerCrop: Boolean = true, fit: Boolean = true) {
// Improve this, there must be a better way
if (centerCrop && fit) {
Picasso.get()
.load(resId)
.fit()
.centerCrop()
.into(this)
} else if (centerCrop && !fit) {
Picasso.get()
.load(resId)
.centerCrop()
.into(this)
} else if (!centerCrop && fit) {
Picasso.get()
.load(resId)
.fit()
.into(this)
} else if (!centerCrop && !fit) {
Picasso.get()
.load(resId)
.into(this)
}
}