Example usage for android.graphics.drawable NinePatchDrawable getPadding

List of usage examples for android.graphics.drawable NinePatchDrawable getPadding

Introduction

In this page you can find the example usage for android.graphics.drawable NinePatchDrawable getPadding.

Prototype

@Override
    public boolean getPadding(@NonNull Rect padding) 

Source Link

Usage

From source file:com.google.blockly.android.ui.vertical.PatchManager.java

/**
 * Compute layout measures such as offsets and paddings from the various block patches.
 *///from  w ww. java2s  .  c  o  m
private void computePatchLayoutMeasures(boolean rtl, boolean useHat) {
    final Configuration config = mResources.getConfiguration();

    if (!getPatchDrawable(R.drawable.top_start_default).getPadding(mTempRect)) {
        throw new IllegalStateException("9-patch 'top_start_default' does not have padding.");
    }
    mBlockStartPadding = rtl ? mTempRect.right : mTempRect.left;

    mBlockEndPadding = getPatchDrawable(R.drawable.dummy_input).getIntrinsicWidth();
    mValueInputWidth = getPatchDrawable(R.drawable.value_input_external).getIntrinsicWidth() - mBlockEndPadding;

    final NinePatchDrawable bottomPatchDefault = getPatchDrawable(R.drawable.bottom_start_default);
    if (!bottomPatchDefault.getPadding(mTempRect)) {
        throw new IllegalStateException("9-patch 'bottom_start_default' does not have padding.");
    }
    mBlockBottomPadding = mTempRect.bottom;

    final NinePatchDrawable bottomPatchNext = getPatchDrawable(R.drawable.bottom_start_next);
    if (!bottomPatchNext.getPadding(mTempRect)) {
        throw new IllegalStateException("9-patch 'bottom_start_next' does not have padding.");
    }
    mNextConnectorHeight = mTempRect.bottom - mBlockBottomPadding;

    final NinePatchDrawable topLeftDefaultPatch = getPatchDrawable(
            useHat ? R.drawable.top_start_hat : R.drawable.top_start_default);
    if (!topLeftDefaultPatch.getPadding(mTempRect)) {
        throw new IllegalStateException("9-patch 'top_start_default' does not have padding.");
    }
    ;
    mBlockTopDefaultPadding = mTempRect.top;

    final NinePatchDrawable topLeftPreviousPatch = getPatchDrawable(R.drawable.top_start_previous);
    if (!topLeftPreviousPatch.getPadding(mTempRect)) {
        throw new IllegalStateException("9-patch 'top_start_previous' does not have padding.");
    }
    ;
    mBlockTopPreviousPadding = mTempRect.top;

    final NinePatchDrawable topLeftOutputPatch = getPatchDrawable(R.drawable.top_start_output);
    if (!topLeftOutputPatch.getPadding(mTempRect)) {
        throw new IllegalStateException("9-patch 'top_start_output' does not have padding.");
    }
    ;
    mBlockTopOutputPadding = mTempRect.top;
    mOutputConnectorWidth = (rtl ? mTempRect.right : mTempRect.left) - mBlockStartPadding;
    mOutputConnectorHeight = topLeftOutputPatch.getIntrinsicHeight();

    mBlockTopMinPadding = Math.min(mBlockTopDefaultPadding,
            Math.min(mBlockTopOutputPadding, mBlockTopPreviousPadding));

    // Block height must be sufficient to at least accommodate vertical padding and an Output
    // connector.
    mMinBlockHeight = mBlockTopMinPadding + mOutputConnectorHeight + mBlockBottomPadding;

    final NinePatchDrawable statementTopPatch = getPatchDrawable(R.drawable.statementinput_top);
    if (!statementTopPatch.getPadding(mTempRect)) {
        throw new IllegalStateException("9-patch 'statementinput_top' does not have padding.");
    }
    ;
    mStatementTopThickness = mTempRect.top;
    mStatementInputIndent = statementTopPatch.getIntrinsicWidth();
    mStatementInputPadding = rtl ? mTempRect.right : mTempRect.left;

    final NinePatchDrawable statementBottomPatch = getPatchDrawable(R.drawable.statementinput_bottom);
    if (!statementBottomPatch.getPadding(mTempRect)) {
        throw new IllegalStateException("9-patch 'statementinput_bottom' does not have padding.");
    }
    mStatementBottomThickness = mTempRect.bottom;

    mStatementMinHeight = statementTopPatch.getIntrinsicHeight() + statementBottomPatch.getIntrinsicHeight();

    final NinePatchDrawable inlineInputPatch = getPatchDrawable(R.drawable.value_input_inline);
    mInlineInputMinimumWidth = inlineInputPatch.getIntrinsicWidth();
    mInlineInputMinimumHeight = inlineInputPatch.getIntrinsicHeight();

    inlineInputPatch.getPadding(mTempRect);
    mInlineInputStartPadding = rtl ? mTempRect.right : mTempRect.left;
    mInlineInputTopPadding = mTempRect.top;
    mInlineInputTotalPaddingX = mTempRect.left + mTempRect.right;
    mInlineInputTotalPaddingY = mTempRect.top + mTempRect.bottom;

    // Convenience fields.
    mBlockTotalPaddingX = mBlockStartPadding + mBlockEndPadding;
}

