Add FixedThumb to fix thumbnail size

This commit is contained in:
seven332 committed 2019-01-13 02:38:48 +08:00
1 parent 1d5b26ee51
commit f1bab7e3d4
6 files changed
+107 -7

No files matched your search

@@ -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);
}
}
@@ -239,11 +239,13 @@ public class LoadImageView extends FixedAspectImageView implements Unikery<Image
public void load(Drawable drawable) {
unload();
onPreSetImageDrawable(drawable, true);
setImageDrawable(drawable);
}
public void load(@DrawableRes int id) {
unload();
onPreSetImageResource(id, true);
setImageResource(id);
}
@@ -290,6 +292,7 @@ public class LoadImageView extends FixedAspectImageView implements Unikery<Image
drawable = new PreciselyClipDrawable(drawable, mOffsetX, mOffsetY, mClipWidth, mClipHeight);
}
onPreSetImageDrawable(drawable, true);
if ((source == Conaco.SOURCE_DISK || source == Conaco.SOURCE_NETWORK) && isShown()) {
Drawable[] layers = new Drawable[2];
layers[0] = new ColorDrawable(Color.TRANSPARENT);
@@ -308,7 +311,9 @@ public class LoadImageView extends FixedAspectImageView implements Unikery<Image
public void onFailure() {
mFailed = true;
clearDrawable();
setImageDrawable(DrawableManager.getVectorDrawable(getContext(), R.drawable.image_failed));
Drawable drawable = DrawableManager.getVectorDrawable(getContext(), R.drawable.image_failed);
onPreSetImageDrawable(drawable, true);
setImageDrawable(drawable);
if (mRetryType == RETRY_TYPE_CLICK) {
setOnClickListener(this);
} else if (mRetryType == RETRY_TYPE_LONG_CLICK) {
@@ -362,4 +367,8 @@ public class LoadImageView extends FixedAspectImageView implements Unikery<Image
load(mKey, mUrl, mContainer, true);
return true;
}
public void onPreSetImageDrawable(Drawable drawable, boolean isTarget) { }
public void onPreSetImageResource(int resId, boolean isTarget) { }
}