Commit 8a4e09d8 authored by qq247321453's avatar qq247321453

ImageLoader

parent d3ef672c
......@@ -92,7 +92,11 @@ public interface Constants {
String UNKNOWN_IMAGE = "unknown.jpg";
String YDK_FILE_EX = ".ydk";
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_AVATAR_SIZE = new int[]{128, 128};
boolean SUPPORT_BPG = true;
......
......@@ -44,13 +44,18 @@ import cn.garymb.ygomobile.utils.NetUtils;
import static com.bumptech.glide.Glide.with;
public class ImageLoader implements Closeable {
public static final ImageLoader sImageLoader = new ImageLoader();
public static ImageLoader get() {
return sImageLoader;
public enum Type {
//origin size
origin,
//44x64
deck,
//177x254
list,
//354x508
detail
}
private static class Cache{
private static class Cache {
private final byte[] data;
private final String name;
......@@ -67,7 +72,8 @@ public class ImageLoader implements Closeable {
private final Map<Long, Cache> zipDataCache = new ConcurrentHashMap<>();
private ZipFile mDefaultZipFile;
private File mPicsFile;
public ImageLoader(){
public ImageLoader() {
this(false);
}
......@@ -76,7 +82,7 @@ public class ImageLoader implements Closeable {
}
private ZipFile openPicsZip() {
if(mPicsFile == null) {
if (mPicsFile == null) {
mPicsFile = new File(AppsSettings.get().getResourcePath(), Constants.CORE_PICS_ZIP);
}
if (mDefaultZipFile == null) {
......@@ -91,11 +97,11 @@ public class ImageLoader implements Closeable {
return mDefaultZipFile;
}
public void resume(){
public void resume() {
}
public void clearZipCache(){
public void clearZipCache() {
//关闭zip
for (ZipFile zipFile : zipFileCache.values()) {
IOUtils.closeZip(zipFile);
......@@ -115,24 +121,24 @@ public class ImageLoader implements Closeable {
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) {
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) {
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,
@Nullable com.bumptech.glide.load.Key signature,
@Nullable Drawable pre,
@Nullable int[] size) {
@NonNull Type type) {
if (pre != null) {
resource.placeholder(pre);
} else {
......@@ -140,10 +146,24 @@ public class ImageLoader implements Closeable {
}
resource.error(R.drawable.unknown);
resource.animate(R.anim.push_in);
if (size != null) {
resource.override(size[0], size[1]);
if (type != Type.origin) {
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);
}
String ex = FileUtils.getFileExpansion(name);
......@@ -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 {
DrawableTypeRequest<T> resource = with(imageview.getContext()).load(data);
if (size == null) {
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);
}
setDefaults(resource, name, new StringSignature(name + ":" + type), pre, type);
resource.into(imageview);
} catch (Exception 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) {
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 {
DrawableTypeRequest<File> resource = with(imageview.getContext()).load(file);
long key = size == null ? 0:(size[0] * size[1]);
setDefaults(resource, file.getName(),
new MediaStoreSignature("image/*",
file.lastModified() + key, 0), pre, size);
file.lastModified(), type.ordinal()), pre, type);
resource.into(imageview);
} catch (Exception 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;
InputStream inputStream = null;
ByteArrayOutputStream outputStream;
......@@ -195,10 +209,10 @@ public class ImageLoader implements Closeable {
outputStream = new ByteArrayOutputStream();
IOUtils.copy(inputStream, outputStream);
byte[] data = outputStream.toByteArray();
if(useCache){
if (useCache) {
zipDataCache.put(code, new Cache(data, nameWithEx));
}
bind(data, nameWithEx, imageView, pre, size);
bind(data, nameWithEx, imageView, pre, type);
bind = true;
}
} catch (Exception e) {
......@@ -221,25 +235,13 @@ public class ImageLoader implements Closeable {
}
}
/**
* 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, @NonNull Type type) {
bindImage(imageview, code, null, type);
}
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) {
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_ex = Constants.CORE_EXPANSIONS_IMAGE_PATH + "/" + code;
......@@ -248,18 +250,18 @@ public class ImageLoader implements Closeable {
File file = new File(AppsSettings.get().getResourcePath(), name + ex);
File file_ex = new File(AppsSettings.get().getResourcePath(), name_ex + ex);
if (file_ex.exists()) {
bind(file_ex, imageview, pre, size);
bind(file_ex, imageview, pre, type);
return;
} else if (file.exists()) {
bind(file, imageview, pre, size);
bind(file, imageview, pre, type);
return;
}
}
//cache
if(useCache) {
if (useCache) {
Cache cache = zipDataCache.get(code);
if (cache != null) {
bind(cache.data, cache.name, imageview, pre, size);
bind(cache.data, cache.name, imageview, pre, type);
return;
}
}
......@@ -268,7 +270,7 @@ public class ImageLoader implements Closeable {
ZipFile pics = openPicsZip();
if (pics != null) {
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;
}
}
......@@ -284,11 +286,11 @@ public class ImageLoader implements Closeable {
if (files != null) {
for (File file : files) {
if (file.isFile()) {
ZipFile zipFile = useCache ? zipFileCache.get(file.getAbsolutePath()) : null;
ZipFile zipFile = useCache ? zipFileCache.get(file.getAbsolutePath()) : null;
if (zipFile == null) {
try {
zipFile = new ZipFile(file);
if(useCache) {
if (useCache) {
zipFileCache.put(file.getAbsolutePath(), zipFile);
}
} catch (Throwable e) {
......@@ -299,7 +301,7 @@ public class ImageLoader implements Closeable {
continue;
}
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;
}
}
......@@ -308,7 +310,7 @@ public class ImageLoader implements Closeable {
}
//4 http
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 {
imageview.setImageResource(R.drawable.unknown);
}
......
......@@ -105,7 +105,7 @@ public class CardListAdapter extends BaseRecyclerAdapterPlus<Card, ViewHolder> i
if(item == null){
return;
}
imageLoader.bindImage(holder.cardImage, item.Code);
imageLoader.bindImage(holder.cardImage, item.Code, ImageLoader.Type.list);
holder.cardName.setText(item.Name);
if (item.isType(CardType.Monster)) {
holder.layout_atk.setVisibility(View.VISIBLE);
......
......@@ -103,8 +103,8 @@ public class CardDetail extends BaseAdapterPlus.BaseViewHolder {
isDownloadCardImage = true;
ll_bar.startAnimation(AnimationUtils.loadAnimation(context, R.anim.out_from_bottom));
ll_bar.setVisibility(View.GONE);
imageLoader.bindImage(photoView, msg.arg1, null, true);
imageLoader.bindImage(cardImage, msg.arg1, null, true);
imageLoader.bindImage(photoView, msg.arg1, null, ImageLoader.Type.origin);
imageLoader.bindImage(cardImage, msg.arg1, null, ImageLoader.Type.detail);
break;
case TYPE_DOWNLOAD_CARD_IMAGE_ING:
tv_loading.setText(msg.arg1 + "%");
......@@ -259,7 +259,7 @@ public class CardDetail extends BaseAdapterPlus.BaseViewHolder {
private void setCardInfo(Card cardInfo, View view) {
if (cardInfo == null) return;
mCardInfo = cardInfo;
imageLoader.bindImage(cardImage, cardInfo.Code, null, true);
imageLoader.bindImage(cardImage, cardInfo.Code, ImageLoader.Type.detail);
dialog = DialogUtils.getdx(context);
cardImage.setOnClickListener((v) -> {
showCardImageDetail(cardInfo.Code);
......@@ -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;
}
});
//先显示普通卡片大图,判断如果没有高清图就下载
imageLoader.bindImage(photoView, code, null, true);
imageLoader.bindImage(photoView, code, null, ImageLoader.Type.detail);
if (!file.exists()) {
downloadCardImage(code, file);
......
......@@ -98,7 +98,7 @@ public class CardDetailRandom {
}
public void bindCardImage(ImageLoader imageLoader, long code) {
imageLoader.bindImage(cardImage, code, null, true);
imageLoader.bindImage(cardImage, code, ImageLoader.Type.origin);
}
public View getView() {
......
......@@ -596,7 +596,7 @@ public class DeckAdapater extends RecyclerView.Adapter<DeckViewHolder> implement
holder.setRightImage(null);
}
// holder.useDefault();
imageLoader.bindImage(holder.cardImage, cardInfo.Code);
imageLoader.bindImage(holder.cardImage, cardInfo.Code, ImageLoader.Type.deck);
} else {
holder.setCardType(0);
holder.setRightImage(null);
......
......@@ -25,7 +25,7 @@ public class CardGroupView extends FrameLayout {
private int mLineLimit = 10;
private int mOrgLineLimit = 10;
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 ImageLoader mImageLoader;
......
......@@ -95,7 +95,7 @@ public class CardView extends FrameLayout {
if (mCard != null && mCard.equals(cardInfo)) return;
mCard = cardInfo;
if (cardInfo != null && imageLoader != null) {
imageLoader.bindImage(mCardView, cardInfo.Code, null, true);
imageLoader.bindImage(mCardView, cardInfo.Code, ImageLoader.Type.deck);
} else {
mTopImage.setVisibility(View.GONE);
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