Commit 7a92d476 authored by qq247321453's avatar qq247321453

ImageLoader

parent e98a7e0c
...@@ -3,7 +3,6 @@ package cn.garymb.ygomobile; ...@@ -3,7 +3,6 @@ package cn.garymb.ygomobile;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.content.Context; import android.content.Context;
import android.graphics.Point; import android.graphics.Point;
import android.os.ParcelFileDescriptor;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import android.view.WindowManager; import android.view.WindowManager;
...@@ -36,7 +35,6 @@ import static cn.garymb.ygomobile.Constants.DEF_PREF_KEEP_SCALE; ...@@ -36,7 +35,6 @@ import static cn.garymb.ygomobile.Constants.DEF_PREF_KEEP_SCALE;
import static cn.garymb.ygomobile.Constants.DEF_PREF_NOTCH_HEIGHT; import static cn.garymb.ygomobile.Constants.DEF_PREF_NOTCH_HEIGHT;
import static cn.garymb.ygomobile.Constants.DEF_PREF_ONLY_GAME; import static cn.garymb.ygomobile.Constants.DEF_PREF_ONLY_GAME;
import static cn.garymb.ygomobile.Constants.DEF_PREF_READ_EX; import static cn.garymb.ygomobile.Constants.DEF_PREF_READ_EX;
import static cn.garymb.ygomobile.Constants.DEF_PREF_WINDOW_TOP_BOTTOM;
import static cn.garymb.ygomobile.Constants.PREF_DEF_IMMERSIVE_MODE; import static cn.garymb.ygomobile.Constants.PREF_DEF_IMMERSIVE_MODE;
import static cn.garymb.ygomobile.Constants.PREF_DEF_SENSOR_REFRESH; import static cn.garymb.ygomobile.Constants.PREF_DEF_SENSOR_REFRESH;
import static cn.garymb.ygomobile.Constants.PREF_FONT_SIZE; import static cn.garymb.ygomobile.Constants.PREF_FONT_SIZE;
......
...@@ -92,6 +92,7 @@ public interface Constants { ...@@ -92,6 +92,7 @@ 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_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;
......
package cn.garymb.ygomobile.loader; package cn.garymb.ygomobile.loader;
import android.content.Context;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.net.Uri; import android.net.Uri;
import android.util.Log; import android.util.Log;
import android.widget.ImageView; import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.bumptech.glide.DrawableTypeRequest; import com.bumptech.glide.DrawableTypeRequest;
import com.bumptech.glide.load.ResourceDecoder; import com.bumptech.glide.load.ResourceDecoder;
import com.bumptech.glide.load.engine.Resource; import com.bumptech.glide.load.engine.Resource;
...@@ -15,7 +17,7 @@ import com.bumptech.glide.load.model.ImageVideoWrapper; ...@@ -15,7 +17,7 @@ import com.bumptech.glide.load.model.ImageVideoWrapper;
import com.bumptech.glide.load.resource.bitmap.BitmapResource; import com.bumptech.glide.load.resource.bitmap.BitmapResource;
import com.bumptech.glide.load.resource.gifbitmap.GifBitmapWrapper; import com.bumptech.glide.load.resource.gifbitmap.GifBitmapWrapper;
import com.bumptech.glide.load.resource.gifbitmap.GifBitmapWrapperResource; import com.bumptech.glide.load.resource.gifbitmap.GifBitmapWrapperResource;
import com.bumptech.glide.request.target.GlideDrawableImageViewTarget; import com.bumptech.glide.signature.MediaStoreSignature;
import com.bumptech.glide.signature.StringSignature; import com.bumptech.glide.signature.StringSignature;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
...@@ -33,91 +35,104 @@ import java.util.zip.ZipFile; ...@@ -33,91 +35,104 @@ import java.util.zip.ZipFile;
import cn.garymb.ygomobile.AppsSettings; import cn.garymb.ygomobile.AppsSettings;
import cn.garymb.ygomobile.Constants; import cn.garymb.ygomobile.Constants;
import cn.garymb.ygomobile.core.IrrlichtBridge; import cn.garymb.ygomobile.core.IrrlichtBridge;
import cn.garymb.ygomobile.lite.BuildConfig;
import cn.garymb.ygomobile.lite.R; import cn.garymb.ygomobile.lite.R;
import cn.garymb.ygomobile.utils.BitmapUtil; import cn.garymb.ygomobile.utils.FileUtils;
import cn.garymb.ygomobile.utils.IOUtils; import cn.garymb.ygomobile.utils.IOUtils;
import cn.garymb.ygomobile.utils.MD5Util;
import cn.garymb.ygomobile.utils.NetUtils; import cn.garymb.ygomobile.utils.NetUtils;
import static cn.garymb.ygomobile.Constants.CORE_SKIN_BG_SIZE;
import static com.bumptech.glide.Glide.with; import static com.bumptech.glide.Glide.with;
public class ImageLoader implements Closeable { public class ImageLoader implements Closeable {
private static final String TAG = ImageLoader.class.getSimpleName(); public static final ImageLoader sImageLoader = new ImageLoader();
private ZipFile mZipFile;
private List<ZipFile> zipFileList;
private LruBitmapPool mLruBitmapPool;
// private ExecutorService mExecutorService = Executors.newSingleThreadExecutor();
private boolean isClose = false;
private Context mContext;
private static final Map<Context, ImageLoader> IMAGE_LOADER_MAP = new ConcurrentHashMap<>();
public static ImageLoader get(Context context) { public static ImageLoader get() {
ImageLoader imageLoader = IMAGE_LOADER_MAP.get(context); return sImageLoader;
if (imageLoader == null) {
synchronized (IMAGE_LOADER_MAP) {
imageLoader = IMAGE_LOADER_MAP.get(context);
if (imageLoader == null) {
imageLoader = new ImageLoader(context);
IMAGE_LOADER_MAP.put(context, imageLoader);
}
} }
private static class Cache{
private final byte[] data;
private final String name;
public Cache(byte[] data, String name) {
this.data = data;
this.name = name;
} }
return imageLoader;
} }
public static void onDestory(Context context) { private final boolean useCache;
synchronized (IMAGE_LOADER_MAP) { private static final String TAG = ImageLoader.class.getSimpleName();
IMAGE_LOADER_MAP.remove(context); private final Map<String, ZipFile> zipFileCache = new ConcurrentHashMap<>();
private final LruBitmapPool mLruBitmapPool = new LruBitmapPool(128);
private final Map<Long, Cache> zipDataCache = new ConcurrentHashMap<>();
private ZipFile mDefaultZipFile;
private File mPicsFile;
public ImageLoader(){
this(false);
} }
public ImageLoader(boolean useCache) {
this.useCache = useCache;
} }
private ImageLoader(Context context) { private ZipFile openPicsZip() {
mContext = context; if(mPicsFile == null) {
mLruBitmapPool = new LruBitmapPool(100); mPicsFile = new File(AppsSettings.get().getResourcePath(), Constants.CORE_PICS_ZIP);
zipFileList=new ArrayList<>(); }
if (mDefaultZipFile == null) {
if (mPicsFile.exists()) {
try {
mDefaultZipFile = new ZipFile(mPicsFile);
} catch (IOException e) {
//Ignore
}
}
}
return mDefaultZipFile;
} }
private class BpgResourceDecoder implements ResourceDecoder<ImageVideoWrapper, GifBitmapWrapper> { public void resume(){
String id;
private BpgResourceDecoder(String id) {
this.id = id;
} }
@Override public void pause(){
public Resource<GifBitmapWrapper> decode(ImageVideoWrapper source, int width, int height) throws IOException { //关闭zip
// Log.i("kk", "decode source:"+source); for (ZipFile zipFile : zipFileCache.values()) {
Bitmap bitmap = IrrlichtBridge.getBpgImage(source.getStream(), Bitmap.Config.RGB_565); IOUtils.closeZip(zipFile);
// Log.i("kk", "decode bitmap:"+bitmap); }
BitmapResource resource = new BitmapResource(bitmap, mLruBitmapPool); zipFileCache.clear();
return new GifBitmapWrapperResource(new GifBitmapWrapper(resource, null)); if (mDefaultZipFile != null) {
IOUtils.closeZip(mDefaultZipFile);
}
} }
@Override @Override
public String getId() { public void close() {
return id; if (BuildConfig.DEBUG_MODE) {
Log.d(TAG, "close and clean cache");
} }
pause();
zipDataCache.clear();
} }
@Override private void bind(final byte[] data, String name, ImageView imageview, Drawable pre, int[] size) {
public void close() throws IOException { if (BuildConfig.DEBUG_MODE) {
isClose = true; Log.d(TAG, "bind data:" + name + ", size=" + (size == null ? "null" : size[0] + "x" + size[1]));
// if (!mExecutorService.isShutdown()) { }
// mExecutorService.shutdown(); bindT(data, name, imageview, pre, size);
// }
} }
private Bitmap loadImage(String path, int w, int h) { private void bind(final Uri uri, String name, ImageView imageview, Drawable pre, int[] size) {
File file = new File(path); if (BuildConfig.DEBUG_MODE) {
if (file.exists()) { Log.d(TAG, "bind uri:" + name + ", size=" + (size == null ? "null" : size[0] + "x" + size[1]));
return BitmapUtil.getBitmapFromFile(file.getAbsolutePath(), CORE_SKIN_BG_SIZE[0], CORE_SKIN_BG_SIZE[1]);
} }
return null; bindT(uri, name, imageview, pre, size);
} }
private void bind(byte[] data, ImageView imageview, boolean isbpg, long code, Drawable pre, boolean isBig) { private <T> void setDefaults(@NonNull DrawableTypeRequest<T> resource, String name,
DrawableTypeRequest<byte[]> resource = with(mContext).load(data); @Nullable com.bumptech.glide.load.Key signature,
@Nullable Drawable pre,
@Nullable int[] size) {
if (pre != null) { if (pre != null) {
resource.placeholder(pre); resource.placeholder(pre);
} else { } else {
...@@ -125,152 +140,202 @@ public class ImageLoader implements Closeable { ...@@ -125,152 +140,202 @@ 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(isbpg){ if (size != null) {
// resource.override(Constants.CORE_SKIN_CARD_COVER_SIZE[0], Constants.CORE_SKIN_CARD_COVER_SIZE[1]); resource.override(size[0], size[1]);
// } }
resource.signature(new StringSignature(MD5Util.getStringMD5(data.length + "_" + code + "_" + isBig))); if(signature != null) {
if (isbpg) { resource.signature(signature);
resource.decoder(new BpgResourceDecoder("bpg@" + code)); }
String ex = FileUtils.getFileExpansion(name);
if ("bpg".equals(ex)) {
resource.decoder(new BpgResourceDecoder(name, mLruBitmapPool));
} }
resource.into(imageview);
} }
public void bind(final File file, ImageView imageview, boolean isbpg, long code, Drawable pre, boolean isBig) { private <T> void bindT(final T data, String name, ImageView imageview, Drawable pre, int[] size) {
try { try {
DrawableTypeRequest<File> resource = with(mContext).load(file); DrawableTypeRequest<T> resource = with(imageview.getContext()).load(data);
if (pre != null) { if (size == null) {
resource.placeholder(pre); setDefaults(resource, name, new StringSignature(name), pre, size);
resource.signature(new StringSignature(name));
} else { } else {
resource.placeholder(R.drawable.unknown); setDefaults(resource, name, new StringSignature(name + ":" + size[0] + "x" + size[1]), pre, size);
} }
resource.error(R.drawable.unknown); resource.into(imageview);
resource.animate(R.anim.push_in); } catch (Exception e) {
resource.signature(new StringSignature(MD5Util.getStringMD5(file.length() + code + "_" + isBig))); Log.e(TAG, "$", e);
if (isbpg) {
resource.decoder(new BpgResourceDecoder("bpg@" + code));
} }
}
private void bind(final File file, ImageView imageview, Drawable pre, int[] size) {
if (BuildConfig.DEBUG_MODE) {
Log.d(TAG, "bind file:" + file.getPath() + ", size=" + (size == null ? "null" : size[0] + "x" + size[1]));
}
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);
resource.into(imageview); resource.into(imageview);
} catch (Exception e) { } catch (Exception e) {
Log.e(TAG, "$", e); Log.e(TAG, "$", e);
} }
} }
private void bind(final String url, ImageView imageview, long code, Drawable pre, boolean isBig) { private boolean bindInZip(ImageView imageView, long code, Drawable pre, ZipFile zipFile, String nameWithEx, int[] size) {
DrawableTypeRequest<Uri> resource = with(mContext).load(Uri.parse(url)); ZipEntry entry;
if (pre != null) { InputStream inputStream = null;
resource.placeholder(pre); ByteArrayOutputStream outputStream;
} else { boolean bind = false;
resource.placeholder(R.drawable.unknown); try {
entry = zipFile.getEntry(nameWithEx);
if (entry != null) {
inputStream = zipFile.getInputStream(entry);
outputStream = new ByteArrayOutputStream();
IOUtils.copy(inputStream, outputStream);
byte[] data = outputStream.toByteArray();
if(useCache){
zipDataCache.put(code, new Cache(data, nameWithEx));
} }
resource.error(R.drawable.unknown); bind(data, nameWithEx, imageView, pre, size);
resource.override(Constants.CORE_SKIN_CARD_COVER_SIZE[0], Constants.CORE_SKIN_CARD_COVER_SIZE[1]); bind = true;
resource.signature(new StringSignature("" + code)); }
resource.into(new GlideDrawableImageViewTarget(imageview)); } catch (Exception e) {
e.printStackTrace();
} finally {
IOUtils.close(inputStream);
}
return bind;
} }
public void bindImage(ImageView imageview, long code) { private void cleanInValidZips() {
bindImage(imageview, code, null); List<String> removes = new ArrayList<>();
for (String old : zipFileCache.keySet()) {
if (!FileUtils.isExist(old)) {
removes.add(old);
}
}
for (String key : removes) {
zipFileCache.remove(key);
}
} }
public void bindImage(ImageView imageview, long code, Drawable pre) {
bindImage(imageview, code, pre, false); /**
* 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, boolean isBig) { public void bindImage(ImageView imageview, long code, Drawable pre, int[] size) {
if (BuildConfig.DEBUG_MODE) {
Log.d(TAG, "bind image:" + code + ", size=" + (size == null ? "null" : size[0] + "x" + size[1]));
}
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;
String path = AppsSettings.get().getResourcePath(); //1.图片文件
boolean bind = false;
File zip = new File(path, Constants.CORE_PICS_ZIP);
List<File> zipList=new ArrayList<>();
for (String ex : Constants.IMAGE_EX) { for (String ex : Constants.IMAGE_EX) {
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, Constants.BPG.equals(ex), code, pre, isBig); bind(file_ex, imageview, pre, size);
bind = true;
return; return;
} else } else if (file.exists()) {
if (file.exists()) { bind(file, imageview, pre, size);
bind(file, imageview, Constants.BPG.equals(ex), code, pre, isBig);
bind = true;
return; return;
} }
} }
if (zip.exists()) { //cache
ZipEntry entry = null; if(useCache) {
InputStream inputStream = null; Cache cache = zipDataCache.get(code);
ByteArrayOutputStream outputStream = null; if (cache != null) {
try { bind(cache.data, cache.name, imageview, pre, size);
if (mZipFile == null) { return;
mZipFile = new ZipFile(zip); }
} }
//2.zip
{
ZipFile pics = openPicsZip();
if (pics != null) {
for (String ex : Constants.IMAGE_EX) { for (String ex : Constants.IMAGE_EX) {
entry = mZipFile.getEntry(name + ex); if (bindInZip(imageview, code, pre, pics, name + ex, size)) {
if (entry != null) { return;
inputStream = mZipFile.getInputStream(entry);
outputStream = new ByteArrayOutputStream();
IOUtils.copy(inputStream, outputStream);
bind(outputStream.toByteArray(), imageview, Constants.BPG.equals(ex), code, pre, isBig);
bind = true;
break;
} }
} }
} catch (Exception e) {
e.printStackTrace();
} finally {
IOUtils.close(inputStream);
} }
} }
if (!bind) { //3.
File[] files = new File(AppsSettings.get().getResourcePath(), Constants.CORE_EXPANSIONS).listFiles(); //zips
File[] files = new File(AppsSettings.get().getResourcePath(), Constants.CORE_EXPANSIONS)
.listFiles((dir, name1) -> name1.endsWith(".zip") || name1.endsWith(".ypk"));
cleanInValidZips();
if (files != null) { if (files != null) {
for (File file :files) { for (File file : files) {
if (file.isFile() && (file.getName().endsWith(".zip") || file.getName().endsWith(".ypk"))) { if (file.isFile()) {
ZipEntry entry = null; ZipFile zipFile = useCache ? zipFileCache.get(file.getAbsolutePath()) : null;
InputStream inputStream = null; if (zipFile == null) {
ByteArrayOutputStream outputStream = null;
try { try {
ZipFile zipFile = null; zipFile = new ZipFile(file);
for(ZipFile zipFile1:zipFileList){ if(useCache) {
if (zipFile1.getName().equals(file.getAbsolutePath())){ zipFileCache.put(file.getAbsolutePath(), zipFile);
zipFile=zipFile1;
break;
} }
} catch (Throwable e) {
//Ignore
} }
if (zipFile==null){
zipFile=new ZipFile(file.getAbsoluteFile());
zipFileList.add(zipFile);
} }
for (String ex : Constants.IMAGE_EX) { if (zipFile == null) {
entry = zipFile.getEntry(name + ex); continue;
if (entry != null) {
inputStream = zipFile.getInputStream(entry);
outputStream = new ByteArrayOutputStream();
IOUtils.copy(inputStream, outputStream);
bind(outputStream.toByteArray(), imageview, Constants.BPG.equals(ex), code, pre, isBig);
bind = true;
break;
} }
} for (String ex : Constants.IMAGE_EX) {
if (bind) if (bindInZip(imageview, code, pre, zipFile, name + ex, size)) {
break; return;
} catch (Exception e) {
e.printStackTrace();
} finally {
IOUtils.close(inputStream);
} }
} }
} }
} }
} }
if (!bind) { //4 http
if (Constants.NETWORK_IMAGE && NetUtils.isWifiConnected(imageview.getContext())) { if (Constants.NETWORK_IMAGE && NetUtils.isWifiConnected(imageview.getContext())) {
bind(String.format(Constants.IMAGE_URL, "" + code), imageview, code, pre, isBig); bind(Uri.parse(String.format(Constants.IMAGE_URL, "" + code)), code + ".jpg", imageview, pre, size);
} else { } else {
imageview.setImageResource(R.drawable.unknown); imageview.setImageResource(R.drawable.unknown);
} }
} }
private static class BpgResourceDecoder implements ResourceDecoder<ImageVideoWrapper, GifBitmapWrapper> {
private final String id;
private final LruBitmapPool mLruBitmapPool;
private BpgResourceDecoder(String id, LruBitmapPool lruBitmapPool) {
this.id = id;
this.mLruBitmapPool = lruBitmapPool;
}
@Override
public Resource<GifBitmapWrapper> decode(ImageVideoWrapper source, int width, int height) {
Bitmap bitmap = IrrlichtBridge.getBpgImage(source.getStream(), Bitmap.Config.RGB_565);
if (bitmap == null) {
return null;
}
BitmapResource resource = new BitmapResource(bitmap, mLruBitmapPool);
return new GifBitmapWrapperResource(new GifBitmapWrapper(resource, null));
}
@Override
public String getId() {
return id;
}
} }
} }
...@@ -24,7 +24,6 @@ import com.app.hubert.guide.model.HighLight; ...@@ -24,7 +24,6 @@ import com.app.hubert.guide.model.HighLight;
import com.app.hubert.guide.model.HighlightOptions; import com.app.hubert.guide.model.HighlightOptions;
import com.bumptech.glide.Glide; import com.bumptech.glide.Glide;
import java.io.IOException;
import java.util.List; import java.util.List;
import cn.garymb.ygomobile.Constants; import cn.garymb.ygomobile.Constants;
...@@ -45,7 +44,7 @@ public abstract class BaseCardsActivity extends BaseActivity implements CardLoad ...@@ -45,7 +44,7 @@ public abstract class BaseCardsActivity extends BaseActivity implements CardLoad
protected CardListAdapter mCardListAdapter; protected CardListAdapter mCardListAdapter;
protected CardLoader mCardLoader; protected CardLoader mCardLoader;
protected boolean isLoad = false; protected boolean isLoad = false;
private ImageLoader mImageLoader; protected ImageLoader mImageLoader;
protected StringManager mStringManager = DataManager.get().getStringManager(); protected StringManager mStringManager = DataManager.get().getStringManager();
protected LimitManager mLimitManager = DataManager.get().getLimitManager(); protected LimitManager mLimitManager = DataManager.get().getLimitManager();
protected int screenWidth; protected int screenWidth;
...@@ -55,7 +54,7 @@ public abstract class BaseCardsActivity extends BaseActivity implements CardLoad ...@@ -55,7 +54,7 @@ public abstract class BaseCardsActivity extends BaseActivity implements CardLoad
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_deck_cards); setContentView(R.layout.activity_deck_cards);
AnimationShake2(); AnimationShake2();
mImageLoader = ImageLoader.get(this); mImageLoader = new ImageLoader(true);
mDrawerLayout = $(R.id.drawer_layout); mDrawerLayout = $(R.id.drawer_layout);
screenWidth = getResources().getDisplayMetrics().widthPixels; screenWidth = getResources().getDisplayMetrics().widthPixels;
mListView = $(R.id.list_cards); mListView = $(R.id.list_cards);
...@@ -217,14 +216,22 @@ public abstract class BaseCardsActivity extends BaseActivity implements CardLoad ...@@ -217,14 +216,22 @@ public abstract class BaseCardsActivity extends BaseActivity implements CardLoad
} }
} }
@Override
protected void onResume() {
// mImageLoader.resume();
super.onResume();
}
@Override
protected void onPause() {
//仅退出的时候才关闭zip
// mImageLoader.pause();
super.onPause();
}
@Override @Override
protected void onDestroy() { protected void onDestroy() {
ImageLoader.onDestory(this);
try {
mImageLoader.close(); mImageLoader.close();
} catch (IOException e) {
e.printStackTrace();
}
super.onDestroy(); super.onDestroy();
} }
......
...@@ -16,26 +16,28 @@ import ocgcore.data.Card; ...@@ -16,26 +16,28 @@ import ocgcore.data.Card;
import ocgcore.enums.CardType; import ocgcore.enums.CardType;
import static android.view.View.inflate; import static android.view.View.inflate;
import static android.view.View.resolveSize;
public class CardDetailRandom { public class CardDetailRandom {
private static View viewCardDetail; private View viewCardDetail;
private static ImageView cardImage; private ImageView cardImage;
private static TextView name; private TextView name;
private static TextView desc; private TextView desc;
private static TextView level; private TextView level;
private static TextView type; private TextView type;
private static TextView race; private TextView race;
private static TextView cardAtk; private TextView cardAtk;
private static TextView cardDef; private TextView cardDef;
private static TextView attrView; private TextView attrView;
private static View monsterlayout; private View monsterlayout;
private static View atkdefView, textdefView; private View atkdefView, textdefView;
private static ImageLoader imageLoader; private StringManager mStringManager;
private static StringManager mStringManager; private Context mContext;
public static void RandomCardDetail(Context context, Card cardInfo) { private static CardDetailRandom sCardDetailRandom = null;
if (cardInfo == null) return;
imageLoader = ImageLoader.get(context); private CardDetailRandom(Context context, Card cardInfo) {
mContext = context;
viewCardDetail = inflate(context, R.layout.dialog_cardinfo_small, null); viewCardDetail = inflate(context, R.layout.dialog_cardinfo_small, null);
cardImage = viewCardDetail.findViewById(R.id.card_image_toast); cardImage = viewCardDetail.findViewById(R.id.card_image_toast);
name = viewCardDetail.findViewById(R.id.card_name_toast); name = viewCardDetail.findViewById(R.id.card_name_toast);
...@@ -51,7 +53,6 @@ public class CardDetailRandom { ...@@ -51,7 +53,6 @@ public class CardDetailRandom {
desc = viewCardDetail.findViewById(R.id.text_desc_toast); desc = viewCardDetail.findViewById(R.id.text_desc_toast);
mStringManager = DataManager.get().getStringManager(); mStringManager = DataManager.get().getStringManager();
imageLoader.bindImage(cardImage, cardInfo.Code);
name.setText(cardInfo.Name); name.setText(cardInfo.Name);
type.setText(CardUtils.getAllTypeString(cardInfo, mStringManager).replace("/", "|")); type.setText(CardUtils.getAllTypeString(cardInfo, mStringManager).replace("/", "|"));
attrView.setText(mStringManager.getAttributeString(cardInfo.Attribute)); attrView.setText(mStringManager.getAttributeString(cardInfo.Attribute));
...@@ -88,8 +89,24 @@ public class CardDetailRandom { ...@@ -88,8 +89,24 @@ public class CardDetailRandom {
viewCardDetail.setRotationY(5); viewCardDetail.setRotationY(5);
} }
public static void showRandromCardDetailToast(Context context) { public static CardDetailRandom genRandomCardDetail(Context context, ImageLoader imageLoader, Card cardInfo) {
Toast toast = new Toast(context); if (cardInfo == null) return null;
CardDetailRandom cardDetailRandom = new CardDetailRandom(context, cardInfo);
cardDetailRandom.bindCardImage(imageLoader, cardInfo.Alias != 0 ? cardInfo.Alias : cardInfo.Code);
sCardDetailRandom = cardDetailRandom;
return cardDetailRandom;
}
public void bindCardImage(ImageLoader imageLoader, long code) {
imageLoader.bindImage(cardImage, code, null, true);
}
public View getView() {
return viewCardDetail;
}
public void show(){
Toast toast = new Toast(mContext);
toast.setView(viewCardDetail); toast.setView(viewCardDetail);
toast.setDuration(Toast.LENGTH_LONG); toast.setDuration(Toast.LENGTH_LONG);
toast.setGravity(Gravity.LEFT, 0, 0); toast.setGravity(Gravity.LEFT, 0, 0);
......
...@@ -27,7 +27,6 @@ import com.app.hubert.guide.model.HighlightOptions; ...@@ -27,7 +27,6 @@ import com.app.hubert.guide.model.HighlightOptions;
import com.bumptech.glide.Glide; import com.bumptech.glide.Glide;
import com.ourygo.assistant.util.DuelAssistantManagement; import com.ourygo.assistant.util.DuelAssistantManagement;
import java.io.IOException;
import java.util.List; import java.util.List;
import cn.garymb.ygomobile.Constants; import cn.garymb.ygomobile.Constants;
...@@ -77,7 +76,7 @@ public class CardSearchActivity extends BaseActivity implements CardLoader.CallB ...@@ -77,7 +76,7 @@ public class CardSearchActivity extends BaseActivity implements CardLoader.CallB
// setSupportActionBar(toolbar); // setSupportActionBar(toolbar);
enableBackHome(); enableBackHome();
mDrawerlayout = $(R.id.drawer_layout); mDrawerlayout = $(R.id.drawer_layout);
mImageLoader = ImageLoader.get(this); mImageLoader = new ImageLoader(true);
mListView = $(R.id.list_cards); mListView = $(R.id.list_cards);
mCardListAdapter = new CardListAdapter(this, mImageLoader); mCardListAdapter = new CardListAdapter(this, mImageLoader);
mCardListAdapter.setItemBg(true); mCardListAdapter.setItemBg(true);
...@@ -190,12 +189,7 @@ public class CardSearchActivity extends BaseActivity implements CardLoader.CallB ...@@ -190,12 +189,7 @@ public class CardSearchActivity extends BaseActivity implements CardLoader.CallB
@Override @Override
protected void onDestroy() { protected void onDestroy() {
ImageLoader.onDestory(this);
try {
mImageLoader.close(); mImageLoader.close();
} catch (IOException e) {
e.printStackTrace();
}
super.onDestroy(); super.onDestroy();
} }
......
...@@ -47,7 +47,6 @@ import com.ourygo.assistant.base.listener.OnDuelAssistantListener; ...@@ -47,7 +47,6 @@ import com.ourygo.assistant.base.listener.OnDuelAssistantListener;
import com.ourygo.assistant.util.DuelAssistantManagement; import com.ourygo.assistant.util.DuelAssistantManagement;
import com.ourygo.assistant.util.Util; import com.ourygo.assistant.util.Util;
import com.tencent.bugly.beta.Beta; import com.tencent.bugly.beta.Beta;
import com.tencent.smtt.export.external.TbsCoreSettings;
import com.tencent.smtt.sdk.QbSdk; import com.tencent.smtt.sdk.QbSdk;
import com.tubb.smrv.SwipeMenuRecyclerView; import com.tubb.smrv.SwipeMenuRecyclerView;
...@@ -58,7 +57,6 @@ import org.greenrobot.eventbus.ThreadMode; ...@@ -58,7 +57,6 @@ import org.greenrobot.eventbus.ThreadMode;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap;
import java.util.List; import java.util.List;
import cn.garymb.ygodata.YGOGameOptions; import cn.garymb.ygodata.YGOGameOptions;
...@@ -71,6 +69,7 @@ import cn.garymb.ygomobile.bean.ServerList; ...@@ -71,6 +69,7 @@ import cn.garymb.ygomobile.bean.ServerList;
import cn.garymb.ygomobile.bean.events.ServerInfoEvent; import cn.garymb.ygomobile.bean.events.ServerInfoEvent;
import cn.garymb.ygomobile.lite.BuildConfig; import cn.garymb.ygomobile.lite.BuildConfig;
import cn.garymb.ygomobile.lite.R; import cn.garymb.ygomobile.lite.R;
import cn.garymb.ygomobile.loader.ImageLoader;
import cn.garymb.ygomobile.ui.activities.BaseActivity; import cn.garymb.ygomobile.ui.activities.BaseActivity;
import cn.garymb.ygomobile.ui.activities.FileLogActivity; import cn.garymb.ygomobile.ui.activities.FileLogActivity;
import cn.garymb.ygomobile.ui.activities.WebActivity; import cn.garymb.ygomobile.ui.activities.WebActivity;
...@@ -108,13 +107,15 @@ public abstract class HomeActivity extends BaseActivity implements NavigationVie ...@@ -108,13 +107,15 @@ public abstract class HomeActivity extends BaseActivity implements NavigationVie
private ServerListManager mServerListManager; private ServerListManager mServerListManager;
private DuelAssistantManagement duelAssistantManagement; private DuelAssistantManagement duelAssistantManagement;
private CardManager mCardManager; private CardManager mCardManager;
private SparseArray<Card> cards; private CardDetailRandom mCardDetailRandom;
private ImageLoader mImageLoader;
@Override @Override
protected void onCreate(@Nullable Bundle savedInstanceState) { protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home); setContentView(R.layout.activity_home);
setExitAnimEnable(false); setExitAnimEnable(false);
mImageLoader = new ImageLoader(false);
mCardManager = DataManager.get().getCardManager(); mCardManager = DataManager.get().getCardManager();
//server list //server list
initServerlist(); initServerlist();
...@@ -158,10 +159,17 @@ public abstract class HomeActivity extends BaseActivity implements NavigationVie ...@@ -158,10 +159,17 @@ public abstract class HomeActivity extends BaseActivity implements NavigationVie
@Override @Override
protected void onResume() { protected void onResume() {
mImageLoader.resume();
super.onResume(); super.onResume();
BacktoDuel(); BacktoDuel();
} }
@Override
protected void onPause() {
mImageLoader.pause();
super.onPause();
}
private void duelAssistantCheck() { private void duelAssistantCheck() {
if (AppsSettings.get().isServiceDuelAssistant()) { if (AppsSettings.get().isServiceDuelAssistant()) {
Handler handler = new Handler(); Handler handler = new Handler();
...@@ -304,7 +312,9 @@ public abstract class HomeActivity extends BaseActivity implements NavigationVie ...@@ -304,7 +312,9 @@ public abstract class HomeActivity extends BaseActivity implements NavigationVie
break; break;
case R.id.action_game: case R.id.action_game:
setRandomCardDetail(); setRandomCardDetail();
CardDetailRandom.showRandromCardDetailToast(this); if(mCardDetailRandom != null){
mCardDetailRandom.show();
}
openGame(); openGame();
break; break;
case R.id.action_settings: { case R.id.action_settings: {
...@@ -665,12 +675,12 @@ public abstract class HomeActivity extends BaseActivity implements NavigationVie ...@@ -665,12 +675,12 @@ public abstract class HomeActivity extends BaseActivity implements NavigationVie
//加载数据库中所有卡片卡片 //加载数据库中所有卡片卡片
mCardManager.loadCards(); mCardManager.loadCards();
//mCardManager = DataManager.get().getCardManager(); //mCardManager = DataManager.get().getCardManager();
cards = mCardManager.getAllCards(); SparseArray<Card> cards = mCardManager.getAllCards();
int y = (int) (Math.random() * cards.size()); int y = (int) (Math.random() * cards.size());
Card cardInfo = cards.valueAt(y); Card cardInfo = cards.valueAt(y);
if (cardInfo == null) if (cardInfo == null)
return; return;
CardDetailRandom.RandomCardDetail(this, cardInfo); mCardDetailRandom = CardDetailRandom.genRandomCardDetail(this, mImageLoader, cardInfo);
} }
public void showTipsToast() { public void showTipsToast() {
......
...@@ -13,7 +13,9 @@ import androidx.annotation.Nullable; ...@@ -13,7 +13,9 @@ import androidx.annotation.Nullable;
import java.util.List; import java.util.List;
import cn.garymb.ygomobile.Constants;
import cn.garymb.ygomobile.lite.R; import cn.garymb.ygomobile.lite.R;
import cn.garymb.ygomobile.loader.ImageLoader;
import cn.garymb.ygomobile.ui.cards.deck.ImageTop; import cn.garymb.ygomobile.ui.cards.deck.ImageTop;
import ocgcore.data.Card; import ocgcore.data.Card;
import ocgcore.data.LimitList; import ocgcore.data.LimitList;
...@@ -23,8 +25,13 @@ public class CardGroupView extends FrameLayout { ...@@ -23,8 +25,13 @@ 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 = 177, mCardHeight = 255; private int mCardWidth = Constants.CORE_SKIN_CARD_MINI_SIZE[0], mCardHeight = Constants.CORE_SKIN_CARD_MINI_SIZE[1];
private boolean mPausePadding; private boolean mPausePadding;
private ImageLoader mImageLoader;
public void setImageLoader(ImageLoader imageLoader) {
mImageLoader = imageLoader;
}
//region init //region init
public CardGroupView(@NonNull Context context) { public CardGroupView(@NonNull Context context) {
...@@ -219,7 +226,7 @@ public class CardGroupView extends FrameLayout { ...@@ -219,7 +226,7 @@ public class CardGroupView extends FrameLayout {
refreshLayoutParams(count + 1); refreshLayoutParams(count + 1);
} }
CardView cardView = new CardView(getContext()); CardView cardView = new CardView(getContext());
cardView.showCard(card); cardView.showCard(mImageLoader, card);
addView(cardView, index); addView(cardView, index);
return true; return true;
} }
......
...@@ -91,11 +91,11 @@ public class CardView extends FrameLayout { ...@@ -91,11 +91,11 @@ public class CardView extends FrameLayout {
} }
} }
public void showCard(Card cardInfo) { public void showCard(ImageLoader imageLoader, Card cardInfo) {
if (mCard != null && mCard.equals(cardInfo)) return; if (mCard != null && mCard.equals(cardInfo)) return;
mCard = cardInfo; mCard = cardInfo;
if (cardInfo != null) { if (cardInfo != null && imageLoader != null) {
ImageLoader.get(getContext()).bindImage(mCardView, cardInfo.Code); imageLoader.bindImage(mCardView, cardInfo.Code, null, true);
} else { } else {
mTopImage.setVisibility(View.GONE); mTopImage.setVisibility(View.GONE);
mCardView.setImageBitmap(null); mCardView.setImageBitmap(null);
......
...@@ -20,6 +20,7 @@ import java.util.Map; ...@@ -20,6 +20,7 @@ import java.util.Map;
import cn.garymb.ygomobile.Constants; import cn.garymb.ygomobile.Constants;
import cn.garymb.ygomobile.bean.DeckInfo; import cn.garymb.ygomobile.bean.DeckInfo;
import cn.garymb.ygomobile.lite.R; import cn.garymb.ygomobile.lite.R;
import cn.garymb.ygomobile.loader.ImageLoader;
import cn.garymb.ygomobile.ui.cards.deck.ImageTop; import cn.garymb.ygomobile.ui.cards.deck.ImageTop;
import cn.garymb.ygomobile.ui.cards.deck.LabelInfo; import cn.garymb.ygomobile.ui.cards.deck.LabelInfo;
import ocgcore.data.Card; import ocgcore.data.Card;
...@@ -43,6 +44,11 @@ public class DeckGroupView extends FrameLayout implements View.OnClickListener { ...@@ -43,6 +44,11 @@ public class DeckGroupView extends FrameLayout implements View.OnClickListener {
private CardView mLastView; private CardView mLastView;
private boolean mAutoSort; private boolean mAutoSort;
private EditMode mEditMode; private EditMode mEditMode;
private ImageLoader mImageLoader;
public void setImageLoader(ImageLoader imageLoader) {
mImageLoader = imageLoader;
}
public enum EditMode { public enum EditMode {
None, None,
...@@ -257,7 +263,7 @@ public class DeckGroupView extends FrameLayout implements View.OnClickListener { ...@@ -257,7 +263,7 @@ public class DeckGroupView extends FrameLayout implements View.OnClickListener {
cardView.setSelected(false); cardView.setSelected(false);
if (i < mDeckInfo.getExtraCount()) { if (i < mDeckInfo.getExtraCount()) {
if (i == index && count > 0) { if (i == index && count > 0) {
cardView.showCard(mDeckInfo.getExtraCard(i)); cardView.showCard(mImageLoader, mDeckInfo.getExtraCard(i));
index++; index++;
count--; count--;
} }
...@@ -265,7 +271,7 @@ public class DeckGroupView extends FrameLayout implements View.OnClickListener { ...@@ -265,7 +271,7 @@ public class DeckGroupView extends FrameLayout implements View.OnClickListener {
cardView.updateLimit(getImageTop(), mLimitList); cardView.updateLimit(getImageTop(), mLimitList);
} }
} else { } else {
cardView.showCard(null); cardView.showCard(mImageLoader, null);
} }
} }
resizePadding(Type.Extra, mExtraViews); resizePadding(Type.Extra, mExtraViews);
...@@ -278,11 +284,11 @@ public class DeckGroupView extends FrameLayout implements View.OnClickListener { ...@@ -278,11 +284,11 @@ public class DeckGroupView extends FrameLayout implements View.OnClickListener {
cardView.setSelected(false); cardView.setSelected(false);
orgPos = i % Constants.DECK_WIDTH_MAX_COUNT; orgPos = i % Constants.DECK_WIDTH_MAX_COUNT;
if (orgPos >= mMainLimit) { if (orgPos >= mMainLimit) {
cardView.showCard(null); cardView.showCard(mImageLoader, null);
} else { } else {
if (index < mDeckInfo.getMainCount()) { if (index < mDeckInfo.getMainCount()) {
if (targetIndex == i && count > 0) { if (targetIndex == i && count > 0) {
cardView.showCard(mDeckInfo.getMainCard(index)); cardView.showCard(mImageLoader, mDeckInfo.getMainCard(index));
index++; index++;
targetIndex = (index / mMainLimit) * Constants.DECK_WIDTH_MAX_COUNT + (index % mMainLimit); targetIndex = (index / mMainLimit) * Constants.DECK_WIDTH_MAX_COUNT + (index % mMainLimit);
count--; count--;
...@@ -295,7 +301,7 @@ public class DeckGroupView extends FrameLayout implements View.OnClickListener { ...@@ -295,7 +301,7 @@ public class DeckGroupView extends FrameLayout implements View.OnClickListener {
mMainViews.get(i).updateLimit(getImageTop(), mLimitList); mMainViews.get(i).updateLimit(getImageTop(), mLimitList);
} }
} else { } else {
cardView.showCard(null); cardView.showCard(mImageLoader, null);
} }
} }
} }
...@@ -307,7 +313,7 @@ public class DeckGroupView extends FrameLayout implements View.OnClickListener { ...@@ -307,7 +313,7 @@ public class DeckGroupView extends FrameLayout implements View.OnClickListener {
cardView.setSelected(false); cardView.setSelected(false);
if (i < mDeckInfo.getSideCount()) { if (i < mDeckInfo.getSideCount()) {
if (i == index && count > 0) { if (i == index && count > 0) {
cardView.showCard(mDeckInfo.getSideCard(i)); cardView.showCard(mImageLoader, mDeckInfo.getSideCard(i));
index++; index++;
count--; count--;
} }
...@@ -315,7 +321,7 @@ public class DeckGroupView extends FrameLayout implements View.OnClickListener { ...@@ -315,7 +321,7 @@ public class DeckGroupView extends FrameLayout implements View.OnClickListener {
cardView.updateLimit(getImageTop(), mLimitList); cardView.updateLimit(getImageTop(), mLimitList);
} }
} else { } else {
cardView.showCard(null); cardView.showCard(mImageLoader, null);
} }
} }
resizePadding(Type.Side, mSideViews); resizePadding(Type.Side, mSideViews);
......
...@@ -7,6 +7,7 @@ import android.widget.LinearLayout; ...@@ -7,6 +7,7 @@ import android.widget.LinearLayout;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import cn.garymb.ygomobile.bean.DeckInfo; import cn.garymb.ygomobile.bean.DeckInfo;
import cn.garymb.ygomobile.loader.ImageLoader;
import cn.garymb.ygomobile.ui.cards.deck.ImageTop; import cn.garymb.ygomobile.ui.cards.deck.ImageTop;
import cn.garymb.ygomobile.ui.cards.deck.LabelInfo; import cn.garymb.ygomobile.ui.cards.deck.LabelInfo;
import ocgcore.data.Card; import ocgcore.data.Card;
...@@ -20,6 +21,8 @@ public class DeckView extends LinearLayout { ...@@ -20,6 +21,8 @@ public class DeckView extends LinearLayout {
private final ImageTop mImageTop; private final ImageTop mImageTop;
private boolean mAutoSort, mEditMode, mLimitChanged; private boolean mAutoSort, mEditMode, mLimitChanged;
private ImageLoader mImageLoader;
//region init //region init
public DeckView(Context context) { public DeckView(Context context) {
this(context, null); this(context, null);
...@@ -40,6 +43,7 @@ public class DeckView extends LinearLayout { ...@@ -40,6 +43,7 @@ public class DeckView extends LinearLayout {
mMainGroup = new CardGroupView(context); mMainGroup = new CardGroupView(context);
mExtraGroup = new CardGroupView(context); mExtraGroup = new CardGroupView(context);
mSideGroup = new CardGroupView(context); mSideGroup = new CardGroupView(context);
int cardWidth = 0; int cardWidth = 0;
int cardHeight = 0; int cardHeight = 0;
if (cardWidth <= 0) { if (cardWidth <= 0) {
...@@ -67,6 +71,13 @@ public class DeckView extends LinearLayout { ...@@ -67,6 +71,13 @@ public class DeckView extends LinearLayout {
addView(mSideGroup, new LayoutParams(LayoutParams.MATCH_PARENT, cardHeight)); addView(mSideGroup, new LayoutParams(LayoutParams.MATCH_PARENT, cardHeight));
} }
public void setImageLoader(ImageLoader imageLoader) {
mImageLoader = imageLoader;
mMainGroup.setImageLoader(imageLoader);
mExtraGroup.setImageLoader(imageLoader);
mSideGroup.setImageLoader(imageLoader);
}
public ImageTop getImageTop() { public ImageTop getImageTop() {
return mImageTop; return mImageTop;
} }
......
...@@ -27,6 +27,19 @@ import cn.garymb.ygomobile.Constants; ...@@ -27,6 +27,19 @@ import cn.garymb.ygomobile.Constants;
public class FileUtils { public class FileUtils {
public static boolean isExist(String path){
return path != null && new File(path).exists();
}
public static String getFileExpansion(String path){
int index = path.lastIndexOf(".");
if(index>0){
return path.substring(index+1).toLowerCase();
}
return "";
}
public static void closeQuietly(Closeable closeable) { public static void closeQuietly(Closeable closeable) {
if (closeable != null) { if (closeable != null) {
try { try {
......
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