android.content.res.Resources#getResourceTypeName()源码实例Demo

下面列出了android.content.res.Resources#getResourceTypeName() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: sketch   文件: GifTextView.java
@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);
	}
}
 
源代码2 项目: sketch   文件: GifViewUtils.java
@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;
}
 
源代码3 项目: android_9.0.0_r45   文件: ViewDebug.java
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;
}
 
源代码4 项目: DoraemonKit   文件: UIUtils.java
/**
 * 要特别注意 返回的字段包含空格  做判断时一定要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();
}
 
源代码5 项目: Leanplum-Android-SDK   文件: Util.java
/**
 * 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;
}
 
源代码6 项目: weex   文件: ResourcesUtil.java
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();
}
 
源代码7 项目: Android-Shortify   文件: Binder.java
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;
    }
}
 
源代码8 项目: Android-Shortify   文件: Binder.java
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;
    }
}
 
源代码9 项目: Android-Shortify   文件: Binder.java
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;
    }
}
 
源代码10 项目: Android-Shortify   文件: Binder.java
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;
    }
}
 
源代码11 项目: Android-Shortify   文件: Binder.java
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;
    }
}
 
源代码12 项目: Android-Shortify   文件: Binder.java
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;
    }
}
 
源代码13 项目: Android-Shortify   文件: Binder.java
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;
    }
}
 
源代码14 项目: Android-Shortify   文件: Binder.java
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;
    }
}
 
源代码15 项目: stetho   文件: ResourcesUtil.java
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();
}
 
源代码16 项目: CodenameOne   文件: FragmentActivity.java
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();
}
 
源代码17 项目: adt-leanback-support   文件: FragmentActivity.java
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();
}
 
源代码18 项目: V.FlyoutTest   文件: FragmentActivity.java
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();
}
 
源代码19 项目: guideshow   文件: FragmentActivity.java
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();
}
 
源代码20 项目: Genius-Android   文件: Loading.java
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);
    }
}