Search in sources :

Example 1 with NinePatch

use of android.graphics.NinePatch in project android_frameworks_base by ParanoidAndroid.

the class NinePatchDrawable method inflate.

@Override
public void inflate(Resources r, XmlPullParser parser, AttributeSet attrs) throws XmlPullParserException, IOException {
    super.inflate(r, parser, attrs);
    TypedArray a = r.obtainAttributes(attrs, com.android.internal.R.styleable.NinePatchDrawable);
    final int id = a.getResourceId(com.android.internal.R.styleable.NinePatchDrawable_src, 0);
    if (id == 0) {
        throw new XmlPullParserException(parser.getPositionDescription() + ": <nine-patch> requires a valid src attribute");
    }
    final boolean dither = a.getBoolean(com.android.internal.R.styleable.NinePatchDrawable_dither, DEFAULT_DITHER);
    final BitmapFactory.Options options = new BitmapFactory.Options();
    if (dither) {
        options.inDither = false;
    }
    options.inScreenDensity = r.getDisplayMetrics().noncompatDensityDpi;
    final Rect padding = new Rect();
    final Rect opticalInsets = new Rect();
    Bitmap bitmap = null;
    try {
        final TypedValue value = new TypedValue();
        final InputStream is = r.openRawResource(id, value);
        bitmap = BitmapFactory.decodeResourceStream(r, value, is, padding, options);
        is.close();
    } catch (IOException e) {
    // Ignore
    }
    if (bitmap == null) {
        throw new XmlPullParserException(parser.getPositionDescription() + ": <nine-patch> requires a valid src attribute");
    } else if (bitmap.getNinePatchChunk() == null) {
        throw new XmlPullParserException(parser.getPositionDescription() + ": <nine-patch> requires a valid 9-patch source image");
    }
    setNinePatchState(new NinePatchState(new NinePatch(bitmap, bitmap.getNinePatchChunk(), "XML 9-patch"), padding, opticalInsets, dither), r);
    mNinePatchState.mTargetDensity = mTargetDensity;
    a.recycle();
}
Also used : Rect(android.graphics.Rect) InputStream(java.io.InputStream) NinePatch(android.graphics.NinePatch) IOException(java.io.IOException) Paint(android.graphics.Paint) Bitmap(android.graphics.Bitmap) TypedArray(android.content.res.TypedArray) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) BitmapFactory(android.graphics.BitmapFactory) TypedValue(android.util.TypedValue)

Example 2 with NinePatch

use of android.graphics.NinePatch in project platform_frameworks_base by android.

the class NinePatchDrawable method computeBitmapSize.

private void computeBitmapSize() {
    final NinePatch ninePatch = mNinePatchState.mNinePatch;
    if (ninePatch == null) {
        return;
    }
    final int sourceDensity = ninePatch.getDensity();
    final int targetDensity = mTargetDensity;
    final Insets sourceOpticalInsets = mNinePatchState.mOpticalInsets;
    if (sourceOpticalInsets != Insets.NONE) {
        final int left = Drawable.scaleFromDensity(sourceOpticalInsets.left, sourceDensity, targetDensity, true);
        final int top = Drawable.scaleFromDensity(sourceOpticalInsets.top, sourceDensity, targetDensity, true);
        final int right = Drawable.scaleFromDensity(sourceOpticalInsets.right, sourceDensity, targetDensity, true);
        final int bottom = Drawable.scaleFromDensity(sourceOpticalInsets.bottom, sourceDensity, targetDensity, true);
        mOpticalInsets = Insets.of(left, top, right, bottom);
    } else {
        mOpticalInsets = Insets.NONE;
    }
    final Rect sourcePadding = mNinePatchState.mPadding;
    if (sourcePadding != null) {
        if (mPadding == null) {
            mPadding = new Rect();
        }
        mPadding.left = Drawable.scaleFromDensity(sourcePadding.left, sourceDensity, targetDensity, false);
        mPadding.top = Drawable.scaleFromDensity(sourcePadding.top, sourceDensity, targetDensity, false);
        mPadding.right = Drawable.scaleFromDensity(sourcePadding.right, sourceDensity, targetDensity, false);
        mPadding.bottom = Drawable.scaleFromDensity(sourcePadding.bottom, sourceDensity, targetDensity, false);
    } else {
        mPadding = null;
    }
    mBitmapHeight = Drawable.scaleFromDensity(ninePatch.getHeight(), sourceDensity, targetDensity, true);
    mBitmapWidth = Drawable.scaleFromDensity(ninePatch.getWidth(), sourceDensity, targetDensity, true);
    final NinePatch.InsetStruct insets = ninePatch.getBitmap().getNinePatchInsets();
    if (insets != null) {
        Rect outlineRect = insets.outlineRect;
        mOutlineInsets = NinePatch.InsetStruct.scaleInsets(outlineRect.left, outlineRect.top, outlineRect.right, outlineRect.bottom, targetDensity / (float) sourceDensity);
        mOutlineRadius = Drawable.scaleFromDensity(insets.outlineRadius, sourceDensity, targetDensity);
    } else {
        mOutlineInsets = null;
    }
}
Also used : Rect(android.graphics.Rect) Insets(android.graphics.Insets) NinePatch(android.graphics.NinePatch) Paint(android.graphics.Paint)

