static boolean isXmlResource(Resources resources, @DrawableRes int drawableId) { TypedValue typedValue = new TypedValue(); resources.getValue(drawableId, typedValue, true); CharSequence file = typedValue.string; return file != null && file.toString().endsWith(".xml"); }
public static Drawable getDrawable(Resources res, int resid, Resources.Theme theme) { TypedValue value = new TypedValue(); res.getValue(resid, value, true); return loadDrawable(res, value, theme); }
@UiThread // Implicit synchronization for use of shared resource VALUE. public static float getFloat(Context context, @DimenRes int id) { TypedValue value = VALUE; context.getResources().getValue(id, value, true); if (value.type == TypedValue.TYPE_FLOAT) { return value.getFloat(); } throw new Resources.NotFoundException("Resource ID #0x" + Integer.toHexString(id) + " type #0x" + Integer.toHexString(value.type) + " is not valid"); }
static float getDensityScale(@NonNull Resources res, @DrawableRes @RawRes int id) { final TypedValue value = new TypedValue(); res.getValue(id, value, true); final int resourceDensity = value.density; final int density; if (resourceDensity == TypedValue.DENSITY_DEFAULT) { density = DisplayMetrics.DENSITY_DEFAULT; } else if (resourceDensity != TypedValue.DENSITY_NONE) { density = resourceDensity; } else { density = 0; } final int targetDensity = res.getDisplayMetrics().densityDpi; if (density > 0 && targetDensity > 0) { return (float) targetDensity / density; } return 1f; }
static Resources mockResources(final String resValueString) { Resources resources = mock(Resources.class); doAnswer(new Answer<Void>() { @Override public Void answer(InvocationOnMock invocation) { Object[] args = invocation.getArguments(); ((TypedValue) args[1]).string = resValueString; return null; } }).when(resources).getValue(anyInt(), any(TypedValue.class), anyBoolean()); return resources; }
@Test public void getMultilineLayoutResource_shouldResolveLayoutReferencesWithLineBreaks() { // multiline_layout is a layout reference to activity_main layout. TypedValue multilineLayoutValue = new TypedValue(); resources.getValue(R.layout.multiline_layout, multilineLayoutValue, true /* resolveRefs */); TypedValue mainActivityLayoutValue = new TypedValue(); resources.getValue(R.layout.activity_main, mainActivityLayoutValue, false /* resolveRefs */); assertThat(multilineLayoutValue.string).isEqualTo(mainActivityLayoutValue.string); }
private Resources getMockedResources(final int resourceDensity, final int displayDensity) { doAnswer(new Answer<TypedValue>() { @Override public TypedValue answer(InvocationOnMock invocation) throws Throwable { final TypedValue outValue = invocation.getArgument(1); outValue.density = resourceDensity; return outValue; } }).when(resources).getValue(anyInt(), any(TypedValue.class), anyBoolean()); final DisplayMetrics displayMetrics = new DisplayMetrics(); displayMetrics.densityDpi = displayDensity; when(resources.getDisplayMetrics()).thenReturn(displayMetrics); return resources; }
@Test public void getResourceValue_colorARGB8() { TypedValue outValue = new TypedValue(); resources.getValue(test_ARGB8, outValue, false); assertThat(outValue.type).isEqualTo(TYPE_INT_COLOR_ARGB8); assertThat(Color.blue(outValue.data)).isEqualTo(2); }
@Test public void asFloat() { Target target = new Target(); TypedValue value = new TypedValue(); context.getResources().getValue(R.dimen.twelve_point_two, value, true); float expected = value.getFloat(); Unbinder unbinder = new BindFloatTest$Target_ViewBinding(target, context); assertThat(target.actual).isEqualTo(expected); unbinder.unbind(); assertThat(target.actual).isEqualTo(expected); } }
@Test public void getResourceValue_colorRGB8() { TypedValue outValue = new TypedValue(); resources.getValue(test_RGB8, outValue, false); assertThat(outValue.type).isEqualTo(TYPE_INT_COLOR_RGB8); assertThat(Color.blue(outValue.data)).isEqualTo(4); }
res.getValue(resId, typedValue, true); Drawable dr = null;
context.getResources().getValue(resId, value, true); ColorStateList cl = null; if (value.type >= TypedValue.TYPE_FIRST_COLOR_INT
@Test public void getValueShouldClearTypedArrayBetweenCalls() throws Exception { TypedValue outValue = new TypedValue(); resources.getValue(R.string.hello, outValue, true); assertThat(outValue.type).isEqualTo(TYPE_STRING); assertThat(outValue.string).isEqualTo(resources.getString(R.string.hello)); // outValue.data is an index into the String block which we don't know for raw xml resources. assertThat(outValue.assetCookie).isNotEqualTo(0); resources.getValue(R.color.blue, outValue, true); assertThat(outValue.type).isEqualTo(TYPE_INT_COLOR_RGB8); assertThat(outValue.data).isEqualTo(0xFF0000FF); assertThat(outValue.string).isNull(); // outValue.assetCookie is not supported with raw XML resources.getValue(R.integer.loneliest_number, outValue, true); assertThat(outValue.type).isEqualTo(TYPE_INT_DEC); assertThat(outValue.data).isEqualTo(1); assertThat(outValue.string).isNull(); resources.getValue(R.bool.true_bool_value, outValue, true); assertThat(outValue.type).isEqualTo(TYPE_INT_BOOLEAN); assertThat(outValue.data).isNotEqualTo(0); // true == traditionally 0xffffffff, -1 in Java but // tests should be checking for non-zero assertThat(outValue.string).isNull(); }
@SuppressWarnings("unused") public static float resolveFloatResource(Resources res, int resId) { TypedValue outValue = new TypedValue(); res.getValue(resId, outValue, true); return outValue.getFloat(); } }
maxCameraDistance = resources.getDimensionPixelSize(R.dimen.max_camera_distance); TypedValue typedValue = new TypedValue(); resources.getValue(R.dimen.swiped_tab_scale, typedValue, true); swipedTabScale = typedValue.getFloat(); resources.getValue(R.dimen.swiped_tab_alpha, typedValue, true); swipedTabAlpha = typedValue.getFloat(); showSwitcherAnimationDuration =
@SuppressWarnings("unused") public static float resolveFloatResource(Resources res, int resId) { TypedValue outValue = new TypedValue(); res.getValue(resId, outValue, true); return outValue.getFloat(); } }
Resources res = getResources; DisplayMetrics metrics = res.getDisplayMetrics(); TypedValue outValue = new TypedValue(); // Loads the raw data into the typed value res.getValue(android.R.dimen.app_icon_size, outValue, true); // Converts the raw data into a usable value int appIconSizePx = outValue.getDimension(metrics);