添加缓存视图控件类
This commit is contained in:
@@ -0,0 +1,127 @@
|
||||
package cc.winboll.studio.powerbell.views;
|
||||
|
||||
import android.content.Context;
|
||||
import android.text.TextUtils;
|
||||
import android.util.AttributeSet;
|
||||
import cc.winboll.studio.libappbase.LogUtils;
|
||||
import cc.winboll.studio.powerbell.models.BackgroundBean;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @Author ZhanGSKen&豆包大模型<zhangsken@qq.com>
|
||||
* @Date 2025/12/21 20:43
|
||||
* @Describe 内存缓存版背景视图控件(基于Java7)
|
||||
* 核心:通过静态Map缓存控件实例,按图片路径唯一标识,支持强制重载图片
|
||||
*/
|
||||
public class MemoryCachedBackgroundView extends BackgroundView {
|
||||
public static final String TAG = "MemoryCachedBackgroundView";
|
||||
// 静态Map:缓存<图片路径, 控件实例>,全局唯一
|
||||
private static final Map<String, MemoryCachedBackgroundView> sViewCacheMap = new HashMap<String, MemoryCachedBackgroundView>();
|
||||
|
||||
// ====================================== 构造器(继承并兼容父类) ======================================
|
||||
public MemoryCachedBackgroundView(Context context) {
|
||||
super(context);
|
||||
LogUtils.d(TAG, "构造器1:创建MemoryCachedBackgroundView实例");
|
||||
}
|
||||
|
||||
public MemoryCachedBackgroundView(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
LogUtils.d(TAG, "构造器2:创建MemoryCachedBackgroundView实例");
|
||||
}
|
||||
|
||||
public MemoryCachedBackgroundView(Context context, AttributeSet attrs, int defStyleAttr) {
|
||||
super(context, attrs, defStyleAttr);
|
||||
LogUtils.d(TAG, "构造器3:创建MemoryCachedBackgroundView实例");
|
||||
}
|
||||
|
||||
// ====================================== 核心静态方法:获取/创建缓存实例 ======================================
|
||||
/**
|
||||
* 从缓存获取或创建MemoryCachedBackgroundView实例
|
||||
* @param context 上下文
|
||||
* @param imagePath 图片绝对路径(作为缓存Key)
|
||||
* @param isReload 是否强制重新加载图片(路径匹配时仍刷新)
|
||||
* @return 缓存/新创建的MemoryCachedBackgroundView实例
|
||||
*/
|
||||
public static MemoryCachedBackgroundView getInstance(Context context, String imagePath, boolean isReload) {
|
||||
LogUtils.d(TAG, "getInstance() 调用 | 图片路径:" + imagePath + " | 是否重载:" + isReload);
|
||||
if (TextUtils.isEmpty(imagePath)) {
|
||||
LogUtils.e(TAG, "getInstance():图片路径为空,创建空实例");
|
||||
return new MemoryCachedBackgroundView(context);
|
||||
}
|
||||
|
||||
// 1. 从Map缓存中获取实例
|
||||
MemoryCachedBackgroundView cachedView = sViewCacheMap.get(imagePath);
|
||||
// 2. 缓存不存在 → 新建实例并加入Map
|
||||
if (cachedView == null) {
|
||||
LogUtils.d(TAG, "getInstance():路径未缓存,新建实例 | " + imagePath);
|
||||
cachedView = new MemoryCachedBackgroundView(context);
|
||||
sViewCacheMap.put(imagePath, cachedView);
|
||||
// 新建实例直接加载图片
|
||||
cachedView.loadImage(imagePath);
|
||||
} else {
|
||||
// 3. 缓存存在 → 根据isReload判断是否强制重载
|
||||
if (isReload) {
|
||||
LogUtils.d(TAG, "getInstance():路径已缓存,强制重载图片 | " + imagePath);
|
||||
cachedView.loadImage(imagePath); // 调用父类loadImage刷新
|
||||
} else {
|
||||
LogUtils.d(TAG, "getInstance():使用缓存实例,无需重载 | " + imagePath);
|
||||
}
|
||||
}
|
||||
return cachedView;
|
||||
}
|
||||
|
||||
// ====================================== 工具方法:缓存管理 ======================================
|
||||
/**
|
||||
* 清除指定路径的缓存实例
|
||||
* @param imagePath 图片路径
|
||||
*/
|
||||
public static void removeCache(String imagePath) {
|
||||
LogUtils.d(TAG, "removeCache() 调用 | 图片路径:" + imagePath);
|
||||
if (TextUtils.isEmpty(imagePath)) {
|
||||
LogUtils.e(TAG, "removeCache():图片路径为空,清除失败");
|
||||
return;
|
||||
}
|
||||
sViewCacheMap.remove(imagePath);
|
||||
LogUtils.d(TAG, "removeCache():已清除缓存 | " + imagePath);
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除所有缓存实例
|
||||
*/
|
||||
public static void clearAllCache() {
|
||||
LogUtils.d(TAG, "clearAllCache() 调用 | 缓存数量:" + sViewCacheMap.size());
|
||||
sViewCacheMap.clear();
|
||||
LogUtils.d(TAG, "clearAllCache():已清除所有缓存实例");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前缓存数量
|
||||
* @return 缓存的实例数量
|
||||
*/
|
||||
public static int getCacheCount() {
|
||||
int count = sViewCacheMap.size();
|
||||
LogUtils.d(TAG, "getCacheCount() 调用 | 当前缓存数量:" + count);
|
||||
return count;
|
||||
}
|
||||
|
||||
// ====================================== 重写父类方法:增强日志 ======================================
|
||||
@Override
|
||||
public void loadImage(String imagePath) {
|
||||
LogUtils.d(TAG, "loadImage() 重载方法调用 | 图片路径:" + imagePath);
|
||||
super.loadImage(imagePath);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loadBackgroundBean(BackgroundBean bean) {
|
||||
LogUtils.d(TAG, "loadBackgroundBean() 重载方法调用 | BackgroundBean:" + (bean == null ? "null" : bean.toString()));
|
||||
super.loadBackgroundBean(bean);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loadBackgroundBean(BackgroundBean bean, boolean isRefresh) {
|
||||
LogUtils.d(TAG, "loadBackgroundBean() 重载方法调用 | BackgroundBean:" + (bean == null ? "null" : bean.toString()) + " | 是否刷新:" + isRefresh);
|
||||
super.loadBackgroundBean(bean, isRefresh);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user