Example 3 with NinePatch

use of android.graphics.NinePatch in project android_frameworks_base by ResurrectionRemix.

the class NinePatchDrawable method updateStateFromTypedArray.

/**
     * Updates the constant state from the values in the typed array.
     */
private void updateStateFromTypedArray(@NonNull TypedArray a) throws XmlPullParserException {
    final Resources r = a.getResources();
    final NinePatchState state = mNinePatchState;
    // Account for any configuration changes.
    state.mChangingConfigurations |= a.getChangingConfigurations();
    // Extract the theme attributes, if any.
    state.mThemeAttrs = a.extractThemeAttrs();
    state.mDither = a.getBoolean(R.styleable.NinePatchDrawable_dither, state.mDither);
    final int srcResId = a.getResourceId(R.styleable.NinePatchDrawable_src, 0);
    if (srcResId != 0) {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inDither = !state.mDither;
        options.inScreenDensity = r.getDisplayMetrics().noncompatDensityDpi;
        final Rect padding = new Rect();
        final Rect opticalInsets = new Rect();
        Bitmap bitmap = null;
        try {
            final TypedValue value = new TypedValue();
            final InputStream is = r.openRawResource(srcResId, value);
            bitmap = BitmapFactory.decodeResourceStream(r, value, is, padding, options);
            is.close();
        } catch (IOException e) {
        // Ignore
        }
        if (bitmap == null) {
            throw new XmlPullParserException(a.getPositionDescription() + ": <nine-patch> requires a valid src attribute");
        } else if (bitmap.getNinePatchChunk() == null) {
            throw new XmlPullParserException(a.getPositionDescription() + ": <nine-patch> requires a valid 9-patch source image");
        }
        bitmap.getOpticalInsets(opticalInsets);
        state.mNinePatch = new NinePatch(bitmap, bitmap.getNinePatchChunk());
        state.mPadding = padding;
        state.mOpticalInsets = Insets.of(opticalInsets);
    }
    state.mAutoMirrored = a.getBoolean(R.styleable.NinePatchDrawable_autoMirrored, state.mAutoMirrored);
    state.mBaseAlpha = a.getFloat(R.styleable.NinePatchDrawable_alpha, state.mBaseAlpha);
    final int tintMode = a.getInt(R.styleable.NinePatchDrawable_tintMode, -1);
    if (tintMode != -1) {
        state.mTintMode = Drawable.parseTintMode(tintMode, Mode.SRC_IN);
    }
    final ColorStateList tint = a.getColorStateList(R.styleable.NinePatchDrawable_tint);
    if (tint != null) {
        state.mTint = tint;
    }
}
Also used : Rect(android.graphics.Rect) InputStream(java.io.InputStream) NinePatch(android.graphics.NinePatch) ColorStateList(android.content.res.ColorStateList) IOException(java.io.IOException) Paint(android.graphics.Paint) Bitmap(android.graphics.Bitmap) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) Resources(android.content.res.Resources) BitmapFactory(android.graphics.BitmapFactory) TypedValue(android.util.TypedValue)

Example 4 with NinePatch

use of android.graphics.NinePatch in project platform_frameworks_base by android.

the class NinePatchDrawable method updateStateFromTypedArray.

/**
     * Updates the constant state from the values in the typed array.
     */
