2019/06/18に開催されたPotatotips #62にて発表したスライドです
Yoshihiro Wada a.k.a. @e10dokupPotatotips #62 - 2019/06/18
View Slide
Yoshihiro Wada a.k.a. @e10dokupCyberAgant Inc. / AmebaAmeba
1 URL233.5
bumptech/glide square/picasso Image Loading Librarybumptech/glide 4.9.0ConstraintLayout
1URL
GlideApp.with(this).load(/* Image URL */).placeholder(/* Placeholder Drawable */).error(/* Error Drawable */).into(/* Target ImageView */)
2
ConstraintLayoutXML ImageViewandroid:id=“@+id/image_view”android:layout_width="0dp"android:layout_height=“0dp”app:layout_constraintDimensionRatio="H,3:2"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent"app:layout_constraintEnd_toEndOf=“parent" />
centerCropGlideApp.with(this).load(/* Image URL */).placeholder(/* Placeholder Drawable */).error(/* Error Drawable */).centerCrop().into(/* Target ImageView */)
XMLBindingAdapterandroid:id=“@+id/image_view”android:layout_width="0dp"android:layout_height=“0dp”app:layout_constraintDimensionRatio="H,3:2"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent"app:layout_constraintEnd_toEndOf=“parent”app:url=“@{imageUrl}” />
BindingAdapterBindingAdapter@BindingAdapter(“url")internal fun ImageView.loadImage(url: String?) {if (url == null) {setImageDrawable(null)return}GlideApp.with(this).load(url).centerCrop().into(this)}
3
DimensionRatio
API URLBindingAdapter DimensionRatio3android:id=“@+id/image_view”android:layout_width="0dp"android:layout_height=“0dp”app:width=“@{imageWidth}”app:height=“@{imageHeight}”app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent"app:layout_constraintEnd_toEndOf=“parent”app:url=“@{imageUrl}” />
width/height dimensionRatioBindingAdapterlayout_constraintDimensionRatio ConstraintLayoutBindingAdapter3@BindingAdapter(“width”, “height”)internal fun ImageView.setAspectRatio(width: Int, height: Int) {(layoutParams as ConstraintLayout.LayoutParams).dimensionRatio= “h,${width}:${height}”}
glide RequestListener DrawablerequestLayout() onMeasure()3.5
3.5override fun onResourceReady(resource: Drawable?,model: Any?,target: Target?,dataSource: DataSource?,isFirstResource: Boolean): Boolean {resource ?: return falseval width = resource.intrinsicWidth.toFloat()val height = resource.intrinsicHeight.toFloat()aspectRatio = width / heightrequestLayout()return false}
3.5override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {aspectRatio?.let {// ݱঢ়ͷԣ෯Λऔಘ͠ɺ৽نʹΞεϖΫτൺΛөͨ͠αΠζʹΓସ͑Δval width = MeasureSpec.getSize(widthMeasureSpec)val w = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY)val h = MeasureSpec.makeMeasureSpec((width / it).toInt(), MeasureSpec.EXACTLY)super.onMeasure(w, h)return}super.onMeasure(widthMeasureSpec, heightMeasureSpec)}
4
*NBHF7JFX*NBHF7JFX*NBHF7JFX
Load BitmapGlide CenterCrop() TransformationTransformation4:2 2 ->->Transformation
• override fun updateDiskCacheKey(messageDigest: MessageDigest)equals() / hashCode()• override fun transform( pool: BitmapPool, toTransform: Bitmap, outWidth: Int, outHeight: Int ): BitmaptoTransform BitmapBitmapTransformation
override fun transform(pool: BitmapPool,toTransform: Bitmap,outWidth: Int,outHeight: Int): Bitmap {val width = toTransform.widthval height = toTransform.heightaspectRatio = width / heightreturn when {aspectRatio >= 2 -> // 2:5 ΑΓԣcropWidth(pool, toTransform, imageWidth, imageHeight)else -> // ͦΕҎ֎ͷͱ͖ɺͦͷ··BitmapΛฦ٫toTransform}}
private fun transform(pool: BitmapPool,toTransform: Bitmap,width: Int,height: Int): Bitmap {val croppedWidth = (width * 0.5).toInt()return TransformationUtils.centerCrop(pool,toTransform,trimmedWidth,height)}
:pray: