Example usage for android.content.res TypedArray getFloat

List of usage examples for android.content.res TypedArray getFloat

Introduction

In this page you can find the example usage for android.content.res TypedArray getFloat.

Prototype

public float getFloat(@StyleableRes int index, float defValue) 

Source Link

Document

Retrieve the float value for the attribute at index.

Usage

From source file:Main.java

@SuppressWarnings("ConstantConditions")
public static float resolveFloat(Context context, int attr) {
    TypedArray a = context.obtainStyledAttributes(null, new int[] { attr });
    try {/* ww  w  . j av a 2s .  c  om*/
        return a.getFloat(0, 0);
    } finally {
        a.recycle();
    }
}

From source file:com.bilibili.magicasakura.utils.ColorStateListUtils.java

static ColorStateList inflateColorStateList(Context context, XmlPullParser parser, AttributeSet attrs)
        throws IOException, XmlPullParserException {
    final int innerDepth = parser.getDepth() + 1;
    int depth;//from  w w w  .j a va 2s.c om
    int type;

    LinkedList<int[]> stateList = new LinkedList<>();
    LinkedList<Integer> colorList = new LinkedList<>();

    while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
            && ((depth = parser.getDepth()) >= innerDepth || type != XmlPullParser.END_TAG)) {
        if (type != XmlPullParser.START_TAG || depth > innerDepth || !parser.getName().equals("item")) {
            continue;
        }

        TypedArray a1 = context.obtainStyledAttributes(attrs, new int[] { android.R.attr.color });
        final int value = a1.getResourceId(0, Color.MAGENTA);
        final int baseColor = value == Color.MAGENTA ? Color.MAGENTA
                : ThemeUtils.replaceColorById(context, value);
        a1.recycle();
        TypedArray a2 = context.obtainStyledAttributes(attrs, new int[] { android.R.attr.alpha });
        final float alphaMod = a2.getFloat(0, 1.0f);
        a2.recycle();
        colorList.add(alphaMod != 1.0f
                ? ColorUtils.setAlphaComponent(baseColor, Math.round(Color.alpha(baseColor) * alphaMod))
                : baseColor);

        stateList.add(extractStateSet(attrs));
    }

    if (stateList.size() > 0 && stateList.size() == colorList.size()) {
        int[] colors = new int[colorList.size()];
        for (int i = 0; i < colorList.size(); i++) {
            colors[i] = colorList.get(i);
        }
        return new ColorStateList(stateList.toArray(new int[stateList.size()][]), colors);
    }
    return null;
}

From source file:org.gearvrf.controls.model.Apple.java

public static float[] getColor(GVRContext gvrContext) {

    Resources res = gvrContext.getContext().getResources();
    TypedArray colorArray = res.obtainTypedArray(R.array.colors);
    TypedArray colorTypeValues;
    float[] appleColor = new float[3];
    colorTypeValues = res.obtainTypedArray(colorArray.getResourceId(Apple.currentMotion, 0));

    appleColor[0] = colorTypeValues.getFloat(0, 0);
    appleColor[1] = colorTypeValues.getFloat(1, 0);
    appleColor[2] = colorTypeValues.getFloat(2, 0);

    return Util.normalizeColor(appleColor);

}

From source file:android.support.v7.content.res.AppCompatColorStateListInflater.java

/**
 * Fill in this object based on the contents of an XML "selector" element.
 *//*from  ww  w .  ja  v a  2s . c o m*/
private static ColorStateList inflate(@NonNull Resources r, @NonNull XmlPullParser parser,
        @NonNull AttributeSet attrs, @Nullable Resources.Theme theme)
        throws XmlPullParserException, IOException {
    final int innerDepth = parser.getDepth() + 1;
    int depth;
    int type;
    int defaultColor = DEFAULT_COLOR;

    int[][] stateSpecList = new int[20][];
    int[] colorList = new int[stateSpecList.length];
    int listSize = 0;

    while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
            && ((depth = parser.getDepth()) >= innerDepth || type != XmlPullParser.END_TAG)) {
        if (type != XmlPullParser.START_TAG || depth > innerDepth || !parser.getName().equals("item")) {
            continue;
        }

        final TypedArray a = obtainAttributes(r, theme, attrs, R.styleable.ColorStateListItem);
        final int baseColor = a.getColor(R.styleable.ColorStateListItem_android_color, Color.MAGENTA);

        float alphaMod = 1.0f;
        if (a.hasValue(R.styleable.ColorStateListItem_android_alpha)) {
            alphaMod = a.getFloat(R.styleable.ColorStateListItem_android_alpha, alphaMod);
        } else if (a.hasValue(R.styleable.ColorStateListItem_alpha)) {
            alphaMod = a.getFloat(R.styleable.ColorStateListItem_alpha, alphaMod);
        }

        a.recycle();

        // Parse all unrecognized attributes as state specifiers.
        int j = 0;
        final int numAttrs = attrs.getAttributeCount();
        int[] stateSpec = new int[numAttrs];
        for (int i = 0; i < numAttrs; i++) {
            final int stateResId = attrs.getAttributeNameResource(i);
            if (stateResId != android.R.attr.color && stateResId != android.R.attr.alpha
                    && stateResId != R.attr.alpha) {
                // Unrecognized attribute, add to state set
                stateSpec[j++] = attrs.getAttributeBooleanValue(i, false) ? stateResId : -stateResId;
            }
        }
        stateSpec = StateSet.trimStateSet(stateSpec, j);

        // Apply alpha modulation. If we couldn't resolve the color or
        // alpha yet, the default values leave us enough information to
        // modulate again during applyTheme().
        final int color = modulateColorAlpha(baseColor, alphaMod);
        if (listSize == 0 || stateSpec.length == 0) {
            defaultColor = color;
        }

        colorList = GrowingArrayUtils.append(colorList, listSize, color);
        stateSpecList = GrowingArrayUtils.append(stateSpecList, listSize, stateSpec);
        listSize++;
    }

    int[] colors = new int[listSize];
    int[][] stateSpecs = new int[listSize][];
    System.arraycopy(colorList, 0, colors, 0, listSize);
    System.arraycopy(stateSpecList, 0, stateSpecs, 0, listSize);

    return new ColorStateList(stateSpecs, colors);
}

