Fix cover image in lists

This commit is contained in:
Koitharu
2021-11-17 19:08:13 +02:00
parent c853fae820
commit 049f9fa625
3 changed files with 14 additions and 39 deletions

View File

@@ -6,7 +6,7 @@ import android.widget.LinearLayout
import androidx.appcompat.widget.AppCompatImageView import androidx.appcompat.widget.AppCompatImageView
import androidx.core.content.withStyledAttributes import androidx.core.content.withStyledAttributes
import org.koitharu.kotatsu.R import org.koitharu.kotatsu.R
import org.koitharu.kotatsu.utils.ext.resolveAdjustedSize import kotlin.math.roundToInt
class CoverImageView @JvmOverloads constructor( class CoverImageView @JvmOverloads constructor(
@@ -17,47 +17,22 @@ class CoverImageView @JvmOverloads constructor(
init { init {
context.withStyledAttributes(attrs, R.styleable.CoverImageView, defStyleAttr) { context.withStyledAttributes(attrs, R.styleable.CoverImageView, defStyleAttr) {
orientation = getInt(R.styleable.CoverImageView_android_orientation, HORIZONTAL) orientation = getInt(R.styleable.CoverImageView_android_orientation, orientation)
} }
} }
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
val w: Int super.onMeasure(widthMeasureSpec, heightMeasureSpec)
val h: Int val desiredWidth: Int
val desiredHeight: Int
if (orientation == VERTICAL) { if (orientation == VERTICAL) {
val desiredHeight = (drawable?.intrinsicHeight?.coerceAtLeast(0) ?: 0) + desiredHeight = measuredHeight
paddingTop + paddingBottom desiredWidth = (desiredHeight * ASPECT_RATIO_WIDTH / ASPECT_RATIO_HEIGHT).roundToInt()
h = resolveAdjustedSize(
desiredHeight.coerceAtLeast(suggestedMinimumHeight),
maxHeight,
heightMeasureSpec
)
val desiredWidth =
(h * ASPECT_RATIO_WIDTH / ASPECT_RATIO_HEIGHT).toInt() + paddingLeft + paddingRight
w = resolveAdjustedSize(
desiredWidth.coerceAtLeast(suggestedMinimumWidth),
maxWidth,
widthMeasureSpec
)
} else { } else {
val desiredWidth = (drawable?.intrinsicWidth?.coerceAtLeast(0) ?: 0) + desiredWidth = measuredWidth
paddingLeft + paddingRight desiredHeight = (desiredWidth * ASPECT_RATIO_HEIGHT / ASPECT_RATIO_WIDTH).roundToInt()
w = resolveAdjustedSize(
desiredWidth.coerceAtLeast(suggestedMinimumWidth),
maxWidth,
widthMeasureSpec
)
val desiredHeight =
(w * ASPECT_RATIO_HEIGHT / ASPECT_RATIO_WIDTH).toInt() + paddingTop + paddingBottom
h = resolveAdjustedSize(
desiredHeight.coerceAtLeast(suggestedMinimumHeight),
maxHeight,
heightMeasureSpec
)
} }
val widthSize = resolveSizeAndState(w, widthMeasureSpec, 0) setMeasuredDimension(desiredWidth, desiredHeight)
val heightSize = resolveSizeAndState(h, heightMeasureSpec, 0)
setMeasuredDimension(widthSize, heightSize)
} }
companion object { companion object {

View File

@@ -11,15 +11,15 @@
<com.google.android.material.card.MaterialCardView <com.google.android.material.card.MaterialCardView
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:layout_margin="8dp" android:layout_margin="8dp"
app:cardCornerRadius="4dp" app:cardCornerRadius="4dp"
app:cardElevation="4dp"> app:cardElevation="4dp">
<org.koitharu.kotatsu.base.ui.widgets.CoverImageView <org.koitharu.kotatsu.base.ui.widgets.CoverImageView
android:id="@+id/imageView_cover" android:id="@+id/imageView_cover"
android:layout_width="match_parent" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:orientation="vertical" android:orientation="vertical"
android:scaleType="centerCrop" android:scaleType="centerCrop"
tools:src="@tools:sample/backgrounds/scenic" /> tools:src="@tools:sample/backgrounds/scenic" />

View File

@@ -10,7 +10,7 @@
<com.google.android.material.card.MaterialCardView <com.google.android.material.card.MaterialCardView
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:layout_margin="8dp" android:layout_margin="8dp"
app:cardElevation="4dp"> app:cardElevation="4dp">