Upgrade to Pro — share decks privately, control downloads, hide ads and more …

よしなに頑張る画像ロードの話 / image load mettya tsurai

よしなに頑張る画像ロードの話 / image load mettya tsurai

2019/06/18に開催されたPotatotips #62にて発表したスライドです

Yoshihiro WADA

June 18, 2019
Tweet

More Decks by Yoshihiro WADA

Other Decks in Programming

Transcript

  1. 2

  2. centerCrop GlideApp.with(this) .load(/* Image URL */) .placeholder(/* Placeholder Drawable */)

    .error(/* Error Drawable */) .centerCrop() .into(/* Target ImageView */)
  3. BindingAdapter BindingAdapter @BindingAdapter(“url") internal fun ImageView.loadImage(url: String?) { if (url

    == null) { setImageDrawable(null) return } GlideApp .with(this) .load(url) .centerCrop() .into(this) }
  4. 3

  5. API URL BindingAdapter DimensionRatio 3 <ImageView android: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}” />
  6. width/height dimensionRatio BindingAdapter layout_constraintDimensionRatio ConstraintLayout BindingAdapter 3 @BindingAdapter(“width”, “height”) internal

    fun ImageView.setAspectRatio(width: Int, height: Int) { (layoutParams as ConstraintLayout.LayoutParams).dimensionRatio = “h,${width}:${height}” }
  7. 3.5 override fun onResourceReady( resource: Drawable?, model: Any?, target: Target<Drawable>?,

    dataSource: DataSource?, isFirstResource: Boolean ): Boolean { resource ?: return false val width = resource.intrinsicWidth.toFloat() val height = resource.intrinsicHeight.toFloat() aspectRatio = width / height requestLayout() return false }
  8. 3.5 override 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) }
  9. 4

  10. • override fun updateDiskCacheKey(messageDigest: MessageDigest) equals() / hashCode() • override

    fun transform(
 pool: BitmapPool, toTransform: Bitmap, outWidth: Int, outHeight: Int
 ): Bitmap toTransform Bitmap BitmapTransformation
  11. override fun transform( pool: BitmapPool, toTransform: Bitmap, outWidth: Int, outHeight:

    Int ): Bitmap { val width = toTransform.width val height = toTransform.height aspectRatio = width / height return when { aspectRatio >= 2 -> // 2:5 ΑΓԣ௕ cropWidth(pool, toTransform, imageWidth, imageHeight) else -> // ͦΕҎ֎ͷͱ͖ɺͦͷ··BitmapΛฦ٫ toTransform } }
  12. 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 ) }