Use jitpack libraries
This commit is contained in:
1 parent
d186a6ac3b
commit
e3113083bc
136 files changed
+473
-21172
No files matched your search
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2015 Hippo Seven
|
||||
* 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.
|
||||
@@ -18,32 +18,48 @@ package com.hippo.widget;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.TypedArray;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.drawable.BitmapDrawable;
|
||||
import android.graphics.drawable.Animatable;
|
||||
import android.graphics.drawable.ColorDrawable;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.graphics.drawable.TransitionDrawable;
|
||||
import android.support.annotation.DrawableRes;
|
||||
import android.support.annotation.IntDef;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.util.AttributeSet;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
|
||||
import com.hippo.conaco.Conaco;
|
||||
import com.hippo.conaco.ConacoTask;
|
||||
import com.hippo.conaco.DataContainer;
|
||||
import com.hippo.conaco.Unikery;
|
||||
import com.hippo.conaco.ValueHolder;
|
||||
import com.hippo.drawable.PreciselyClipDrawable;
|
||||
import com.hippo.ehviewer.EhApplication;
|
||||
import com.hippo.ehviewer.R;
|
||||
import com.hippo.image.ImageBitmap;
|
||||
import com.hippo.image.ImageDrawable;
|
||||
import com.hippo.image.RecycledException;
|
||||
|
||||
public class LoadImageView extends FixedAspectImageView implements Unikery<Bitmap>,
|
||||
View.OnClickListener, View.OnLongClickListener {
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
|
||||
public class LoadImageView extends FixedAspectImageView implements Unikery<ImageBitmap>,
|
||||
View.OnClickListener, View.OnLongClickListener, Animatable {
|
||||
|
||||
private static final String TAG = LoadImageView.class.getSimpleName();
|
||||
|
||||
@IntDef({RETRY_TYPE_NONE, RETRY_TYPE_CLICK, RETRY_TYPE_LONG_CLICK})
|
||||
@Retention(RetentionPolicy.SOURCE)
|
||||
private @interface RetryType {}
|
||||
|
||||
public static final int RETRY_TYPE_NONE = 0;
|
||||
public static final int RETRY_TYPE_CLICK = 1;
|
||||
public static final int RETRY_TYPE_LONG_CLICK = 2;
|
||||
|
||||
private int mTaskId = Unikery.INVALID_ID;
|
||||
|
||||
private Conaco<Bitmap> mConaco;
|
||||
private Conaco<ImageBitmap> mConaco;
|
||||
|
||||
private String mKey;
|
||||
private String mUrl;
|
||||
@@ -55,49 +71,29 @@ public class LoadImageView extends FixedAspectImageView implements Unikery<Bitma
|
||||
private int mClipWidth = Integer.MIN_VALUE;
|
||||
private int mClipHeight = Integer.MIN_VALUE;
|
||||
|
||||
private ValueHolder<Bitmap> mHolder;
|
||||
private int mRetryType;
|
||||
|
||||
private boolean mFailed;
|
||||
|
||||
private RetryType mRetryType = RetryType.NONE;
|
||||
|
||||
private static final RetryType[] sRetryTypeArray = {
|
||||
RetryType.NONE,
|
||||
RetryType.CLICK,
|
||||
RetryType.LONG_CLICK
|
||||
};
|
||||
|
||||
public enum RetryType {
|
||||
NONE (0),
|
||||
CLICK (1),
|
||||
LONG_CLICK (2);
|
||||
|
||||
RetryType(int ni) {
|
||||
nativeInt = ni;
|
||||
}
|
||||
final int nativeInt;
|
||||
}
|
||||
|
||||
public LoadImageView(Context context) {
|
||||
super(context);
|
||||
init(context, null, 0, 0);
|
||||
}
|
||||
|
||||
public LoadImageView(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
init(context, attrs);
|
||||
init(context, attrs, 0, 0);
|
||||
}
|
||||
|
||||
public LoadImageView(Context context, AttributeSet attrs, int defStyleAttr) {
|
||||
super(context, attrs, defStyleAttr);
|
||||
init(context, attrs);
|
||||
public LoadImageView(Context context, AttributeSet attrs, int defStyle) {
|
||||
super(context, attrs, defStyle);
|
||||
init(context, attrs, defStyle, 0);
|
||||
}
|
||||
|
||||
private void init(Context context, AttributeSet attrs) {
|
||||
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.LoadImageView);
|
||||
final int index = a.getInt(R.styleable.LoadImageView_retryType, -1);
|
||||
if (index >= 0) {
|
||||
setRetryType(sRetryTypeArray[index]);
|
||||
}
|
||||
@SuppressWarnings("WrongConstant")
|
||||
private void init(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
|
||||
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.LoadImageView, defStyleAttr, defStyleRes);
|
||||
setRetryType(a.getInt(R.styleable.LoadImageView_retryType, 0));
|
||||
a.recycle();
|
||||
|
||||
if (!isInEditMode()) {
|
||||
@@ -105,11 +101,23 @@ public class LoadImageView extends FixedAspectImageView implements Unikery<Bitma
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setTaskId(int id) {
|
||||
mTaskId = id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getTaskId() {
|
||||
return mTaskId;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onAttachedToWindow() {
|
||||
super.onAttachedToWindow();
|
||||
|
||||
if (!mFailed) { // Restore if not failed
|
||||
if (mFailed) {
|
||||
onFailure();
|
||||
} else {
|
||||
load(mKey, mUrl, mContainer, mUseNetwork);
|
||||
}
|
||||
}
|
||||
@@ -117,45 +125,73 @@ public class LoadImageView extends FixedAspectImageView implements Unikery<Bitma
|
||||
@Override
|
||||
protected void onDetachedFromWindow() {
|
||||
super.onDetachedFromWindow();
|
||||
|
||||
// Cancel
|
||||
mConaco.cancel(this);
|
||||
// Clear drawable
|
||||
clearDrawable();
|
||||
}
|
||||
|
||||
public void setRetryType(RetryType retryType) {
|
||||
if (mRetryType != retryType) {
|
||||
RetryType oldRetryType = mRetryType;
|
||||
mRetryType = retryType;
|
||||
|
||||
if (mFailed) {
|
||||
if (oldRetryType == RetryType.CLICK) {
|
||||
setOnClickListener(null);
|
||||
setClickable(false);
|
||||
} else if (oldRetryType == RetryType.LONG_CLICK) {
|
||||
setOnLongClickListener(null);
|
||||
setLongClickable(false);
|
||||
}
|
||||
|
||||
if (retryType == RetryType.CLICK) {
|
||||
setOnClickListener(this);
|
||||
} else if (retryType == RetryType.LONG_CLICK) {
|
||||
setOnLongClickListener(this);
|
||||
}
|
||||
private ImageDrawable getImageDrawable() {
|
||||
Drawable drawable = getDrawable();
|
||||
if (drawable instanceof TransitionDrawable) {
|
||||
TransitionDrawable transitionDrawable = (TransitionDrawable) drawable;
|
||||
if (transitionDrawable.getNumberOfLayers() == 2) {
|
||||
drawable = transitionDrawable.getDrawable(1);
|
||||
}
|
||||
}
|
||||
if (drawable instanceof PreciselyClipDrawable) {
|
||||
drawable = ((PreciselyClipDrawable) drawable).getWrappedDrawable();
|
||||
}
|
||||
if (drawable instanceof ImageDrawable) {
|
||||
return (ImageDrawable) drawable;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private void cancelRetryType() {
|
||||
if (mRetryType == RetryType.CLICK) {
|
||||
private void clearDrawable() {
|
||||
// Recycle ImageDrawable
|
||||
ImageDrawable imageDrawable = getImageDrawable();
|
||||
if (imageDrawable != null) {
|
||||
imageDrawable.recycle();
|
||||
}
|
||||
|
||||
// Set drawable null
|
||||
setImageDrawable(null);
|
||||
}
|
||||
|
||||
private void clearRetry() {
|
||||
if (mRetryType == RETRY_TYPE_CLICK) {
|
||||
setOnClickListener(null);
|
||||
setClickable(false);
|
||||
} else if (mRetryType == RetryType.LONG_CLICK) {
|
||||
} else if (mRetryType == RETRY_TYPE_LONG_CLICK) {
|
||||
setOnLongClickListener(null);
|
||||
setLongClickable(false);
|
||||
}
|
||||
}
|
||||
|
||||
public void load(String key, String url) {
|
||||
load(key, url, null, true);
|
||||
public void setRetryType(@RetryType int retryType) {
|
||||
if (mRetryType != retryType) {
|
||||
int oldRetryType = mRetryType;
|
||||
mRetryType = retryType;
|
||||
|
||||
if (mFailed) {
|
||||
if (oldRetryType == RETRY_TYPE_CLICK) {
|
||||
setOnClickListener(null);
|
||||
setClickable(false);
|
||||
} else if (oldRetryType == RETRY_TYPE_LONG_CLICK) {
|
||||
setOnLongClickListener(null);
|
||||
setLongClickable(false);
|
||||
}
|
||||
|
||||
if (retryType == RETRY_TYPE_CLICK) {
|
||||
setOnClickListener(this);
|
||||
} else if (retryType == RETRY_TYPE_LONG_CLICK) {
|
||||
setOnLongClickListener(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void setClip(int offsetX, int offsetY, int clipWidth, int clipHeight) {
|
||||
@@ -172,28 +208,28 @@ public class LoadImageView extends FixedAspectImageView implements Unikery<Bitma
|
||||
mClipHeight = Integer.MIN_VALUE;
|
||||
}
|
||||
|
||||
public void load(String key, String url) {
|
||||
load(key, url, null, true);
|
||||
}
|
||||
|
||||
public void load(String key, String url, boolean useNetwork) {
|
||||
load(key, url, null, useNetwork);
|
||||
}
|
||||
|
||||
public void load(String key, String url, DataContainer container, boolean useNetwork) {
|
||||
if (url == null || (key == null && container == null)) {
|
||||
return;
|
||||
}
|
||||
|
||||
mFailed = false;
|
||||
cancelRetryType();
|
||||
|
||||
if (url == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (key == null && container == null) {
|
||||
return;
|
||||
}
|
||||
clearRetry();
|
||||
|
||||
mKey = key;
|
||||
mUrl = url;
|
||||
mContainer = container;
|
||||
mUseNetwork = useNetwork;
|
||||
|
||||
ConacoTask.Builder<Bitmap> builder = new ConacoTask.Builder<Bitmap>()
|
||||
ConacoTask.Builder<ImageBitmap> builder = new ConacoTask.Builder<ImageBitmap>()
|
||||
.setUnikery(this)
|
||||
.setKey(key)
|
||||
.setUrl(url)
|
||||
@@ -217,66 +253,45 @@ public class LoadImageView extends FixedAspectImageView implements Unikery<Bitma
|
||||
mKey = null;
|
||||
mUrl = null;
|
||||
mContainer = null;
|
||||
|
||||
removeDrawableAndHolder();
|
||||
clearDrawable();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setTaskId(int id) {
|
||||
mTaskId = id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getTaskId() {
|
||||
return mTaskId;
|
||||
}
|
||||
|
||||
private void removeDrawableAndHolder() {
|
||||
// Remove drawable
|
||||
setImageDrawable(null);
|
||||
|
||||
// Remove holder
|
||||
if (mHolder != null) {
|
||||
mHolder.release(this);
|
||||
mHolder = null;
|
||||
public void onMiss(int source) {
|
||||
if (source == Conaco.SOURCE_MEMORY) {
|
||||
clearDrawable();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMiss(Conaco.Source source) {
|
||||
if (source == Conaco.Source.MEMORY) {
|
||||
removeDrawableAndHolder();
|
||||
public void onRequest() {}
|
||||
|
||||
@Override
|
||||
public void onProgress(long singleReceivedSize, long receivedSize, long totalSize) {}
|
||||
|
||||
@Override
|
||||
public void onWait() {
|
||||
clearDrawable();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onGetValue(@NonNull ImageBitmap value, int source) {
|
||||
Drawable drawable;
|
||||
try {
|
||||
drawable = new ImageDrawable(value);
|
||||
} catch (RecycledException e) {
|
||||
// The image might be recycled because it is removed from memory cache.
|
||||
Log.d(TAG, "The image is recycled", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRequest() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onProgress(long singleReceivedSize, long receivedSize, long totalSize) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onGetObject(@NonNull ValueHolder<Bitmap> holder, Conaco.Source source) {
|
||||
// Release
|
||||
mKey = null;
|
||||
mUrl = null;
|
||||
mContainer = null;
|
||||
|
||||
holder.obtain(this);
|
||||
|
||||
removeDrawableAndHolder();
|
||||
|
||||
mHolder = holder;
|
||||
Bitmap bitmap = holder.getValue();
|
||||
Drawable drawable = new BitmapDrawable(getResources(), bitmap);
|
||||
clearDrawable();
|
||||
|
||||
if (Integer.MIN_VALUE != mOffsetX) {
|
||||
drawable = new PreciselyClipDrawable(drawable, mOffsetX, mOffsetY, mClipWidth, mClipHeight);
|
||||
}
|
||||
|
||||
if ((source == Conaco.Source.DISK || source == Conaco.Source.NETWORK)) {
|
||||
if ((source == Conaco.SOURCE_DISK || source == Conaco.SOURCE_NETWORK)) {
|
||||
Drawable[] layers = new Drawable[2];
|
||||
layers[0] = new ColorDrawable(Color.TRANSPARENT);
|
||||
layers[1] = drawable;
|
||||
@@ -290,23 +305,14 @@ public class LoadImageView extends FixedAspectImageView implements Unikery<Bitma
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSetDrawable(Drawable drawable) {
|
||||
mKey = null;
|
||||
mUrl = null;
|
||||
mContainer = null;
|
||||
removeDrawableAndHolder();
|
||||
setImageDrawable(drawable);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure() {
|
||||
mFailed = true;
|
||||
removeDrawableAndHolder();
|
||||
clearDrawable();
|
||||
setImageDrawable(getContext().getResources().getDrawable(R.drawable.image_failed));
|
||||
if (mRetryType == RetryType.CLICK) {
|
||||
if (mRetryType == RETRY_TYPE_CLICK) {
|
||||
setOnClickListener(this);
|
||||
} else if (mRetryType == RetryType.LONG_CLICK) {
|
||||
} else if (mRetryType == RETRY_TYPE_LONG_CLICK) {
|
||||
setOnLongClickListener(this);
|
||||
} else {
|
||||
// Can't retry, so release
|
||||
@@ -319,11 +325,32 @@ public class LoadImageView extends FixedAspectImageView implements Unikery<Bitma
|
||||
@Override
|
||||
public void onCancel() {
|
||||
mFailed = false;
|
||||
cancelRetryType();
|
||||
// release
|
||||
mKey = null;
|
||||
mUrl = null;
|
||||
mContainer = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start() {
|
||||
ImageDrawable drawable = getImageDrawable();
|
||||
if (drawable != null) {
|
||||
drawable.start();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() {
|
||||
ImageDrawable drawable = getImageDrawable();
|
||||
if (drawable != null) {
|
||||
drawable.stop();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isRunning() {
|
||||
ImageDrawable drawable = getImageDrawable();
|
||||
if (drawable != null) {
|
||||
return drawable.isRunning();
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in new issue
Block a user