Java源码示例:android.support.annotation.AnyRes
示例1
/**
* get uri to any resource type
*
* @param resId - resource id
* @return - Uri to resource by given id
*/
public static Uri getUriToResource(@NonNull Activity activity, @AnyRes int resId) {
Uri resUri = null;
try {
/** Return a Resources instance for your application's package. */
Resources res = activity.getResources();
/**
* Creates a Uri which parses the given encoded URI string.
* @param uriString an RFC 2396-compliant, encoded URI
* @throws NullPointerException if uriString is null
* @return Uri for this given uri string
*/
resUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + res.getResourcePackageName(resId) + '/' + res
.getResourceTypeName(resId) + '/' + res.getResourceEntryName(resId));
/** return uri */
} catch (Resources.NotFoundException e) {
e.printStackTrace();
}
return resUri;
}
示例2
/**
* 根据资源id获取资源文件名字 --- 带包名的全路径。
*
* @param context 上下文
* @param resid 资源id
* @return
*/
public static String getResourceName(@NonNull Context context, @AnyRes int resid) {
String res = "";
if (-1 == resid) return res;
try {
res = context.getResources().getResourceName(resid);
} catch (Resources.NotFoundException e) {
// e.printStackTrace();
//LogUtils.d("out", "未获取到ResourceName ---id="+resid);
}
return res;
}
示例3
/**
* 根据资源id获取资源文件名字 --- 简单名称。
*
* @param context 上下文
* @param resid 资源id
* @return
*/
public static String getSimpleResourceName(@NonNull Context context, @AnyRes int resid) {
String res = "";
if (-1 == resid) return res;
try {
res = context.getResources().getResourceName(resid);
res = res.substring(res.indexOf("/") + 1);
} catch (Resources.NotFoundException e) {
// e.printStackTrace();
//LogUtils.d("out", "未获取到ResourceName ---id="+resid);
}
return res;
}
示例4
private void getSkinValue(Context context, @AnyRes int resId, TypedValue outValue, boolean resolveRefs) {
if (!isDefaultSkin) {
int targetResId = getTargetResId(context, resId);
if (targetResId != 0) {
mResources.getValue(targetResId, outValue, resolveRefs);
return;
}
}
context.getResources().getValue(resId, outValue, resolveRefs);
}
示例5
/**
* get uri to drawable or any other resource type if u wish
* @param context - context
* @param drawableId - drawable res id
* @return - uri
*/
public static String getUriToDrawable(@NonNull Context context, @AnyRes int drawableId) {
String imageUri = ContentResolver.SCHEME_ANDROID_RESOURCE +
"://" + context.getResources().getResourcePackageName(drawableId)
+ '/' + context.getResources().getResourceTypeName(drawableId)
+ '/' + context.getResources().getResourceEntryName(drawableId);
return imageUri;
}
示例6
@Nullable private String getApplicationResourceEntryName(@AnyRes int resId)
throws IllegalStateException {
if (!useApplicationResources) {
throw new IllegalStateException(
"You have set the useApplicationResources to false, using application resource is an error.");
}
return Utils.getAndroidResourceEntryName(context, resId);
}
示例7
@Nullable public static String getAndroidResourceEntryName(Context context, @AnyRes int resId) {
try {
return context.getResources().getResourceEntryName(resId);
} catch (android.content.res.Resources.NotFoundException e) {
return null;
}
}
示例8
/**
* Resolves the reference to an attribute, returning the root resource id.
*
* @param context The context to use when determining the root id
* @param attr The attribute to resolve
* @return The resource id pointing to the de-referenced attribute
*/
@AnyRes
public static int getResolvedResourceId(Context context, @AttrRes int attr) {
TypedValue typedValue = new TypedValue();
Resources.Theme theme = context.getTheme();
theme.resolveAttribute(attr, typedValue, true);
if (typedValue.type == TypedValue.TYPE_REFERENCE) {
return typedValue.data;
}
return typedValue.resourceId;
}
示例9
public static String getResourceEntryName(@AnyRes int anyRes) {
return Base.getResources().getResourceEntryName(anyRes);
}
示例10
public static String getResourceName(@AnyRes int anyRes) {
return Base.getResources().getResourceName(anyRes);
}
示例11
public static String getResourcePackageName(@AnyRes int anyRes) {
return Base.getResources().getResourcePackageName(anyRes);
}
示例12
public static String getResourceTypeName(@AnyRes int anyRes) {
return Base.getResources().getResourceTypeName(anyRes);
}
示例13
public static void getValue(@AnyRes int anyRes, TypedValue outValue, boolean resolveRefs) {
Base.getResources().getValue(anyRes, outValue, resolveRefs);
}
示例14
public static void getValueForDensity(@AnyRes int anyRes, int density, TypedValue outValue, boolean resolveRefs) {
if (APILevel.require(15))
Base.getResources().getValueForDensity(anyRes, density, outValue, resolveRefs);
else
Base.getResources().getValue(anyRes, outValue, resolveRefs);
}
示例15
public static Uri getUriFromResource(@NonNull Context context, @AnyRes int resId) throws Resources.NotFoundException {
Resources res = context.getResources();
Uri resUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + res.getResourcePackageName(resId) + '/' + res.getResourceTypeName(resId) + '/' + res.getResourceEntryName(resId));
return resUri;
}
示例16
@AnyRes
public static int getResourceId(TypedArray a, @StyleableRes int index, @StyleableRes int fallbackIndex, @AnyRes int defaultValue) {
return a.getResourceId(index, a.getResourceId(fallbackIndex, defaultValue));
}
示例17
public static void getValue(Context context, @AnyRes int resId, TypedValue outValue, boolean resolveRefs) {
getInstance().getSkinValue(context, resId, outValue, resolveRefs);
}
示例18
/**
* @param model
* the model that will be bound.
* @return the type of the viewholder.
*/
@AnyRes
int getViewType(U model);