Commit 8a4e09d8 authored by qq247321453's avatar qq247321453

ImageLoader

parent d3ef672c
...@@ -92,7 +92,11 @@ public interface Constants { ...@@ -92,7 +92,11 @@ public interface Constants {
String UNKNOWN_IMAGE = "unknown.jpg"; String UNKNOWN_IMAGE = "unknown.jpg";
String YDK_FILE_EX = ".ydk"; String YDK_FILE_EX = ".ydk";
int[] CORE_SKIN_BG_SIZE = new int[]{1280, 720}; int[] CORE_SKIN_BG_SIZE = new int[]{1280, 720};
int[] CORE_SKIN_CARD_MINI_SIZE = new int[]{177, 254};
int[] CORE_SKIN_CARD_DECK_SIZE = new int[]{44, 64};
int[] CORE_SKIN_CARD_LIST_SIZE = new int[]{177, 254};
int[] CORE_SKIN_CARD_DETAIL_SIZE = new int[]{354, 508};
int[] CORE_SKIN_CARD_COVER_SIZE = new int[]{177, 254}; int[] CORE_SKIN_CARD_COVER_SIZE = new int[]{177, 254};
int[] CORE_SKIN_AVATAR_SIZE = new int[]{128, 128}; int[] CORE_SKIN_AVATAR_SIZE = new int[]{128, 128};
boolean SUPPORT_BPG = true; boolean SUPPORT_BPG = true;
......
...@@ -44,13 +44,18 @@ import cn.garymb.ygomobile.utils.NetUtils; ...@@ -44,13 +44,18 @@ import cn.garymb.ygomobile.utils.NetUtils;
import static com.bumptech.glide.Glide.with; import static com.bumptech.glide.Glide.with;
public class ImageLoader implements Closeable { public class ImageLoader implements Closeable {
public static final ImageLoader sImageLoader = new ImageLoader(); public enum Type {
//origin size
public static ImageLoader get() { origin,
return sImageLoader; //44x64
deck,
//177x254
list,
//354x508
detail
} }
private static class Cache{ private static class Cache {
private final byte[] data; private final byte[] data;
private final String name; private final String name;
...@@ -67,7 +72,8 @@ public class ImageLoader implements Closeable { ...@@ -67,7 +72,8 @@ public class ImageLoader implements Closeable {
private final Map<Long, Cache> zipDataCache = new ConcurrentHashMap<>(); private final Map<Long, Cache> zipDataCache = new ConcurrentHashMap<>();
private ZipFile mDefaultZipFile; private ZipFile mDefaultZipFile;
private File mPicsFile; private File mPicsFile;
public ImageLoader(){
public ImageLoader() {
this(false); this(false);
} }
...@@ -76,7 +82,7 @@ public class ImageLoader implements Closeable { ...@@ -76,7 +82,7 @@ public class ImageLoader implements Closeable {
} }
private ZipFile openPicsZip() { private ZipFile openPicsZip() {
if(mPicsFile == null) { if (mPicsFile == null) {
mPicsFile = new File(AppsSettings.get().getResourcePath(), Constants.CORE_PICS_ZIP); mPicsFile = new File(AppsSettings.get().getResourcePath(), Constants.CORE_PICS_ZIP);
} }
if (mDefaultZipFile == null) { if (mDefaultZipFile == null) {
...@@ -91,11 +97,11 @@ public class ImageLoader implements Closeable { ...@@ -91,11 +97,11 @@ public class ImageLoader implements Closeable {
return mDefaultZipFile; return mDefaultZipFile;
} }
public void resume(){ public void resume() {
} }
public void clearZipCache(){ public void clearZipCache() {
//关闭zip //关闭zip
for (ZipFile zipFile : zipFileCache.values()) { for (ZipFile zipFile : zipFileCache.values()) {
IOUtils.closeZip(zipFile); IOUtils.closeZip(zipFile);
...@@ -115,24 +121,24 @@ public class ImageLoader implements Closeable { ...@@ -115,24 +121,24 @@ public class ImageLoader implements Closeable {
zipDataCache.clear(); zipDataCache.clear();
} }
private void bind(final byte[] data, String name, ImageView imageview, Drawable pre, int[] size) { private void bind(final byte[] data, String name, ImageView imageview, Drawable pre, @NonNull Type type) {
if (BuildConfig.DEBUG_MODE) { if (BuildConfig.DEBUG_MODE) {
Log.v(TAG, "bind data:" + name + ", size=" + (size == null ? "null" : size[0] + "x" + size[1])); Log.v(TAG, "bind data:" + name + ", type=" + type);
} }
bindT(data, name, imageview, pre, size); bindT(data, name, imageview, pre, type);
} }
private void bind(final Uri uri, String name, ImageView imageview, Drawable pre, int[] size) { private void bind(final Uri uri, String name, ImageView imageview, Drawable pre,@NonNull Type type) {
if (BuildConfig.DEBUG_MODE) { if (BuildConfig.DEBUG_MODE) {
Log.v(TAG, "bind uri:" + name + ", size=" + (size == null ? "null" : size[0] + "x" + size[1])); Log.v(TAG, "bind uri:" + name + ", type=" + type);
} }
bindT(uri, name, imageview, pre, size); bindT(uri, name, imageview, pre, type);
} }
private <T> void setDefaults(@NonNull DrawableTypeRequest<T> resource, String name, private <T> void setDefaults(@NonNull DrawableTypeRequest<T> resource, String name,
@Nullable com.bumptech.glide.load.Key signature, @Nullable com.bumptech.glide.load.Key signature,
@Nullable Drawable pre, @Nullable Drawable pre,
@Nullable int[] size) { @NonNull Type type) {
if (pre != null) { if (pre != null) {
resource.placeholder(pre); resource.placeholder(pre);
} else { } else {
...@@ -140,10 +146,24 @@ public class ImageLoader implements Closeable { ...@@ -140,10 +146,24 @@ public class ImageLoader implements Closeable {
} }
resource.error(R.drawable.unknown); resource.error(R.drawable.unknown);
resource.animate(R.anim.push_in); resource.animate(R.anim.push_in);
if (size != null) { if (type != Type.origin) {
resource.override(size[0], size[1]); int[] size = null;
switch (type) {
case deck:
size = Constants.CORE_SKIN_CARD_DECK_SIZE;
break;
case list:
size = Constants.CORE_SKIN_CARD_LIST_SIZE;
break;
case detail:
size = Constants.CORE_SKIN_CARD_DETAIL_SIZE;
break;
}
if (size != null) {
resource.override(size[0], size[1]);
}
} }
if(signature != null) { if (signature != null) {
resource.signature(signature); resource.signature(signature);
} }
String ex = FileUtils.getFileExpansion(name); String ex = FileUtils.getFileExpansion(name);
...@@ -152,38 +172,32 @@ public class ImageLoader implements Closeable { ...@@ -152,38 +172,32 @@ public class ImageLoader implements Closeable {
} }
} }
private <T> void bindT(final T data, String name, ImageView imageview, Drawable pre, int[] size) { private <T> void bindT(final T data, String name, ImageView imageview, Drawable pre, @NonNull Type type) {
try { try {
DrawableTypeRequest<T> resource = with(imageview.getContext()).load(data); DrawableTypeRequest<T> resource = with(imageview.getContext()).load(data);
if (size == null) { setDefaults(resource, name, new StringSignature(name + ":" + type), pre, type);
setDefaults(resource, name, new StringSignature(name), pre, size);
resource.signature(new StringSignature(name));
} else {
setDefaults(resource, name, new StringSignature(name + ":" + size[0] + "x" + size[1]), pre, size);
}
resource.into(imageview); resource.into(imageview);
} catch (Exception e) { } catch (Exception e) {
Log.e(TAG, "$", e); Log.e(TAG, "$", e);
} }
} }
private void bind(final File file, ImageView imageview, Drawable pre, int[] size) { private void bind(final File file, ImageView imageview, Drawable pre, @NonNull Type type) {
if (BuildConfig.DEBUG_MODE) { if (BuildConfig.DEBUG_MODE) {
Log.v(TAG, "bind file:" + file.getPath() + ", size=" + (size == null ? "null" : size[0] + "x" + size[1])); Log.v(TAG, "bind file:" + file.getPath() + ", type=" + type);
} }
try { try {
DrawableTypeRequest<File> resource = with(imageview.getContext()).load(file); DrawableTypeRequest<File> resource = with(imageview.getContext()).load(file);
long key = size == null ? 0:(size[0] * size[1]);
setDefaults(resource, file.getName(), setDefaults(resource, file.getName(),
new MediaStoreSignature("image/*", new MediaStoreSignature("image/*",
file.lastModified() + key, 0), pre, size); file.lastModified(), type.ordinal()), pre, type);
resource.into(imageview); resource.into(imageview);
} catch (Exception e) { } catch (Exception e) {
Log.e(TAG, "$", e); Log.e(TAG, "$", e);
} }
} }
private boolean bindInZip(ImageView imageView, long code, Drawable pre, ZipFile zipFile, String nameWithEx, int[] size) { private boolean bindInZip(ImageView imageView, long code, Drawable pre, ZipFile zipFile, String nameWithEx, @NonNull Type type) {
ZipEntry entry; ZipEntry entry;
InputStream inputStream = null; InputStream inputStream = null;
ByteArrayOutputStream outputStream; ByteArrayOutputStream outputStream;
...@@ -195,10 +209,10 @@ public class ImageLoader implements Closeable { ...@@ -195,10 +209,10 @@ public class ImageLoader implements Closeable {
outputStream = new ByteArrayOutputStream(); outputStream = new ByteArrayOutputStream();
IOUtils.copy(inputStream, outputStream); IOUtils.copy(inputStream, outputStream);
byte[] data = outputStream.toByteArray(); byte[] data = outputStream.toByteArray();
if(useCache){ if (useCache) {
zipDataCache.put(code, new Cache(data, nameWithEx)); zipDataCache.put(code, new Cache(data, nameWithEx));
} }
bind(data, nameWithEx, imageView, pre, size); bind(data, nameWithEx, imageView, pre, type);
bind = true; bind = true;
} }
} catch (Exception e) { } catch (Exception e) {
...@@ -221,25 +235,13 @@ public class ImageLoader implements Closeable { ...@@ -221,25 +235,13 @@ public class ImageLoader implements Closeable {
} }
} }
public void bindImage(ImageView imageview, long code, @NonNull Type type) {
/** bindImage(imageview, code, null, type);
* 177x254
*/
public void bindImage(ImageView imageview, long code) {
bindImage(imageview, code, null, Constants.CORE_SKIN_CARD_MINI_SIZE);
}
/**
* @param big true则是原始大小
*/
@Deprecated
public void bindImage(ImageView imageview, long code, Drawable pre, boolean big) {
bindImage(imageview, code, pre, big ? null : Constants.CORE_SKIN_CARD_MINI_SIZE);
} }
public void bindImage(ImageView imageview, long code, Drawable pre, int[] size) { public void bindImage(ImageView imageview, long code, Drawable pre, @NonNull Type type) {
if (BuildConfig.DEBUG_MODE) { if (BuildConfig.DEBUG_MODE) {
Log.v(TAG, "bind image:" + code + ", size=" + (size == null ? "null" : size[0] + "x" + size[1])); Log.v(TAG, "bind image:" + code + ", type=" + type);
} }
String name = Constants.CORE_IMAGE_PATH + "/" + code; String name = Constants.CORE_IMAGE_PATH + "/" + code;
String name_ex = Constants.CORE_EXPANSIONS_IMAGE_PATH + "/" + code; String name_ex = Constants.CORE_EXPANSIONS_IMAGE_PATH + "/" + code;
...@@ -248,18 +250,18 @@ public class ImageLoader implements Closeable { ...@@ -248,18 +250,18 @@ public class ImageLoader implements Closeable {
File file = new File(AppsSettings.get().getResourcePath(), name + ex); File file = new File(AppsSettings.get().getResourcePath(), name + ex);
File file_ex = new File(AppsSettings.get().getResourcePath(), name_ex + ex); File file_ex = new File(AppsSettings.get().getResourcePath(), name_ex + ex);
if (file_ex.exists()) { if (file_ex.exists()) {
bind(file_ex, imageview, pre, size); bind(file_ex, imageview, pre, type);
return; return;
} else if (file.exists()) { } else if (file.exists()) {
bind(file, imageview, pre, size); bind(file, imageview, pre, type);
return; return;
} }
} }
//cache //cache
if(useCache) { if (useCache) {
Cache cache = zipDataCache.get(code); Cache cache = zipDataCache.get(code);
if (cache != null) { if (cache != null) {
bind(cache.data, cache.name, imageview, pre, size); bind(cache.data, cache.name, imageview, pre, type);
return; return;
} }
} }
...@@ -268,7 +270,7 @@ public class ImageLoader implements Closeable { ...@@ -268,7 +270,7 @@ public class ImageLoader implements Closeable {
ZipFile pics = openPicsZip(); ZipFile pics = openPicsZip();
if (pics != null) { if (pics != null) {
for (String ex : Constants.IMAGE_EX) { for (String ex : Constants.IMAGE_EX) {
if (bindInZip(imageview, code, pre, pics, name + ex, size)) { if (bindInZip(imageview, code, pre, pics, name + ex, type)) {
return; return;
} }
} }
...@@ -284,11 +286,11 @@ public class ImageLoader implements Closeable { ...@@ -284,11 +286,11 @@ public class ImageLoader implements Closeable {
if (files != null) { if (files != null) {
for (File file : files) { for (File file : files) {
if (file.isFile()) { if (file.isFile()) {
ZipFile zipFile = useCache ? zipFileCache.get(file.getAbsolutePath()) : null; ZipFile zipFile = useCache ? zipFileCache.get(file.getAbsolutePath()) : null;
if (zipFile == null) { if (zipFile == null) {
try { try {
zipFile = new ZipFile(file); zipFile = new ZipFile(file);
if(useCache) { if (useCache) {
zipFileCache.put(file.getAbsolutePath(), zipFile); zipFileCache.put(file.getAbsolutePath(), zipFile);
} }
} catch (Throwable e) { } catch (Throwable e) {
...@@ -299,7 +301,7 @@ public class ImageLoader implements Closeable { ...@@ -299,7 +301,7 @@ public class ImageLoader implements Closeable {
continue; continue;
} }
for (String ex : Constants.IMAGE_EX) { for (String ex : Constants.IMAGE_EX) {
if (bindInZip(imageview, code, pre, zipFile, name + ex, size)) { if (bindInZip(imageview, code, pre, zipFile, name + ex, type)) {
return; return;
} }
} }
...@@ -308,7 +310,7 @@ public class ImageLoader implements Closeable { ...@@ -308,7 +310,7 @@ public class ImageLoader implements Closeable {
} }
//4 http //4 http
if (Constants.NETWORK_IMAGE && NetUtils.isWifiConnected(imageview.getContext())) { if (Constants.NETWORK_IMAGE && NetUtils.isWifiConnected(imageview.getContext())) {
bind(Uri.parse(String.format(Constants.IMAGE_URL, "" + code)), code + ".jpg", imageview, pre, size); bind(Uri.parse(String.format(Constants.IMAGE_URL, "" + code)), code + ".jpg", imageview, pre, type);
} else { } else {
imageview.setImageResource(R.drawable.unknown); imageview.setImageResource(R.drawable.unknown);
} }
......
...@@ -105,7 +105,7 @@ public class CardListAdapter extends BaseRecyclerAdapterPlus<Card, ViewHolder> i ...@@ -105,7 +105,7 @@ public class CardListAdapter extends BaseRecyclerAdapterPlus<Card, ViewHolder> i
if(item == null){ if(item == null){
return; return;
} }
imageLoader.bindImage(holder.cardImage, item.Code); imageLoader.bindImage(holder.cardImage, item.Code, ImageLoader.Type.list);
holder.cardName.setText(item.Name); holder.cardName.setText(item.Name);
if (item.isType(CardType.Monster)) { if (item.isType(CardType.Monster)) {
holder.layout_atk.setVisibility(View.VISIBLE); holder.layout_atk.setVisibility(View.VISIBLE);
......
...@@ -103,8 +103,8 @@ public class CardDetail extends BaseAdapterPlus.BaseViewHolder { ...@@ -103,8 +103,8 @@ public class CardDetail extends BaseAdapterPlus.BaseViewHolder {
isDownloadCardImage = true; isDownloadCardImage = true;
ll_bar.startAnimation(AnimationUtils.loadAnimation(context, R.anim.out_from_bottom)); ll_bar.startAnimation(AnimationUtils.loadAnimation(context, R.anim.out_from_bottom));
ll_bar.setVisibility(View.GONE); ll_bar.setVisibility(View.GONE);
imageLoader.bindImage(photoView, msg.arg1, null, true); imageLoader.bindImage(photoView, msg.arg1, null, ImageLoader.Type.origin);
imageLoader.bindImage(cardImage, msg.arg1, null, true); imageLoader.bindImage(cardImage, msg.arg1, null, ImageLoader.Type.detail);
break; break;
case TYPE_DOWNLOAD_CARD_IMAGE_ING: case TYPE_DOWNLOAD_CARD_IMAGE_ING:
tv_loading.setText(msg.arg1 + "%"); tv_loading.setText(msg.arg1 + "%");
...@@ -259,7 +259,7 @@ public class CardDetail extends BaseAdapterPlus.BaseViewHolder { ...@@ -259,7 +259,7 @@ public class CardDetail extends BaseAdapterPlus.BaseViewHolder {
private void setCardInfo(Card cardInfo, View view) { private void setCardInfo(Card cardInfo, View view) {
if (cardInfo == null) return; if (cardInfo == null) return;
mCardInfo = cardInfo; mCardInfo = cardInfo;
imageLoader.bindImage(cardImage, cardInfo.Code, null, true); imageLoader.bindImage(cardImage, cardInfo.Code, ImageLoader.Type.detail);
dialog = DialogUtils.getdx(context); dialog = DialogUtils.getdx(context);
cardImage.setOnClickListener((v) -> { cardImage.setOnClickListener((v) -> {
showCardImageDetail(cardInfo.Code); showCardImageDetail(cardInfo.Code);
...@@ -405,13 +405,13 @@ public class CardDetail extends BaseAdapterPlus.BaseViewHolder { ...@@ -405,13 +405,13 @@ public class CardDetail extends BaseAdapterPlus.BaseViewHolder {
} }
}); });
imageLoader.bindImage(cardImage, code, null, true); imageLoader.bindImage(cardImage, code, null, ImageLoader.Type.origin);
return true; return true;
} }
}); });
//先显示普通卡片大图,判断如果没有高清图就下载 //先显示普通卡片大图,判断如果没有高清图就下载
imageLoader.bindImage(photoView, code, null, true); imageLoader.bindImage(photoView, code, null, ImageLoader.Type.detail);
if (!file.exists()) { if (!file.exists()) {
downloadCardImage(code, file); downloadCardImage(code, file);
......
...@@ -98,7 +98,7 @@ public class CardDetailRandom { ...@@ -98,7 +98,7 @@ public class CardDetailRandom {
} }
public void bindCardImage(ImageLoader imageLoader, long code) { public void bindCardImage(ImageLoader imageLoader, long code) {
imageLoader.bindImage(cardImage, code, null, true); imageLoader.bindImage(cardImage, code, ImageLoader.Type.origin);
} }
public View getView() { public View getView() {
......
...@@ -596,7 +596,7 @@ public class DeckAdapater extends RecyclerView.Adapter<DeckViewHolder> implement ...@@ -596,7 +596,7 @@ public class DeckAdapater extends RecyclerView.Adapter<DeckViewHolder> implement
holder.setRightImage(null); holder.setRightImage(null);
} }
// holder.useDefault(); // holder.useDefault();
imageLoader.bindImage(holder.cardImage, cardInfo.Code); imageLoader.bindImage(holder.cardImage, cardInfo.Code, ImageLoader.Type.deck);
} else { } else {
holder.setCardType(0); holder.setCardType(0);
holder.setRightImage(null); holder.setRightImage(null);
......
...@@ -25,7 +25,7 @@ public class CardGroupView extends FrameLayout { ...@@ -25,7 +25,7 @@ public class CardGroupView extends FrameLayout {
private int mLineLimit = 10; private int mLineLimit = 10;
private int mOrgLineLimit = 10; private int mOrgLineLimit = 10;
private int mLineMaxCount = 15; private int mLineMaxCount = 15;
private int mCardWidth = Constants.CORE_SKIN_CARD_MINI_SIZE[0], mCardHeight = Constants.CORE_SKIN_CARD_MINI_SIZE[1]; private int mCardWidth = Constants.CORE_SKIN_CARD_DECK_SIZE[0], mCardHeight = Constants.CORE_SKIN_CARD_DECK_SIZE[1];
private boolean mPausePadding; private boolean mPausePadding;
private ImageLoader mImageLoader; private ImageLoader mImageLoader;
......
...@@ -95,7 +95,7 @@ public class CardView extends FrameLayout { ...@@ -95,7 +95,7 @@ public class CardView extends FrameLayout {
if (mCard != null && mCard.equals(cardInfo)) return; if (mCard != null && mCard.equals(cardInfo)) return;
mCard = cardInfo; mCard = cardInfo;
if (cardInfo != null && imageLoader != null) { if (cardInfo != null && imageLoader != null) {
imageLoader.bindImage(mCardView, cardInfo.Code, null, true); imageLoader.bindImage(mCardView, cardInfo.Code, ImageLoader.Type.deck);
} else { } else {
mTopImage.setVisibility(View.GONE); mTopImage.setVisibility(View.GONE);
mCardView.setImageBitmap(null); mCardView.setImageBitmap(null);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment