Search in sources :

Example 41 with TypedValue

use of android.util.TypedValue in project XobotOS by xamarin.

the class BitmapFactory method decodeResource.

/**
     * Synonym for opening the given resource and calling
     * {@link #decodeResourceStream}.
     *
     * @param res   The resources object containing the image data
     * @param id The resource id of the image data
     * @param opts null-ok; Options that control downsampling and whether the
     *             image should be completely decoded, or just is size returned.
     * @return The decoded bitmap, or null if the image data could not be
     *         decoded, or, if opts is non-null, if opts requested only the
     *         size be returned (in opts.outWidth and opts.outHeight)
     */
public static Bitmap decodeResource(Resources res, int id, Options opts) {
    Bitmap bm = null;
    InputStream is = null;
    try {
        final TypedValue value = new TypedValue();
        is = res.openRawResource(id, value);
        bm = decodeResourceStream(res, value, is, null, opts);
    } catch (Exception e) {
    /*  do nothing.
                If the exception happened on open, bm will be null.
                If it happened on close, bm is still valid.
            */
    } finally {
        try {
            if (is != null)
                is.close();
        } catch (IOException e) {
        // Ignore
        }
    }
    if (bm == null && opts != null && opts.inBitmap != null) {
        throw new IllegalArgumentException("Problem decoding into existing bitmap");
    }
    return bm;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) IOException(java.io.IOException) TypedValue(android.util.TypedValue)

Example 42 with TypedValue

use of android.util.TypedValue in project XobotOS by xamarin.

the class RotateDrawable method inflate.

@Override
public void inflate(Resources r, XmlPullParser parser, AttributeSet attrs) throws XmlPullParserException, IOException {
    TypedArray a = r.obtainAttributes(attrs, com.android.internal.R.styleable.RotateDrawable);
    super.inflateWithAttributes(r, parser, a, com.android.internal.R.styleable.RotateDrawable_visible);
    TypedValue tv = a.peekValue(com.android.internal.R.styleable.RotateDrawable_pivotX);
    boolean pivotXRel;
    float pivotX;
    if (tv == null) {
        pivotXRel = true;
        pivotX = 0.5f;
    } else {
        pivotXRel = tv.type == TypedValue.TYPE_FRACTION;
        pivotX = pivotXRel ? tv.getFraction(1.0f, 1.0f) : tv.getFloat();
    }
    tv = a.peekValue(com.android.internal.R.styleable.RotateDrawable_pivotY);
    boolean pivotYRel;
    float pivotY;
    if (tv == null) {
        pivotYRel = true;
        pivotY = 0.5f;
    } else {
        pivotYRel = tv.type == TypedValue.TYPE_FRACTION;
        pivotY = pivotYRel ? tv.getFraction(1.0f, 1.0f) : tv.getFloat();
    }
    float fromDegrees = a.getFloat(com.android.internal.R.styleable.RotateDrawable_fromDegrees, 0.0f);
    float toDegrees = a.getFloat(com.android.internal.R.styleable.RotateDrawable_toDegrees, 360.0f);
    int res = a.getResourceId(com.android.internal.R.styleable.RotateDrawable_drawable, 0);
    Drawable drawable = null;
    if (res > 0) {
        drawable = r.getDrawable(res);
    }
    a.recycle();
    int outerDepth = parser.getDepth();
    int type;
    while ((type = parser.next()) != XmlPullParser.END_DOCUMENT && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
        if (type != XmlPullParser.START_TAG) {
            continue;
        }
        if ((drawable = Drawable.createFromXmlInner(r, parser, attrs)) == null) {
            Log.w("drawable", "Bad element under <rotate>: " + parser.getName());
        }
    }
    if (drawable == null) {
        Log.w("drawable", "No drawable specified for <rotate>");
    }
    mState.mDrawable = drawable;
    mState.mPivotXRel = pivotXRel;
    mState.mPivotX = pivotX;
    mState.mPivotYRel = pivotYRel;
    mState.mPivotY = pivotY;
    mState.mFromDegrees = mState.mCurrentDegrees = fromDegrees;
    mState.mToDegrees = toDegrees;
    if (drawable != null) {
        drawable.setCallback(this);
    }
}
Also used : TypedArray(android.content.res.TypedArray) TypedValue(android.util.TypedValue)

Example 43 with TypedValue

use of android.util.TypedValue in project XobotOS by xamarin.

the class Resources method getDimensionPixelSize.

/**
     * Retrieve a dimensional for a particular resource ID for use
     * as a size in raw pixels.  This is the same as
     * {@link #getDimension}, except the returned value is converted to
     * integer pixels for use as a size.  A size conversion involves
     * rounding the base value, and ensuring that a non-zero base value
     * is at least one pixel in size.
     * 
     * @param id The desired resource identifier, as generated by the aapt
     *           tool. This integer encodes the package, type, and resource
     *           entry. The value 0 is an invalid identifier.
     * 
     * @return Resource dimension value multiplied by the appropriate 
     * metric and truncated to integer pixels.
     *  
     * @throws NotFoundException Throws NotFoundException if the given ID does not exist.
     *
     * @see #getDimension
     * @see #getDimensionPixelOffset
     */
