diff --git a/app/src/main/java/com/hippo/ehviewer/widget/FixedThumb.java b/app/src/main/java/com/hippo/ehviewer/widget/FixedThumb.java new file mode 100644 index 000000000..fd662c48f --- /dev/null +++ b/app/src/main/java/com/hippo/ehviewer/widget/FixedThumb.java @@ -0,0 +1,79 @@ +/* + * Copyright 2019 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.content.res.TypedArray; +import android.graphics.drawable.Drawable; +import android.util.AttributeSet; +import com.hippo.ehviewer.R; +import com.hippo.widget.LoadImageView; + +public class FixedThumb extends LoadImageView { + + private float minAspect; + private float maxAspect; + + public FixedThumb(Context context) { + super(context); + init(context, null, 0, 0); + } + + public FixedThumb(Context context, AttributeSet attrs) { + super(context, attrs); + init(context, attrs, 0, 0); + } + + public FixedThumb(Context context, AttributeSet attrs, int defStyle) { + super(context, attrs, defStyle); + init(context, attrs, defStyle, 0); + } + + private void init(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { + TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FixedThumb, defStyleAttr, defStyleRes); + minAspect = a.getFloat(R.styleable.FixedThumb_minAspect, 0.0f); + maxAspect = a.getFloat(R.styleable.FixedThumb_maxAspect, 0.0f); + a.recycle(); + } + + public void setFix(float minAspect, float maxAspect) { + this.minAspect = minAspect; + this.maxAspect = maxAspect; + } + + @Override + public void onPreSetImageDrawable(Drawable drawable, boolean isTarget) { + if (isTarget && drawable != null) { + int width = drawable.getIntrinsicWidth(); + int height = drawable.getIntrinsicHeight(); + if (width > 0 && height > 0) { + float aspect = (float) width / (float) height; + if (aspect < maxAspect && aspect > minAspect) { + setScaleType(ScaleType.CENTER_CROP); + return; + } + } + } + + setScaleType(ScaleType.FIT_CENTER); + } + + @Override + public void onPreSetImageResource(int resId, boolean isTarget) { + setScaleType(ScaleType.FIT_CENTER); + } +} diff --git a/app/src/main/java/com/hippo/widget/LoadImageView.java b/app/src/main/java/com/hippo/widget/LoadImageView.java index b2865b5bf..e943697cb 100644 --- a/app/src/main/java/com/hippo/widget/LoadImageView.java +++ b/app/src/main/java/com/hippo/widget/LoadImageView.java @@ -239,11 +239,13 @@ public class LoadImageView extends FixedAspectImageView implements Unikery - + android:layout_height="@dimen/gallery_list_thumb_height" + app:retryType="longClick" + app:minAspect="0.3" + app:maxAspect="2.0"/> - + app:retryType="click" + app:minAspect="0.3" + app:maxAspect="2.0"/> - + auto:retryType="longClick" + auto:minAspect="0.5" + auto:maxAspect="0.8"/> + + + + +