From source file:com.h6ah4i.android.materialshadowninepatch.MaterialShadowContainerView.java

private void updateNinePatchBounds(NinePatchDrawable ninePatch, int childLeft, int childTop, int childRight,
        int childBottom) {
    if (ninePatch == null) {
        return;/*  ww w . j  a va  2s .  c  om*/
    }

    final Rect t = mTempRect;
    ninePatch.getPadding(t);
    ninePatch.setBounds(childLeft - t.left, childTop - t.top, childRight + t.right, childBottom + t.bottom);
}

From source file:org.kde.necessitas.ministro.ExtractStyle.java

public JSONObject getDrawable(Object drawable, String filename) {
    JSONObject json = new JSONObject();
    Bitmap bmp = null;/*from  w w w  .j  a  va2 s. com*/
    if (drawable instanceof Bitmap)
        bmp = (Bitmap) drawable;
    else {
        if (drawable instanceof BitmapDrawable)
            bmp = ((BitmapDrawable) drawable).getBitmap();
        else {
            if (drawable instanceof LayerDrawable) {
                return getLayerDrawable(drawable, filename);
            }
            if (drawable instanceof StateListDrawable) {
                return getStateListDrawable(drawable, filename);
            }
            if (drawable instanceof GradientDrawable) {
                return getGradientDrawable((GradientDrawable) drawable);
            }
            if (drawable instanceof ClipDrawable) {
                try {
                    json.put("type", "clipDrawable");
                    json.put("drawable",
                            getDrawable(
                                    getClipStateDrawableObject(((ClipDrawable) drawable).getConstantState()),
                                    filename));
                    Rect padding = new Rect();
                    if (((Drawable) drawable).getPadding(padding))
                        json.put("padding", getJsonRect(padding));
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return json;
            }
            if (drawable instanceof ColorDrawable) {
                bmp = Bitmap.createBitmap(1, 1, Config.ARGB_8888);
                Drawable d = (Drawable) drawable;
                d.setBounds(0, 0, 1, 1);
                d.draw(new Canvas(bmp));
                Rect padding = new Rect();
                try {
                    json.put("type", "color");
                    json.put("color", bmp.getPixel(0, 0));
                    if (d.getPadding(padding))
                        json.put("padding", getJsonRect(padding));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                return json;
            } else {
                Drawable d = (Drawable) drawable;
                int w = d.getIntrinsicWidth();
                int h = d.getIntrinsicHeight();
                d.setLevel(10000);
                if (w < 1 || h < 1) {
                    w = 100;
                    h = 100;
                }
                bmp = Bitmap.createBitmap(w, h, Config.ARGB_8888);
                d.setBounds(0, 0, w, h);
                d.draw(new Canvas(bmp));
                if (drawable instanceof NinePatchDrawable) {
                    NinePatchDrawable npd = (NinePatchDrawable) drawable;
                    try {
                        json.put("type", "9patch");
                        json.put("drawable", getDrawable(bmp, filename));
                        Rect padding = new Rect();
                        if (npd.getPadding(padding))
                            json.put("padding", getJsonRect(padding));
                        json.put("chunkInfo", findPatchesMarings(d));
                        return json;
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    FileOutputStream out;
    try {
        filename = m_extractPath + filename + ".png";
        out = new FileOutputStream(filename);
        bmp.compress(Bitmap.CompressFormat.PNG, 100, out);
        out.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        json.put("type", "image");
        json.put("path", filename);
        json.put("width", bmp.getWidth());
        json.put("height", bmp.getHeight());
        MinistroActivity.nativeChmode(filename, 0644);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return json;
}