package com.nd.sdp.android.common.skinmanager.external;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.support.annotation.ColorInt;
import android.support.annotation.Nullable;
import android.support.v4.util.ArrayMap;
import android.text.TextUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Attributes;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.List;
public enum ResourceManager {
INSTANCE;
public static final String VALUE_DIR = "values";
public static final String COLOR_DIR = "color";
public static final String DRAWABLE_DIR = "drawable";
public static final String NINE_DRAWABLE_SUFFIX = ".9.png";
public static final String PNG_SUFFIX = ".png";
public static final String XML_SUFFIX = ".xml";
private ArrayMap<String, ColorStateList> mColorStateCache = new ArrayMap<>();
private ArrayMap<String, SoftReference<Drawable>> mDrawableCache = new ArrayMap<>();
private FilenameFilter mXmlFilter = new FilenameFilter() {
@Override
public boolean accept(File dir, String filename) {
return filename.endsWith(XML_SUFFIX);
}
};
private FilenameFilter mDrawableFilter = new FilenameFilter() {
@Override
public boolean accept(File dir, String filename) {
return filename.endsWith(PNG_SUFFIX);
}
};
ResourceManager() {
}
public Drawable getDrawableByName(Context context, String name) {
parseDrawable(context);
if (!mDrawableCache.containsKey(name)) {
return null;
}
final SoftReference<Drawable> drawableSoftReference = mDrawableCache.get(name);
return drawableSoftReference.get();
}
private void parseDrawable(Context context) {
if (mDrawableCache.isEmpty()) {
try {
final File skinDirectory = ExternalSkinManager.INSTANCE.getSkinDirectory();
final File drawableDir = new File(skinDirectory, DRAWABLE_DIR);
if (drawableDir.exists()) {
final File[] files = drawableDir.listFiles(mXmlFilter);
for (File file : files) {
final Document parse = Jsoup.parse(file, "UTF-8");
final Elements selectors = parse.getElementsByTag("selector");
if (selectors.isEmpty()) {
continue;
}
final Element selector = selectors.get(0);
final Elements children = selector.children();
StateListDrawable listDrawable = new StateListDrawable();
for (int i = 0; i < children.size(); i++) {
Element child = children.get(i);
if ("item".equals(child.tagName())) {
final Attributes attributes = child.attributes();
String value = attributes.get("android:drawable");
int[] statesArr = getStatesArr(attributes);
listDrawable.addState(statesArr, getDrawableFromFile(context, drawableDir.getAbsolutePath(), value));
}
}
String name = file.getName().replace(XML_SUFFIX, "");
SoftReference<Drawable> softReference = new SoftReference<Drawable>(listDrawable);
mDrawableCache.put(name, softReference);
}
final File[] pngFiles = drawableDir.listFiles(mDrawableFilter);
for (File file : pngFiles) {
SoftReference<Drawable> softReference;
String name;
if (file.getName().endsWith(NINE_DRAWABLE_SUFFIX)) {
name = file.getName().replace(NINE_DRAWABLE_SUFFIX, "");
Drawable patchy = NinePatchUtils.decodeDrawableFromFile(context, file.getAbsolutePath());
softReference = new SoftReference<>(patchy);
} else {
Drawable drawable = Drawable.createFromPath(file.getAbsolutePath());
name = file.getName().replace(PNG_SUFFIX, "");
softReference = new SoftReference<>(drawable);
}
mDrawableCache.put(name, softReference);
}
}
} catch (Exception e) {
e.printStackTrace();
// throw new Resources.NotFoundException();
}
}
}
@Nullable
private Drawable getDrawableFromFile(Context context, String path, String value) throws Exception {
String fileName = String.format("%s%s", value.replace("@drawable/", ""), PNG_SUFFIX);
File drawableFile = new File(path, fileName);
String nineFileName = String.format("%s%s", value.replace("@drawable/", ""), NINE_DRAWABLE_SUFFIX);
File nineDrawableFile = new File(path, nineFileName);
if (drawableFile.exists()) {
return Drawable.createFromPath(drawableFile.getAbsolutePath());
} else if (nineDrawableFile.exists()) {
return NinePatchUtils.decodeDrawableFromFile(context, nineDrawableFile.getAbsolutePath());
} else {
return null;
}
}
@ColorInt
public int getColor(String name) throws Resources.NotFoundException {
parseColorStateList();
if (!mColorStateCache.containsKey(name)) {
throw new Resources.NotFoundException();
}
final ColorStateList colorStateList = mColorStateCache.get(name);
return colorStateList.getDefaultColor();
}
@Nullable
public ColorStateList getColorStateList(String name) {
parseColorStateList();
if (!mColorStateCache.containsKey(name)) {
return null;
}
return mColorStateCache.get(name);
}
private void parseColorStateList() {
if (mColorStateCache == null || mColorStateCache.isEmpty()) {
try {
final File skinDirectory = ExternalSkinManager.INSTANCE.getSkinDirectory();
final File valueDir = new File(skinDirectory, VALUE_DIR);
if (valueDir.exists()) {
final File[] files = valueDir.listFiles(mXmlFilter);
for (File file : files) {
final Document parse = Jsoup.parse(file, "UTF-8");
final Elements colors = parse.getElementsByTag("color");
for (Element color : colors) {
final String name = color.attr("name");
final int parseColor = Color.parseColor(color.html());
int[][] states = new int[1][];
states[0] = new int[]{};
final ColorStateList colorStateList = new ColorStateList(states, new int[]{parseColor});
mColorStateCache.put(name, colorStateList);
}
}
}
// ColorState
final File colorDir = new File(skinDirectory, COLOR_DIR);
if (colorDir.exists()) {
final
没有合适的资源?快使用搜索试试~ 我知道了~
Android 轻量级动态换肤框架
共15个文件
java:11个
xml:4个
需积分: 25 8 下载量 201 浏览量
2016-05-12
16:39:20
上传
评论 1
收藏 17KB ZIP 举报
温馨提示
一共就不到10个类,就能够轻松实现android从SD卡中获取图片、color等资源
资源推荐
资源详情
资源评论
收起资源包目录
skin_src.zip (15个子文件)
src
test
java
com
nd
sdp
android
common
skinmanager
external
ExampleUnitTest.java 353B
androidTest
java
com
nd
sdp
android
common
skinmanager
external
ApplicationTest.java 389B
main
res
drawable
values
colors.xml 67B
strings.xml 27B
ids.xml 116B
java
com
nd
sdp
android
common
skinmanager
external
IExternalSkinManager.java 267B
ResourceManager.java 12KB
ExternalSkinManagerFactory.java 221B
ExternalSkinManager.java 1KB
attr
SkinView.java 623B
SkinAttr.java 462B
SkinAttrSupport.java 3KB
SkinAttrType.java 2KB
NinePatchUtils.java 9KB
AndroidManifest.xml 317B
共 15 条
- 1
资源评论
Android搬运工
- 粉丝: 0
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功