Java Code Examples for android.content.res.Resources#obtainTypedArray()

The following examples show how to use android.content.res.Resources#obtainTypedArray() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SoundStore.java    From android-Soundboard with Apache License 2.0 6 votes vote down vote up
public static Sound[] getSounds(Context context) {
    Resources res = context.getApplicationContext().getResources();

    TypedArray labels = res.obtainTypedArray(R.array.labels);
    TypedArray ids = res.obtainTypedArray(R.array.ids);

    Sound[] sounds = new Sound[labels.length()];

    for (int i = 0; i < sounds.length; i++) {
        sounds[i] = new Sound(labels.getString(i), ids.getResourceId(i, -1));
    }

    labels.recycle();
    ids.recycle();

    return sounds;
}
 
Example 2
Source File: MaterialShadowContainerView.java    From android-materialshadowninepatch with Apache License 2.0 6 votes vote down vote up
private int[] getResourceIdArray(Resources resources, int id) {
    if (id == 0) {
        return null;
    }
    if (isInEditMode()) {
        return null;
    }

    TypedArray ta = resources.obtainTypedArray(id);
    int[] array = new int[ta.length()];

    for (int i = 0; i < array.length; i++) {
        array[i] = ta.getResourceId(i, 0);
    }

    ta.recycle();

    return array;
}
 
Example 3
Source File: LetterTileDrawable.java    From tindroid with Apache License 2.0 6 votes vote down vote up
public LetterTileDrawable(final Context context) {
    Resources res = context.getResources();
    if (sColorsLight == null) {
        sColorsLight = res.obtainTypedArray(R.array.letter_tile_colors_light);
        sColorsDark = res.obtainTypedArray(R.array.letter_tile_colors_dark);
        sDefaultColor = res.getColor(R.color.grey);
        sTileFontColorLight = res.getColor(R.color.letter_tile_text_color_light);
        sTileFontColorDark = res.getColor(R.color.letter_tile_text_color_dark);
        sLetterToTileRatio = 0.75f;
        DEFAULT_PERSON_AVATAR = getBitmapFromVectorDrawable(context, R.drawable.ic_person_white);
        DEFAULT_GROUP_AVATAR = getBitmapFromVectorDrawable(context, R.drawable.ic_group_white);
        sPaint.setTextAlign(Align.CENTER);
        sPaint.setAntiAlias(true);
    }
    mPaint = new Paint();
    mPaint.setFilterBitmap(true);
    mPaint.setDither(true);
    mColor = sDefaultColor;
}
 
Example 4
Source File: TypedArrayUtils.java    From libcommon with Apache License 2.0 6 votes vote down vote up
/**
 * xmlで定義したDimension arrayをfloat配列へ読み込む
 * @param res
 * @param arrayId
 * @param defaultValue
 * @return
 */
@NonNull
public static float[] readDimensionArray(@NonNull final Resources res,
	@ArrayRes final int arrayId, final float defaultValue) {

	float[] result;
	final TypedArray a = res.obtainTypedArray(arrayId);
	try {
		final int n = a.length();
		result = new float[n];
		for (int i = 0; i < n; i++) {
			result[i] = a.getDimension(i, defaultValue);
		}
	} finally {
		a.recycle();
	}
	return result;
}
 
Example 5
Source File: TypedArrayUtils.java    From libcommon with Apache License 2.0 6 votes vote down vote up
/**
 * xmlで定義したint arrayを読み込む
 * xmlで定義している値がintとして扱えないときはUnsupportedOperationExceptionを投げる
 * @param res
 * @param arrayId
 * @param defaultValue
 * @return
 * @throws UnsupportedOperationException
 */
@NonNull
public static int[] readArrayWithException(@NonNull final Resources res,
	@ArrayRes final int arrayId, final int defaultValue)
		throws UnsupportedOperationException {

	int[] result;
	final TypedArray a = res.obtainTypedArray(arrayId);
	try {
		final int n = a.length();
		result = new int[n];
		for (int i = 0; i < n; i++) {
			result[i] = a.getInteger(i, defaultValue);
		}
	} finally {
		a.recycle();
	}
	return result;
}
 
Example 6
Source File: TypedArrayUtils.java    From libcommon with Apache License 2.0 6 votes vote down vote up
/**
 * xmlで定義したstring arrayを読み込む
 * @param res
 * @param arrayId
 * @param defaultValue
 * @return
 */
@NonNull
public static CharSequence[] readArray(@NonNull final Resources res,
	@ArrayRes final int arrayId, final CharSequence defaultValue) {

	CharSequence[] result;
	final TypedArray a = res.obtainTypedArray(arrayId);
	try {
		final int n = a.length();
		result = new CharSequence[n];
		for (int i = 0; i < n; i++) {
			result[i] = a.getText(i);
			if (result[i] == null) {
				result[i] = defaultValue;
			}
		}
	} finally {
		a.recycle();
	}
	return result;
}
 