public int getDimensionPixelSize(int id) throws NotFoundException {
    synchronized (mTmpValue) {
        TypedValue value = mTmpValue;
        getValue(id, value, true);
        if (value.type == TypedValue.TYPE_DIMENSION) {
            return TypedValue.complexToDimensionPixelSize(value.data, mMetrics);
        }
        throw new NotFoundException("Resource ID #0x" + Integer.toHexString(id) + " type #0x" + Integer.toHexString(value.type) + " is not valid");
    }
}
Also used : TypedValue(android.util.TypedValue)

Example 44 with TypedValue

use of android.util.TypedValue in project XobotOS by xamarin.

the class Resources method parseBundleExtra.

/**
     * Parse a name/value pair out of an XML tag holding that data.  The
     * AttributeSet must be holding the data defined by
     * {@link android.R.styleable#Extra}.  The following value types are supported:
     * <ul>
     * <li> {@link TypedValue#TYPE_STRING}:
     * {@link Bundle#putCharSequence Bundle.putCharSequence()}
     * <li> {@link TypedValue#TYPE_INT_BOOLEAN}:
     * {@link Bundle#putCharSequence Bundle.putBoolean()}
     * <li> {@link TypedValue#TYPE_FIRST_INT}-{@link TypedValue#TYPE_LAST_INT}:
     * {@link Bundle#putCharSequence Bundle.putBoolean()}
     * <li> {@link TypedValue#TYPE_FLOAT}:
     * {@link Bundle#putCharSequence Bundle.putFloat()}
     * </ul>
     * 
     * @param tagName The name of the tag these attributes come from; this is
     * only used for reporting error messages.
     * @param attrs The attributes from which to retrieve the name/value pair.
     * @param outBundle The Bundle in which to place the parsed value.
     * @throws XmlPullParserException If the attributes are not valid.
     */
public void parseBundleExtra(String tagName, AttributeSet attrs, Bundle outBundle) throws XmlPullParserException {
    TypedArray sa = obtainAttributes(attrs, com.android.internal.R.styleable.Extra);
    String name = sa.getString(com.android.internal.R.styleable.Extra_name);
    if (name == null) {
        sa.recycle();
        throw new XmlPullParserException("<" + tagName + "> requires an android:name attribute at " + attrs.getPositionDescription());
    }
    TypedValue v = sa.peekValue(com.android.internal.R.styleable.Extra_value);
    if (v != null) {
        if (v.type == TypedValue.TYPE_STRING) {
            CharSequence cs = v.coerceToString();
            outBundle.putCharSequence(name, cs);
        } else if (v.type == TypedValue.TYPE_INT_BOOLEAN) {
            outBundle.putBoolean(name, v.data != 0);
        } else if (v.type >= TypedValue.TYPE_FIRST_INT && v.type <= TypedValue.TYPE_LAST_INT) {
            outBundle.putInt(name, v.data);
        } else if (v.type == TypedValue.TYPE_FLOAT) {
            outBundle.putFloat(name, v.getFloat());
        } else {
            sa.recycle();
            throw new XmlPullParserException("<" + tagName + "> only supports string, integer, float, color, and boolean at " + attrs.getPositionDescription());
        }
    } else {
        sa.recycle();
        throw new XmlPullParserException("<" + tagName + "> requires an android:value or android:resource attribute at " + attrs.getPositionDescription());
    }
    sa.recycle();
}
Also used : XmlPullParserException(org.xmlpull.v1.XmlPullParserException) TypedValue(android.util.TypedValue)

Example 45 with TypedValue

use of android.util.TypedValue in project XobotOS by xamarin.

the class Resources method getBoolean.

/**
     * Return a boolean associated with a particular resource ID.  This can be
     * used with any integral resource value, and will return true if it is
     * non-zero.
     *
     * @param id The desired resource identifier, as generated by the aapt
     *           tool. This integer encodes the package, type, and resource
     *           entry. The value 0 is an invalid identifier.
     *
     * @throws NotFoundException Throws NotFoundException if the given ID does not exist.
     *
     * @return Returns the boolean value contained in the resource.
     */
public boolean getBoolean(int id) throws NotFoundException {
    synchronized (mTmpValue) {
        TypedValue value = mTmpValue;
        getValue(id, value, true);
        if (value.type >= TypedValue.TYPE_FIRST_INT && value.type <= TypedValue.TYPE_LAST_INT) {
            return value.data != 0;
        }
        throw new NotFoundException("Resource ID #0x" + Integer.toHexString(id) + " type #0x" + Integer.toHexString(value.type) + " is not valid");
    }
}
Also used : TypedValue(android.util.TypedValue)

Aggregations

TypedValue (android.util.TypedValue)844 TypedArray (android.content.res.TypedArray)190 Resources (android.content.res.Resources)92 ContextThemeWrapper (android.view.ContextThemeWrapper)52 Context (android.content.Context)50 Drawable (android.graphics.drawable.Drawable)49 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)46 TextView (android.widget.TextView)44 Paint (android.graphics.Paint)41 IOException (java.io.IOException)39 AttributeSet (android.util.AttributeSet)34 View (android.view.View)30 XmlResourceParser (android.content.res.XmlResourceParser)29 Point (android.graphics.Point)26 ResourceValue (com.android.ide.common.rendering.api.ResourceValue)26 ColorStateList (android.content.res.ColorStateList)23 DisplayMetrics (android.util.DisplayMetrics)23 LinearLayout (android.widget.LinearLayout)20 Bundle (android.os.Bundle)19 SpannableString (android.text.SpannableString)19