private int getThreadColor(int itemViewType) { return mColorCoded ? mColors.getColor(itemViewType % mColors.length(), 0) : 0; }
TypedArray ar = context.getResources().obtainTypedArray(R.array.my_array); int len = ar.length(); int[] resIds = new int[len]; for (int i = 0; i < len; i++) resIds[i] = ar.getResourceId(i, 0); ar.recycle(); // Do stuff with resolved reference array, resIds[]... for (int i = 0; i < len; i++) Log.v (TAG, "Res Id " + i + " is " + Integer.toHexString(resIds[i]));
Resources res = getResources(); TypedArray ta = res.obtainTypedArray(R.array.array0); int n = ta.length(); String[][] array = new String[n][]; for (int i = 0; i < n; ++i) { int id = ta.getResourceId(i, 0); if (id > 0) { array[i] = res.getStringArray(id); } else { // something wrong with the XML } } ta.recycle(); // Important!
private int[] loadStyleList(Context context, int resId){ if(context == null) return null; TypedArray array = context.getResources().obtainTypedArray(resId); int[] result = new int[array.length()]; for(int i = 0; i < result.length; i++) result[i] = array.getResourceId(i, 0); array.recycle(); return result; }
public static int getMaterialColor(Resources resources, int index) { TypedArray colors = resources.obtainTypedArray(R.array.mdcolor_300); final int returnColor = colors.getColor(index % colors.length(), Color.BLACK); colors.recycle(); return returnColor; }
private Drawable[] loadScreenIcons() { TypedArray ta = getResources().obtainTypedArray(R.array.ld_activityScreenIcons); Drawable[] icons = new Drawable[ta.length()]; for (int i = 0; i < ta.length(); i++) { int id = ta.getResourceId(i, 0); if (id != 0) { icons[i] = ContextCompat.getDrawable(this, id); } } ta.recycle(); return icons; }
/** * Get color array values form array resource * * @param resources Resources * @param resId Resources id * @return color array */ public static int[] getColorsFromArrayRes(Resources resources, int resId) { try { @SuppressLint("Recycle") TypedArray array = resources.obtainTypedArray(resId); if (array.length() > 0) { final int len = array.length(); final int[] colors = new int[len]; for (int i = 0; i < len; i++) { colors[i] = array.getColor(i, 0); } return colors; } } catch (Resources.NotFoundException ignored) { } return null; }
/** * Override the image ids of this button. */ public void overrideImageIds(int resId) { TypedArray ids = null; try { ids = getResources().obtainTypedArray(resId); mImageIds = new int[ids.length()]; for (int i = 0; i < ids.length(); i++) { mImageIds[i] = ids.getResourceId(i, 0); } } finally { if (ids != null) { ids.recycle(); } } }
TypedArray ta = context.obtainStyledAttributes(attrs, attrsArray, defStyleAttr, defStyleRes); boolean ret = defaultValue; if (ta.length() > 0) { ret = ta.getBoolean(0, ret);
@Override public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) { if (!mThreadIndicatorEnabled) { return; } for (int i = 0; i < parent.getChildCount(); i++) { View child = parent.getChildAt(i); int level = parent.getChildViewHolder(child).getItemViewType(); for (int j = 0; j < level; j++) { int left = mHorizontalMargin + j * mLevelIndicatorWidth + mLevelIndicatorWidth / 2; if (mColorCodeEnabled) { mPaint.setColor(mColors.getColor(j % mColors.length(), 0)); mPaint.setAlpha(31); // 12% alpha } else { mPaint.setColor(Color.GRAY); mPaint.setAlpha(Math.max(0, 31 - 4 * j)); } c.drawLine(left, child.getTop(), left, child.getBottom(), mPaint); } } }
@Override public void run() { if (mTouching) { colorIndex++; if (colorIndex >= mCircles.length()) { colorIndex = 0; } float diameter = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, DIAMETER, getResources().getDisplayMetrics()); Drawable drawable = getResources().getDrawable(mCircles.getResourceId (colorIndex, -1)); createCircle(getActivity(), mRootView, mSpringSystem, mCoasting, mGravity, (int) diameter, drawable); mHandler.postDelayed(this, 100); } } }
this.images = new ArrayList<Integer>() {{ for (int i = 0; i < imgs.length(); i++) {add(imgs.getResourceId(i, -1));} }};
strokeColors = new int[ta.length()]; for(int j = 0; j < ta.length(); j++) strokeColors[j] = ta.getColor(j, 0); ta.recycle(); else if(attr == R.styleable.CircularProgressDrawable_cpd_inStepColors){ TypedArray ta = context.getResources().obtainTypedArray(a.getResourceId(attr, 0)); mInColors = new int[ta.length()]; for(int j = 0; j < ta.length(); j++) mInColors[j] = ta.getColor(j, 0); ta.recycle();
/** * Get Background color if the attr is color value * * @param context Context * @param attrs AttributeSet * @return Color */ public static int getBackgroundColor(Context context, AttributeSet attrs) { int color = Color.TRANSPARENT; if (isHaveAttribute(attrs, "background")) { int styleId = attrs.getStyleAttribute(); int[] attributesArray = new int[]{android.R.attr.background}; try { TypedArray typedArray = context.obtainStyledAttributes(styleId, attributesArray); if (typedArray.length() > 0) color = typedArray.getColor(0, color); typedArray.recycle(); } catch (Resources.NotFoundException e) { e.printStackTrace(); } } return color; }
if((resId = a.getResourceId(R.styleable.CircularProgressDrawable_cpd_strokeColors, 0)) != 0){ TypedArray ta = context.getResources().obtainTypedArray(resId); int[] colors = new int[ta.length()]; for(int j = 0; j < ta.length(); j++) colors[j] = ta.getColor(j, 0); ta.recycle(); if((resId = a.getResourceId(R.styleable.CircularProgressDrawable_cpd_inStepColors, 0)) != 0){ TypedArray ta = context.getResources().obtainTypedArray(resId); int[] colors = new int[ta.length()]; for(int j = 0; j < ta.length(); j++) colors[j] = ta.getColor(j, 0); ta.recycle();
@Override public void onBindViewHolder(ToggleItemViewHolder holder, int position) { if (holder.isFooter()) { return; } if (mLock != null && mLock[0] <= position && position <= mLock[1]) { clear(holder); return; } if (mColorCoded && mColors != null && mColors.length() > 0) { holder.mLevel.setVisibility(View.VISIBLE); holder.mLevel.setBackgroundColor(getThreadColor(getItemViewType(position))); holder.mLevel.setAlpha(mColorOpacity / 100f); } else { holder.mLevel.setVisibility(View.GONE); } super.onBindViewHolder(holder, position); }
@Test public void withIdReferenceEntry_obtainTypedArray() { TypedArray typedArray = resources.obtainTypedArray(R.array.typed_array_with_resource_id); assertThat(typedArray.length()).isEqualTo(2); assertThat(typedArray.getResourceId(0, 0)).isEqualTo(R.id.id_declared_in_item_tag); assertThat(typedArray.getResourceId(1, 0)).isEqualTo(R.id.id_declared_in_layout); }