Example 7
Source File: TypedArrayUtils.java    From libcommon with Apache License 2.0 6 votes vote down vote up
/**
 * xmlで定義したstring arrayを読み込む
 * @param res
 * @param arrayId
 * @param defaultValue
 * @return
 */
@NonNull
public static String[] readArray(@NonNull final Resources res,
	@ArrayRes final int arrayId, final String defaultValue) {

	String[] result;
	final TypedArray a = res.obtainTypedArray(arrayId);
	try {
		final int n = a.length();
		result = new String[n];
		for (int i = 0; i < n; i++) {
			result[i] = a.getString(i);
			if (result[i] == null) {
				result[i] = defaultValue;
			}
		}
	} finally {
		a.recycle();
	}
	return result;
}
 
Example 8
Source File: TypedArrayUtils.java    From libcommon with Apache License 2.0 6 votes vote down vote up
/**
 * xmlで定義したboolean arrayを読み込む
 * @param res
 * @param arrayId
 * @param defaultValue
 * @return
 */
@NonNull
public static boolean[] readArray(@NonNull final Resources res,
	@ArrayRes final int arrayId, final boolean defaultValue) {

	boolean[] result;
	final TypedArray a = res.obtainTypedArray(arrayId);
	try {
		final int n = a.length();
		result = new boolean[n];
		for (int i = 0; i < n; i++) {
			result[i] = a.getBoolean(i, defaultValue);
		}
	} finally {
		a.recycle();
	}
	return result;
}
 
Example 9
Source File: LetterTileProvider.java    From MaterialChipsInput with Apache License 2.0 6 votes vote down vote up
/**
 * Constructor for <code>LetterTileProvider</code>
 *
 * @param context The {@link Context} to use
 */
public LetterTileProvider(Context context) {
    final Resources res = context.getResources();

    mPaint.setTypeface(Typeface.create("sans-serif-light", Typeface.NORMAL));
    mPaint.setColor(Color.WHITE);
    mPaint.setTextAlign(Paint.Align.CENTER);
    mPaint.setAntiAlias(true);

    mColors = res.obtainTypedArray(R.array.letter_tile_colors);
    mTileLetterFontSize = res.getDimensionPixelSize(R.dimen.tile_letter_font_size);

    //mDefaultBitmap = BitmapFactory.decodeResource(res, android.R.drawable.);
    mDefaultBitmap = drawableToBitmap(ContextCompat.getDrawable(context, R.drawable.ic_person_white_24dp));
    mWidth = res.getDimensionPixelSize(R.dimen.letter_tile_size);
    mHeight = res.getDimensionPixelSize(R.dimen.letter_tile_size);
}
 
Example 10
Source File: Ui.java    From Genius-Android with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 11
Source File: MoverSource.java    From Mover with Apache License 2.0 6 votes vote down vote up
@Override
public Drawable[] getNavigationIcons() {
    if(mNavigationDrawables == null){
        Resources resources = ResourceUtils.getResources();
        TypedArray array = resources.obtainTypedArray(R.array.mover_categories_icons);
        final int length = array.length();

        Drawable[] drawables = new Drawable[length];
        for(int index = 0; index < length; index++){
            drawables[index] = MrVector.inflate(resources, array.getResourceId(index, -1));
        }
        array.recycle();
        mNavigationDrawables = drawables;
    }

    return mNavigationDrawables;
}
 
Example 12
Source File: OStringColorUtil.java    From framework with GNU Affero General Public License v3.0 5 votes vote down vote up
public static int getStringColor(Context context, String content) {
    Resources res = context.getResources();
    TypedArray mColors = res.obtainTypedArray(R.array.letter_tile_colors);
    int MAX_COLORS = mColors.length();
    int firstCharAsc = content.toUpperCase(Locale.getDefault()).charAt(0);
    int index = (firstCharAsc % MAX_COLORS);
    if (index > MAX_COLORS - 1) {
        index = index / 2;
    }
    int color = mColors.getColor(index, Color.WHITE);
    mColors.recycle();
    return color;
}
 
Example 13
Source File: CardContentFragment.java    From android-design-library with Apache License 2.0 5 votes vote down vote up
public ContentAdapter(Context context) {
    Resources resources = context.getResources();
    mPlaces = resources.getStringArray(R.array.places);
    mPlaceDesc = resources.getStringArray(R.array.place_desc);
    TypedArray a = resources.obtainTypedArray(R.array.places_picture);
    mPlacePictures = new Drawable[a.length()];
    for (int i = 0; i < mPlacePictures.length; i++) {
        mPlacePictures[i] = a.getDrawable(i);
    }
    a.recycle();
}
 
Example 14
Source File: ListContentFragment.java    From android-design-library with Apache License 2.0 5 votes vote down vote up
public ContentAdapter(Context context) {
    Resources resources = context.getResources();
    mPlaces = resources.getStringArray(R.array.places);
    mPlaceDesc = resources.getStringArray(R.array.place_desc);
    TypedArray a = resources.obtainTypedArray(R.array.place_avator);
    mPlaceAvators = new Drawable[a.length()];
    for (int i = 0; i < mPlaceAvators.length; i++) {
        mPlaceAvators[i] = a.getDrawable(i);
    }
    a.recycle();
}
 