From source file:de.mrapp.android.util.ThemeUtil.java

/**
 * Obtains the float value, which corresponds to a specific resource id, from a specific theme.
 *
 * @param context/*w  ww  . j  a v a  2  s  .  c  o m*/
 *         The context, which should be used, as an instance of the class {@link Context}. The
 *         context may not be null
 * @param themeResourceId
 *         The resource id of the theme, the attribute should be obtained from, as an {@link
 *         Integer} value or -1, if the attribute should be obtained from the given context's
 *         theme
 * @param resourceId
 *         The resource id of the attribute, which should be obtained, as an {@link Integer}
 *         value. The resource id must corresponds to a valid theme attribute
 * @return The float value, which has been obtained, as a {@link Float} value or 0, if the given
 * resource id is invalid
 */
public static float getFloat(@NonNull final Context context, @StyleRes final int themeResourceId,
        @AttrRes final int resourceId) {
    TypedArray typedArray = null;

    try {
        typedArray = obtainStyledAttributes(context, themeResourceId, resourceId);
        return typedArray.getFloat(0, 0);
    } finally {
        if (typedArray != null) {
            typedArray.recycle();
        }
    }
}

From source file:com.skydoves.elasticviewsexample.ElasticVIews.ElasticFloatingActionButton.java

private void setTypeArray(TypedArray typedArray) {
    scale = typedArray.getFloat(R.styleable.ElasticFloatingActionButton_fabutton_scale, scale);
    duration = typedArray.getInt(R.styleable.ElasticFloatingActionButton_fabutton_duration, duration);
}

From source file:com.skydoves.elasticviewsexample.ElasticVIews.ElasticImageView.java

private void setTypeArray(TypedArray typedArray) {
    scale = typedArray.getFloat(R.styleable.ElasticImageView_imageview_scale, scale);
    duration = typedArray.getInt(R.styleable.ElasticImageView_imageview_duration, duration);
}

From source file:nz.org.winters.android.custompreference.FloatPreference.java

@Override
protected Object onGetDefaultValue(TypedArray a, int index) {
    return a.getFloat(index, (float) 0.0);
}

From source file:com.capricorn.ArcMenu.java

private void applyAttrs(AttributeSet attrs) {
    if (attrs != null) {
        TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.ArcLayout, 0, 0);

        float fromDegrees = a.getFloat(R.styleable.ArcLayout_fromDegrees, ArcLayout.DEFAULT_FROM_DEGREES);
        float toDegrees = a.getFloat(R.styleable.ArcLayout_toDegrees, ArcLayout.DEFAULT_TO_DEGREES);
        mArcLayout.setArc(fromDegrees, toDegrees);

        int defaultChildSize = mArcLayout.getChildSize();
        int newChildSize = a.getDimensionPixelSize(R.styleable.ArcLayout_childSize, defaultChildSize);
        mArcLayout.setChildSize(newChildSize);

        a.recycle();//  ww w  . ja  v a 2s  .com
    }
}

From source file:com.ez.gallery.ucrop.view.widget.AspectRatioTextView.java

@SuppressWarnings("deprecation")
private void init(@NonNull TypedArray a) {
    setGravity(Gravity.CENTER_HORIZONTAL);

    mAspectRatioTitle = a.getString(R.styleable.ucrop_AspectRatioTextView_ucrop_artv_ratio_title);
    mAspectRatioX = a.getFloat(R.styleable.ucrop_AspectRatioTextView_ucrop_artv_ratio_x,
            CropImageView.SOURCE_IMAGE_ASPECT_RATIO);
    mAspectRatioY = a.getFloat(R.styleable.ucrop_AspectRatioTextView_ucrop_artv_ratio_y,
            CropImageView.SOURCE_IMAGE_ASPECT_RATIO);

    if (mAspectRatioX == CropImageView.SOURCE_IMAGE_ASPECT_RATIO
            || mAspectRatioY == CropImageView.SOURCE_IMAGE_ASPECT_RATIO) {
        mAspectRatio = CropImageView.SOURCE_IMAGE_ASPECT_RATIO;
    } else {/*ww  w  . j a  v a2  s  .c o m*/
        mAspectRatio = mAspectRatioX / mAspectRatioY;
    }

    mDotSize = getContext().getResources().getDimensionPixelSize(R.dimen.ucrop_size_dot_scale_text_view);
    mDotPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    mDotPaint.setStyle(Paint.Style.FILL);

    setTitle();

    int activeColor = getResources().getColor(R.color.ucrop_color_widget_active);
    applyActiveColor(activeColor);
}