下面列出了android.content.res.Resources#getResourceTypeName() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@SuppressWarnings("deprecation") //Resources#getDrawable(int)
private Drawable getGifOrDefaultDrawable(int resId) {
if (resId == 0) {
return null;
}
final Resources resources = getResources();
final String resourceTypeName = resources.getResourceTypeName(resId);
if (!isInEditMode() && GifViewUtils.SUPPORTED_RESOURCE_TYPE_NAMES.contains(resourceTypeName)) {
try {
return new GifDrawable(resources, resId);
} catch (IOException | NotFoundException ignored) {
// ignored
}
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
return resources.getDrawable(resId, getContext().getTheme());
} else {
return resources.getDrawable(resId);
}
}
@SuppressWarnings("deprecation")
static boolean setResource(ImageView view, boolean isSrc, int resId) {
Resources res = view.getResources();
if (res != null) {
try {
final String resourceTypeName = res.getResourceTypeName(resId);
if (!SUPPORTED_RESOURCE_TYPE_NAMES.contains(resourceTypeName)) {
return false;
}
GifDrawable d = new GifDrawable(res, resId);
if (isSrc) {
view.setImageDrawable(d);
} else {
view.setBackground(d);
}
return true;
} catch (IOException | Resources.NotFoundException ignored) {
//ignored
}
}
return false;
}
static Object resolveId(Context context, int id) {
Object fieldValue;
final Resources resources = context.getResources();
if (id >= 0) {
try {
fieldValue = resources.getResourceTypeName(id) + '/' +
resources.getResourceEntryName(id);
} catch (Resources.NotFoundException e) {
fieldValue = "id/" + formatIntToHexString(id);
}
} else {
fieldValue = "NO_ID";
}
return fieldValue;
}
/**
* 要特别注意 返回的字段包含空格 做判断时一定要trim()
*
* @param view
* @return
*/
public static String getIdText(View view) {
final int id = view.getId();
StringBuilder out = new StringBuilder();
if (id != View.NO_ID) {
final Resources r = view.getResources();
if (id > 0 && resourceHasPackage(id) && r != null) {
try {
String pkgname;
switch (id & 0xff000000) {
case 0x7f000000:
pkgname = "app";
break;
case 0x01000000:
pkgname = "android";
break;
default:
pkgname = r.getResourcePackageName(id);
break;
}
String typename = r.getResourceTypeName(id);
String entryname = r.getResourceEntryName(id);
out.append(" ");
out.append(pkgname);
out.append(":");
out.append(typename);
out.append("/");
out.append(entryname);
} catch (Resources.NotFoundException e) {
e.printStackTrace();
}
}
}
return TextUtils.isEmpty(out.toString()) ? "" : out.toString();
}
/**
* Generates a Resource name from resourceId located in res/ folder.
*
* @param resourceId id of the resource, must be greater then 0.
* @return resourceName in format folder/file.extension.
*/
public static String generateResourceNameFromId(int resourceId) {
try {
if (resourceId <= 0) {
Log.w("Provided resource id is invalid.");
return null;
}
Resources resources = Leanplum.getContext().getResources();
// Get entryName from resourceId, which represents a file name in res/ directory.
String entryName = resources.getResourceEntryName(resourceId);
// Get typeName from resourceId, which represents a folder where file is located in
// res/ directory.
String typeName = resources.getResourceTypeName(resourceId);
// By using TypedValue we can get full path of a file with extension.
TypedValue value = new TypedValue();
resources.getValue(resourceId, value, true);
// Regex matching to find real file extension, "image.img.png" will produce "png".
String[] fullFileName = value.string.toString().split("\\.(?=[^\\.]+$)");
String extension = "";
// If extension is found, we will append dot before it.
if (fullFileName.length == 2) {
extension = "." + fullFileName[1];
}
// Return full resource name in format: drawable/image.png
return typeName + "/" + entryName + extension;
} catch (Exception e) {
Log.w("Failed to generate resource name from provided resource id: ", e);
Util.handleException(e);
}
return null;
}
public static String getIdString(@Nullable Resources r, int resourceId)
throws Resources.NotFoundException {
if (r == null) {
return getFallbackIdString(resourceId);
}
String prefix;
String prefixSeparator;
switch (getResourcePackageId(resourceId)) {
case 0x7f:
prefix = "";
prefixSeparator = "";
break;
default:
prefix = r.getResourcePackageName(resourceId);
prefixSeparator = ":";
break;
}
String typeName = r.getResourceTypeName(resourceId);
String entryName = r.getResourceEntryName(resourceId);
StringBuilder sb = new StringBuilder(
1 + prefix.length() + prefixSeparator.length() +
typeName.length() + 1 + entryName.length());
sb.append("@");
sb.append(prefix);
sb.append(prefixSeparator);
sb.append(typeName);
sb.append("/");
sb.append(entryName);
return sb.toString();
}
public static <T extends View, E extends String>T bind(int resId){
Resources res = sActivity.getResources();
String resourceName = res.getResourceTypeName(resId);
if(resourceName.equalsIgnoreCase("id")) {
T cl = (T) sActivity.findViewById(resId);
return cl;
}else {
return null;
}
}
public static String bindString(int resId){
Resources res = sActivity.getResources();
String resourceName = res.getResourceTypeName(resId);
if(resourceName.equalsIgnoreCase("string")) {
return res.getString(resId);
}else{
return null;
}
}
public static float bindDimension(int resId){
Resources res = sActivity.getResources();
String resourceName = res.getResourceTypeName(resId);
if(resourceName.equalsIgnoreCase("dimen")) {
return res.getDimension(resId);
}else{
return 0f;
}
}
public static Animation bindAnimation(int resId){
Resources res = sActivity.getResources();
String resourceName = res.getResourceTypeName(resId);
if(resourceName.equalsIgnoreCase("anim")) {
return AnimationUtils.loadAnimation(sActivity, resId);
}else{
return null;
}
}
public static Drawable bindDrawable(int resId){
Resources res = sActivity.getResources();
String resourceName = res.getResourceTypeName(resId);
if(resourceName.equalsIgnoreCase("drawable")) {
return res.getDrawable(resId);
}else{
return null;
}
}
public static int bindColor(int resId){
Resources res = sActivity.getResources();
String resourceName = res.getResourceTypeName(resId);
if(resourceName.equalsIgnoreCase("color")) {
return res.getColor(resId);
}else{
return 0;
}
}
public static int bindInteger(int resId){
Resources res = sActivity.getResources();
String resourceName = res.getResourceTypeName(resId);
if(resourceName.equalsIgnoreCase("integer")) {
return res.getInteger(resId);
}else{
return 0;
}
}
public static boolean bindBoolean(int resId){
Resources res = sActivity.getResources();
String resourceName = res.getResourceTypeName(resId);
if(resourceName.equalsIgnoreCase("bool")) {
return res.getBoolean(resId);
}else{
return false;
}
}
public static String getIdString(@Nullable Resources r, int resourceId)
throws Resources.NotFoundException {
if (r == null) {
return getFallbackIdString(resourceId);
}
String prefix;
String prefixSeparator;
switch (getResourcePackageId(resourceId)) {
case 0x7f:
prefix = "";
prefixSeparator = "";
break;
default:
prefix = r.getResourcePackageName(resourceId);
prefixSeparator = ":";
break;
}
String typeName = r.getResourceTypeName(resourceId);
String entryName = r.getResourceEntryName(resourceId);
StringBuilder sb = new StringBuilder(
1 + prefix.length() + prefixSeparator.length() +
typeName.length() + 1 + entryName.length());
sb.append("@");
sb.append(prefix);
sb.append(prefixSeparator);
sb.append(typeName);
sb.append("/");
sb.append(entryName);
return sb.toString();
}
private static String viewToString(View view) {
StringBuilder out = new StringBuilder(128);
out.append(view.getClass().getName());
out.append('{');
out.append(Integer.toHexString(System.identityHashCode(view)));
out.append(' ');
switch (view.getVisibility()) {
case View.VISIBLE: out.append('V'); break;
case View.INVISIBLE: out.append('I'); break;
case View.GONE: out.append('G'); break;
default: out.append('.'); break;
}
out.append(view.isFocusable() ? 'F' : '.');
out.append(view.isEnabled() ? 'E' : '.');
out.append(view.willNotDraw() ? '.' : 'D');
out.append(view.isHorizontalScrollBarEnabled()? 'H' : '.');
out.append(view.isVerticalScrollBarEnabled() ? 'V' : '.');
out.append(view.isClickable() ? 'C' : '.');
out.append(view.isLongClickable() ? 'L' : '.');
out.append(' ');
out.append(view.isFocused() ? 'F' : '.');
out.append(view.isSelected() ? 'S' : '.');
out.append(view.isPressed() ? 'P' : '.');
out.append(' ');
out.append(view.getLeft());
out.append(',');
out.append(view.getTop());
out.append('-');
out.append(view.getRight());
out.append(',');
out.append(view.getBottom());
final int id = view.getId();
if (id != View.NO_ID) {
out.append(" #");
out.append(Integer.toHexString(id));
final Resources r = view.getResources();
if (id != 0 && r != null) {
try {
String pkgname;
switch (id&0xff000000) {
case 0x7f000000:
pkgname="app";
break;
case 0x01000000:
pkgname="android";
break;
default:
pkgname = r.getResourcePackageName(id);
break;
}
String typename = r.getResourceTypeName(id);
String entryname = r.getResourceEntryName(id);
out.append(" ");
out.append(pkgname);
out.append(":");
out.append(typename);
out.append("/");
out.append(entryname);
} catch (Resources.NotFoundException e) {
}
}
}
out.append("}");
return out.toString();
}
private static String viewToString(View view) {
StringBuilder out = new StringBuilder(128);
out.append(view.getClass().getName());
out.append('{');
out.append(Integer.toHexString(System.identityHashCode(view)));
out.append(' ');
switch (view.getVisibility()) {
case View.VISIBLE: out.append('V'); break;
case View.INVISIBLE: out.append('I'); break;
case View.GONE: out.append('G'); break;
default: out.append('.'); break;
}
out.append(view.isFocusable() ? 'F' : '.');
out.append(view.isEnabled() ? 'E' : '.');
out.append(view.willNotDraw() ? '.' : 'D');
out.append(view.isHorizontalScrollBarEnabled()? 'H' : '.');
out.append(view.isVerticalScrollBarEnabled() ? 'V' : '.');
out.append(view.isClickable() ? 'C' : '.');
out.append(view.isLongClickable() ? 'L' : '.');
out.append(' ');
out.append(view.isFocused() ? 'F' : '.');
out.append(view.isSelected() ? 'S' : '.');
out.append(view.isPressed() ? 'P' : '.');
out.append(' ');
out.append(view.getLeft());
out.append(',');
out.append(view.getTop());
out.append('-');
out.append(view.getRight());
out.append(',');
out.append(view.getBottom());
final int id = view.getId();
if (id != View.NO_ID) {
out.append(" #");
out.append(Integer.toHexString(id));
final Resources r = view.getResources();
if (id != 0 && r != null) {
try {
String pkgname;
switch (id&0xff000000) {
case 0x7f000000:
pkgname="app";
break;
case 0x01000000:
pkgname="android";
break;
default:
pkgname = r.getResourcePackageName(id);
break;
}
String typename = r.getResourceTypeName(id);
String entryname = r.getResourceEntryName(id);
out.append(" ");
out.append(pkgname);
out.append(":");
out.append(typename);
out.append("/");
out.append(entryname);
} catch (Resources.NotFoundException e) {
}
}
}
out.append("}");
return out.toString();
}
private static String viewToString(View view) {
StringBuilder out = new StringBuilder(128);
out.append(view.getClass().getName());
out.append('{');
out.append(Integer.toHexString(System.identityHashCode(view)));
out.append(' ');
switch (view.getVisibility()) {
case View.VISIBLE: out.append('V'); break;
case View.INVISIBLE: out.append('I'); break;
case View.GONE: out.append('G'); break;
default: out.append('.'); break;
}
out.append(view.isFocusable() ? 'F' : '.');
out.append(view.isEnabled() ? 'E' : '.');
out.append(view.willNotDraw() ? '.' : 'D');
out.append(view.isHorizontalScrollBarEnabled()? 'H' : '.');
out.append(view.isVerticalScrollBarEnabled() ? 'V' : '.');
out.append(view.isClickable() ? 'C' : '.');
out.append(view.isLongClickable() ? 'L' : '.');
out.append(' ');
out.append(view.isFocused() ? 'F' : '.');
out.append(view.isSelected() ? 'S' : '.');
out.append(view.isPressed() ? 'P' : '.');
out.append(' ');
out.append(view.getLeft());
out.append(',');
out.append(view.getTop());
out.append('-');
out.append(view.getRight());
out.append(',');
out.append(view.getBottom());
final int id = view.getId();
if (id != View.NO_ID) {
out.append(" #");
out.append(Integer.toHexString(id));
final Resources r = view.getResources();
if (id != 0 && r != null) {
try {
String pkgname;
switch (id&0xff000000) {
case 0x7f000000:
pkgname="app";
break;
case 0x01000000:
pkgname="android";
break;
default:
pkgname = r.getResourcePackageName(id);
break;
}
String typename = r.getResourceTypeName(id);
String entryname = r.getResourceEntryName(id);
out.append(" ");
out.append(pkgname);
out.append(":");
out.append(typename);
out.append("/");
out.append(entryname);
} catch (Resources.NotFoundException e) {
}
}
}
out.append("}");
return out.toString();
}
private static String viewToString(View view) {
StringBuilder out = new StringBuilder(128);
out.append(view.getClass().getName());
out.append('{');
out.append(Integer.toHexString(System.identityHashCode(view)));
out.append(' ');
switch (view.getVisibility()) {
case View.VISIBLE: out.append('V'); break;
case View.INVISIBLE: out.append('I'); break;
case View.GONE: out.append('G'); break;
default: out.append('.'); break;
}
out.append(view.isFocusable() ? 'F' : '.');
out.append(view.isEnabled() ? 'E' : '.');
out.append(view.willNotDraw() ? '.' : 'D');
out.append(view.isHorizontalScrollBarEnabled()? 'H' : '.');
out.append(view.isVerticalScrollBarEnabled() ? 'V' : '.');
out.append(view.isClickable() ? 'C' : '.');
out.append(view.isLongClickable() ? 'L' : '.');
out.append(' ');
out.append(view.isFocused() ? 'F' : '.');
out.append(view.isSelected() ? 'S' : '.');
out.append(view.isPressed() ? 'P' : '.');
out.append(' ');
out.append(view.getLeft());
out.append(',');
out.append(view.getTop());
out.append('-');
out.append(view.getRight());
out.append(',');
out.append(view.getBottom());
final int id = view.getId();
if (id != View.NO_ID) {
out.append(" #");
out.append(Integer.toHexString(id));
final Resources r = view.getResources();
if (id != 0 && r != null) {
try {
String pkgname;
switch (id&0xff000000) {
case 0x7f000000:
pkgname="app";
break;
case 0x01000000:
pkgname="android";
break;
default:
pkgname = r.getResourcePackageName(id);
break;
}
String typename = r.getResourceTypeName(id);
String entryname = r.getResourceEntryName(id);
out.append(" ");
out.append(pkgname);
out.append(":");
out.append(typename);
out.append("/");
out.append(entryname);
} catch (Resources.NotFoundException e) {
}
}
}
out.append("}");
return out.toString();
}
private void init(AttributeSet attrs, int defStyleAttr, int defStyleRes) {
final Context context = getContext();
final Resources resource = getResources();
if (attrs == null) {
// default we init a circle style loading drawable
setProgressStyle(STYLE_CIRCLE);
return;
}
final float density = resource.getDisplayMetrics().density;
// default size 2dp
final int baseSize = (int) (density * 2);
// Load attributes
final TypedArray a = context.obtainStyledAttributes(
attrs, R.styleable.Loading, defStyleAttr, defStyleRes);
int bgLineSize = a.getDimensionPixelOffset(R.styleable.Loading_gBackgroundLineSize, baseSize);
int fgLineSize = a.getDimensionPixelOffset(R.styleable.Loading_gForegroundLineSize, baseSize);
int bgColor = 0;// transparent color
ColorStateList colorStateList = a.getColorStateList(R.styleable.Loading_gBackgroundColor);
if (colorStateList != null)
bgColor = colorStateList.getDefaultColor();
int fgColor = Color.BLACK;
int[] fgColorArray = null;
try {
fgColor = a.getColor(R.styleable.Loading_gForegroundColor, 0);
} catch (Exception ignored) {
int fgColorId = a.getResourceId(R.styleable.Loading_gForegroundColor, R.array.g_default_loading_fg);
// Check for IDE preview render
if (!isInEditMode()) {
TypedArray taColor = resource.obtainTypedArray(fgColorId);
int length = taColor.length();
if (length > 0) {
fgColorArray = new int[length];
for (int i = 0; i < length; i++) {
fgColorArray[i] = taColor.getColor(i, Color.BLACK);
}
} else {
String type = resource.getResourceTypeName(fgColorId);
try {
switch (type) {
case "color":
fgColor = resource.getColor(fgColorId);
break;
case "array":
fgColorArray = resource.getIntArray(fgColorId);
break;
default:
fgColorArray = resource.getIntArray(R.array.g_default_loading_fg);
break;
}
} catch (Exception e) {
fgColorArray = resource.getIntArray(R.array.g_default_loading_fg);
}
}
taColor.recycle();
}
}
int style = a.getInt(R.styleable.Loading_gProgressStyle, 1);
boolean autoRun = a.getBoolean(R.styleable.Loading_gAutoRun, true);
float progress = a.getFloat(R.styleable.Loading_gProgressFloat, 0);
a.recycle();
setProgressStyle(style);
setAutoRun(autoRun);
setProgress(progress);
setBackgroundLineSize(bgLineSize);
setForegroundLineSize(fgLineSize);
setBackgroundColor(bgColor);
if (fgColorArray == null) {
setForegroundColor(fgColor);
} else {
setForegroundColor(fgColorArray);
}
}