Example 15
Source File: OStringColorUtil.java    From hr with GNU Affero General Public License v3.0 5 votes vote down vote up
public static int getStringColor(Context context, String content) {
    Resources res = context.getResources();
    TypedArray mColors = res.obtainTypedArray(R.array.letter_tile_colors);
    int MAX_COLORS = mColors.length();
    int firstCharAsc = content.toUpperCase(Locale.getDefault()).charAt(0);
    int index = (firstCharAsc % MAX_COLORS);
    if (index > MAX_COLORS - 1) {
        index = index / 2;
    }
    int color = mColors.getColor(index, Color.WHITE);
    mColors.recycle();
    return color;
}
 
Example 16
Source File: Building.java    From MultiChoiceAdapter with Apache License 2.0 5 votes vote down vote up
public static ArrayList<Building> createList(Context ctx) {
    Resources res = ctx.getResources();
    String[] names = res.getStringArray(R.array.names);
    String[] heights = res.getStringArray(R.array.heights);
    TypedArray icons = res.obtainTypedArray(R.array.photos);
    ArrayList<Building> items = new ArrayList<Building>(names.length);
    for (int i = 0; i < names.length; ++i) {
        items.add(new Building(names[i], heights[i], icons.getDrawable(i)));
    }
    icons.recycle();
    return items;
}
 
Example 17
Source File: ListContentFragment.java    From android-design-library with Apache License 2.0 5 votes vote down vote up
public ContentAdapter(Context context) {
    Resources resources = context.getResources();
    mPlaces = resources.getStringArray(R.array.places);
    mPlaceDesc = resources.getStringArray(R.array.place_desc);
    TypedArray a = resources.obtainTypedArray(R.array.place_avator);
    mPlaceAvators = new Drawable[a.length()];
    for (int i = 0; i < mPlaceAvators.length; i++) {
        mPlaceAvators[i] = a.getDrawable(i);
    }
    a.recycle();
}
 
Example 18
Source File: CardContentFragment.java    From android-design-library with Apache License 2.0 5 votes vote down vote up
public ContentAdapter(Context context) {
    Resources resources = context.getResources();
    mPlaces = resources.getStringArray(R.array.places);
    mPlaceDesc = resources.getStringArray(R.array.place_desc);
    TypedArray a = resources.obtainTypedArray(R.array.places_picture);
    mPlacePictures = new Drawable[a.length()];
    for (int i = 0; i < mPlacePictures.length; i++) {
        mPlacePictures[i] = a.getDrawable(i);
    }
    a.recycle();
}
 
Example 19
Source File: Loading.java    From Genius-Android with Apache License 2.0 4 votes vote down vote up
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);
    }
}
 
Example 20
Source File: BatteryMeterView.java    From AcDisplay with GNU General Public License v2.0 4 votes vote down vote up
public BatteryMeterView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    mContext = context;

    TypedArray arr = context.obtainStyledAttributes(attrs, R.styleable.BatteryMeterView, defStyle, 0);
    mBatteryHeight = arr.getDimensionPixelSize(R.styleable.BatteryMeterView_battery_height, 0);
    mBatteryWidth = arr.getDimensionPixelSize(R.styleable.BatteryMeterView_battery_width, 0);
    mBatteryPadding = arr.getDimensionPixelSize(R.styleable.BatteryMeterView_battery_padding, 0);
    setPadding(getPaddingLeft(), getPaddingTop(), getPaddingRight(), getPaddingBottom());
    arr.recycle();

    final Resources res = context.getResources();
    if (!isInEditMode()) {
        TypedArray levels = res.obtainTypedArray(R.array.batterymeter_color_levels);
        TypedArray colors = res.obtainTypedArray(R.array.batterymeter_color_values);

        final int n = levels.length();
        mColors = new int[2 * n];
        for (int i = 0; i < n; i++) {
            mColors[2 * i] = levels.getInt(i, 0);
            mColors[2 * i + 1] = colors.getColor(i, 0);
        }
        levels.recycle();
        colors.recycle();
    } else {
        mColors = new int[]{
                4, res.getColor(R.color.batterymeter_critical),
                15, res.getColor(R.color.batterymeter_low),
                100, res.getColor(R.color.batterymeter_full),
        };
    }

    mChargeColor = getResources().getColor(R.color.batterymeter_charge_color);
    mBatteryFormat = getResources().getString(R.string.batterymeter_precise);
    mWarningString = context.getString(R.string.batterymeter_very_low_overlay_symbol);

    setMode(BatteryMeterMode.BATTERY_METER_ICON_PORTRAIT);
    mBatteryDrawable.onSizeChanged(mBatteryWidth, mBatteryHeight, 0, 0);

    setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}