private void updateStateFromTypedArray(@NonNull TypedArray a) throws XmlPullParserException {
    final Resources r = a.getResources();
    final NinePatchState state = mNinePatchState;
    // Account for any configuration changes.
    state.mChangingConfigurations |= a.getChangingConfigurations();
    // Extract the theme attributes, if any.
    state.mThemeAttrs = a.extractThemeAttrs();
    state.mDither = a.getBoolean(R.styleable.NinePatchDrawable_dither, state.mDither);
    final int srcResId = a.getResourceId(R.styleable.NinePatchDrawable_src, 0);
    if (srcResId != 0) {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inDither = !state.mDither;
        options.inScreenDensity = r.getDisplayMetrics().noncompatDensityDpi;
        final Rect padding = new Rect();
        final Rect opticalInsets = new Rect();
        Bitmap bitmap = null;
        try {
            final TypedValue value = new TypedValue();
            final InputStream is = r.openRawResource(srcResId, value);
            bitmap = BitmapFactory.decodeResourceStream(r, value, is, padding, options);
            is.close();
        } catch (IOException e) {
        // Ignore
        }
        if (bitmap == null) {
            throw new XmlPullParserException(a.getPositionDescription() + ": <nine-patch> requires a valid src attribute");
        } else if (bitmap.getNinePatchChunk() == null) {
            throw new XmlPullParserException(a.getPositionDescription() + ": <nine-patch> requires a valid 9-patch source image");
        }
        bitmap.getOpticalInsets(opticalInsets);
        state.mNinePatch = new NinePatch(bitmap, bitmap.getNinePatchChunk());
        state.mPadding = padding;
        state.mOpticalInsets = Insets.of(opticalInsets);
    }
    state.mAutoMirrored = a.getBoolean(R.styleable.NinePatchDrawable_autoMirrored, state.mAutoMirrored);
    state.mBaseAlpha = a.getFloat(R.styleable.NinePatchDrawable_alpha, state.mBaseAlpha);
    final int tintMode = a.getInt(R.styleable.NinePatchDrawable_tintMode, -1);
    if (tintMode != -1) {
        state.mTintMode = Drawable.parseTintMode(tintMode, Mode.SRC_IN);
    }
    final ColorStateList tint = a.getColorStateList(R.styleable.NinePatchDrawable_tint);
    if (tint != null) {
        state.mTint = tint;
    }
}
Also used : Rect(android.graphics.Rect) InputStream(java.io.InputStream) NinePatch(android.graphics.NinePatch) ColorStateList(android.content.res.ColorStateList) IOException(java.io.IOException) Paint(android.graphics.Paint) Bitmap(android.graphics.Bitmap) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) Resources(android.content.res.Resources) BitmapFactory(android.graphics.BitmapFactory) TypedValue(android.util.TypedValue)

Example 5 with NinePatch

use of android.graphics.NinePatch in project android_frameworks_base by AOSPA.

the class NinePatchDrawable method computeBitmapSize.

private void computeBitmapSize() {
    final NinePatch ninePatch = mNinePatchState.mNinePatch;
    if (ninePatch == null) {
        return;
    }
    final int sourceDensity = ninePatch.getDensity();
    final int targetDensity = mTargetDensity;
    final Insets sourceOpticalInsets = mNinePatchState.mOpticalInsets;
    if (sourceOpticalInsets != Insets.NONE) {
        final int left = Drawable.scaleFromDensity(sourceOpticalInsets.left, sourceDensity, targetDensity, true);
        final int top = Drawable.scaleFromDensity(sourceOpticalInsets.top, sourceDensity, targetDensity, true);
        final int right = Drawable.scaleFromDensity(sourceOpticalInsets.right, sourceDensity, targetDensity, true);
        final int bottom = Drawable.scaleFromDensity(sourceOpticalInsets.bottom, sourceDensity, targetDensity, true);
        mOpticalInsets = Insets.of(left, top, right, bottom);
    } else {
        mOpticalInsets = Insets.NONE;
    }
    final Rect sourcePadding = mNinePatchState.mPadding;
    if (sourcePadding != null) {
        if (mPadding == null) {
            mPadding = new Rect();
        }
        mPadding.left = Drawable.scaleFromDensity(sourcePadding.left, sourceDensity, targetDensity, false);
        mPadding.top = Drawable.scaleFromDensity(sourcePadding.top, sourceDensity, targetDensity, false);
        mPadding.right = Drawable.scaleFromDensity(sourcePadding.right, sourceDensity, targetDensity, false);
        mPadding.bottom = Drawable.scaleFromDensity(sourcePadding.bottom, sourceDensity, targetDensity, false);
    } else {
        mPadding = null;
    }
    mBitmapHeight = Drawable.scaleFromDensity(ninePatch.getHeight(), sourceDensity, targetDensity, true);
    mBitmapWidth = Drawable.scaleFromDensity(ninePatch.getWidth(), sourceDensity, targetDensity, true);
    final NinePatch.InsetStruct insets = ninePatch.getBitmap().getNinePatchInsets();
    if (insets != null) {
        Rect outlineRect = insets.outlineRect;
        mOutlineInsets = NinePatch.InsetStruct.scaleInsets(outlineRect.left, outlineRect.top, outlineRect.right, outlineRect.bottom, targetDensity / (float) sourceDensity);
        mOutlineRadius = Drawable.scaleFromDensity(insets.outlineRadius, sourceDensity, targetDensity);
    } else {
        mOutlineInsets = null;
    }
}
Also used : Rect(android.graphics.Rect) Insets(android.graphics.Insets) NinePatch(android.graphics.NinePatch) Paint(android.graphics.Paint)

Aggregations

NinePatch (android.graphics.NinePatch)11 Paint (android.graphics.Paint)11 Rect (android.graphics.Rect)11 Bitmap (android.graphics.Bitmap)6 BitmapFactory (android.graphics.BitmapFactory)6 TypedValue (android.util.TypedValue)6 IOException (java.io.IOException)6 InputStream (java.io.InputStream)6 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)6 ColorStateList (android.content.res.ColorStateList)5 Resources (android.content.res.Resources)5 Insets (android.graphics.Insets)5 TypedArray (android.content.res.TypedArray)1