From 2befb766ca88dc977be23a3e180590459de13fd0 Mon Sep 17 00:00:00 2001 From: tarsin Date: Mon, 10 Apr 2023 18:18:26 +0800 Subject: [PATCH] Kill LoadImageView --- .../hippo/ehviewer/ui/scene/GalleryHolder.kt | 16 +- .../com/hippo/ehviewer/widget/TileThumb.java | 52 ------- .../java/com/hippo/widget/LoadImageView.kt | 146 ------------------ app/src/main/res/layout/item_gallery_grid.xml | 8 +- 4 files changed, 15 insertions(+), 207 deletions(-) delete mode 100644 app/src/main/java/com/hippo/ehviewer/widget/TileThumb.java delete mode 100644 app/src/main/java/com/hippo/widget/LoadImageView.kt diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryHolder.kt b/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryHolder.kt index 84af1d11e..f7b09e5c4 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryHolder.kt +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryHolder.kt @@ -58,7 +58,9 @@ internal class ListGalleryHolder( EhAsyncThumb( model = galleryInfo.thumb, contentScale = ContentScale.Crop, - modifier = Modifier.height(height).aspectRatio(0.6666667F), + modifier = Modifier + .height(height) + .aspectRatio(0.6666667F), ) } } @@ -99,8 +101,16 @@ internal class GridGalleryHolder(private val binding: ItemGalleryGridBinding) : GalleryHolder(binding) { override fun bind(galleryInfo: GalleryInfo) { binding.run { - thumb.setThumbSize(galleryInfo.thumbWidth, galleryInfo.thumbHeight) - thumb.load(galleryInfo.thumb!!) + val aspect = (galleryInfo.thumbWidth.toFloat() / galleryInfo.thumbHeight).coerceIn(0.33F, 1.5F) + thumb.setContent { + Mdc3Theme { + EhAsyncThumb( + model = galleryInfo.thumb, + modifier = Modifier.aspectRatio(aspect), + contentScale = ContentScale.Crop, + ) + } + } var drawable = category.background val color = EhUtils.getCategoryColor(galleryInfo.category) if (drawable !is TriangleDrawable) { diff --git a/app/src/main/java/com/hippo/ehviewer/widget/TileThumb.java b/app/src/main/java/com/hippo/ehviewer/widget/TileThumb.java deleted file mode 100644 index 6affbf2fd..000000000 --- a/app/src/main/java/com/hippo/ehviewer/widget/TileThumb.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright 2016 Hippo Seven - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.hippo.ehviewer.widget; - -import android.content.Context; -import android.util.AttributeSet; - -import com.hippo.widget.LoadImageView; -import com.hippo.yorozuya.MathUtils; - -public class TileThumb extends LoadImageView { - - private static final float MIN_ASPECT = 0.33f; - private static final float MAX_ASPECT = 1.5f; - private static final float DEFAULT_ASPECT = 0.67f; - - public TileThumb(Context context) { - super(context); - } - - public TileThumb(Context context, AttributeSet attrs) { - super(context, attrs); - } - - public TileThumb(Context context, AttributeSet attrs, int defStyleAttr) { - super(context, attrs, defStyleAttr); - } - - public void setThumbSize(int thumbWidth, int thumbHeight) { - float aspect; - if (thumbWidth > 0 && thumbHeight > 0) { - aspect = MathUtils.clamp(thumbWidth / (float) thumbHeight, MIN_ASPECT, MAX_ASPECT); - } else { - aspect = DEFAULT_ASPECT; - } - setAspect(aspect); - } -} diff --git a/app/src/main/java/com/hippo/widget/LoadImageView.kt b/app/src/main/java/com/hippo/widget/LoadImageView.kt deleted file mode 100644 index 37101e0fd..000000000 --- a/app/src/main/java/com/hippo/widget/LoadImageView.kt +++ /dev/null @@ -1,146 +0,0 @@ -/* - * Copyright 2015-2016 Hippo Seven - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.hippo.widget - -import android.content.Context -import android.graphics.drawable.Drawable -import android.util.AttributeSet -import android.view.View -import androidx.annotation.IntDef -import androidx.appcompat.content.res.AppCompatResources -import coil.load -import coil.size.Size -import com.hippo.drawable.PreciselyClipDrawable -import com.hippo.ehviewer.EhApplication -import com.hippo.ehviewer.R -import com.hippo.ehviewer.coil.ehUrl - -private val application = EhApplication.application -private val errorDrawable = AppCompatResources.getDrawable(application, R.drawable.image_failed) - -open class LoadImageView @JvmOverloads constructor( - context: Context, - attrs: AttributeSet? = null, - defStyleAttr: Int = 0, -) : FixedAspectImageView(context, attrs, defStyleAttr), - View.OnClickListener, - View.OnLongClickListener { - private var mOffsetX = Integer.MIN_VALUE - private var mOffsetY = Integer.MIN_VALUE - private var mClipWidth = Integer.MIN_VALUE - private var mClipHeight = Integer.MIN_VALUE - private var mUrl: String? = null - - @RetryType - private val mRetryType: Int = - context.obtainStyledAttributes(attrs, R.styleable.LoadImageView, defStyleAttr, 0).run { - getInt(R.styleable.LoadImageView_retryType, 0).also { recycle() } - } - - private fun setRetry(canRetry: Boolean) { - when (mRetryType) { - RETRY_TYPE_CLICK -> { - setOnClickListener(if (canRetry) this else null) - isClickable = canRetry - } - - RETRY_TYPE_LONG_CLICK -> { - setOnLongClickListener(if (canRetry) this else null) - isLongClickable = canRetry - } - - RETRY_TYPE_NONE -> {} - } - } - - fun setClip(offsetX: Int, offsetY: Int, clipWidth: Int, clipHeight: Int) { - mOffsetX = offsetX - mOffsetY = offsetY - mClipWidth = clipWidth - mClipHeight = clipHeight - } - - fun resetClip() { - mOffsetX = Integer.MIN_VALUE - mOffsetY = Integer.MIN_VALUE - mClipWidth = Integer.MIN_VALUE - mClipHeight = Integer.MIN_VALUE - } - - fun load(url: String, crossfade: Boolean = true) { - mUrl = url - load(url) { - size(Size.ORIGINAL) - if (!crossfade) crossfade(false) - listener( - { setRetry(false) }, - { setRetry(true) }, - { _, _ -> - super.setImageDrawable(errorDrawable) - setRetry(true) - }, - { _, _ -> setRetry(false) }, - ) - ehUrl(url) - } - } - - private fun reload() { - mUrl?.let { this.load(it) } - } - - override fun setImageDrawable(drawable: Drawable?) { - var newDrawable = drawable - if (newDrawable != null) { - if (Integer.MIN_VALUE != mOffsetX) { - newDrawable = - PreciselyClipDrawable(newDrawable, mOffsetX, mOffsetY, mClipWidth, mClipHeight) - } - onPreSetImageDrawable(newDrawable, true) - } - super.setImageDrawable(newDrawable) - } - - override fun getDrawable(): Drawable? { - var newDrawable = super.getDrawable() - if (newDrawable is PreciselyClipDrawable) { - newDrawable = newDrawable.drawable - } - return newDrawable - } - - override fun onClick(v: View) { - reload() - } - - override fun onLongClick(v: View): Boolean { - reload() - return true - } - - open fun onPreSetImageDrawable(drawable: Drawable?, isTarget: Boolean) {} - - @IntDef(RETRY_TYPE_NONE, RETRY_TYPE_CLICK, RETRY_TYPE_LONG_CLICK) - @Retention(AnnotationRetention.SOURCE) - private annotation class RetryType - - companion object { - const val RETRY_TYPE_NONE = 0 - const val RETRY_TYPE_CLICK = 1 - const val RETRY_TYPE_LONG_CLICK = 2 - } -} diff --git a/app/src/main/res/layout/item_gallery_grid.xml b/app/src/main/res/layout/item_gallery_grid.xml index 0f13f01a2..ebec57487 100644 --- a/app/src/main/res/layout/item_gallery_grid.xml +++ b/app/src/main/res/layout/item_gallery_grid.xml @@ -15,7 +15,6 @@ --> - + android:layout_height="wrap_content" />