Search in sources :

Example 11 with LayerDrawable

use of android.graphics.drawable.LayerDrawable in project Signal-Android by WhisperSystems.

the class BubbleDrawableBuilder method create.

public Drawable create(Context context) {
    final GradientDrawable bubble = new GradientDrawable();
    final int radius = context.getResources().getDimensionPixelSize(R.dimen.message_bubble_corner_radius);
    final float[] radii = cornerBooleansToRadii(corners, radius);
    bubble.setColor(color);
    bubble.setCornerRadii(radii);
    if (!hasShadow) {
        return bubble;
    } else {
        final GradientDrawable shadow = new GradientDrawable();
        final int distance = context.getResources().getDimensionPixelSize(R.dimen.message_bubble_shadow_distance);
        shadow.setColor(shadowColor);
        shadow.setCornerRadii(radii);
        final LayerDrawable layers = new LayerDrawable(new Drawable[] { shadow, bubble });
        layers.setLayerInset(1, 0, 0, 0, distance);
        return layers;
    }
}
Also used : LayerDrawable(android.graphics.drawable.LayerDrawable) GradientDrawable(android.graphics.drawable.GradientDrawable)

Example 12 with LayerDrawable

use of android.graphics.drawable.LayerDrawable in project Signal-Android by WhisperSystems.

the class ColorPreference method setColorViewValue.

private static void setColorViewValue(View view, int color, boolean selected) {
    if (view instanceof ImageView) {
        ImageView imageView = (ImageView) view;
        Resources res = imageView.getContext().getResources();
        Drawable currentDrawable = imageView.getDrawable();
        GradientDrawable colorChoiceDrawable;
        if (currentDrawable instanceof GradientDrawable) {
            // Reuse drawable
            colorChoiceDrawable = (GradientDrawable) currentDrawable;
        } else {
            colorChoiceDrawable = new GradientDrawable();
            colorChoiceDrawable.setShape(GradientDrawable.OVAL);
        }
        // Set stroke to dark version of color
        //      int darkenedColor = Color.rgb(
        //          Color.red(color) * 192 / 256,
        //          Color.green(color) * 192 / 256,
        //          Color.blue(color) * 192 / 256);
        colorChoiceDrawable.setColor(color);
        //      colorChoiceDrawable.setStroke((int) TypedValue.applyDimension(
        //          TypedValue.COMPLEX_UNIT_DIP, 2, res.getDisplayMetrics()), darkenedColor);
        Drawable drawable = colorChoiceDrawable;
        if (selected) {
            BitmapDrawable checkmark = (BitmapDrawable) res.getDrawable(R.drawable.check);
            checkmark.setGravity(Gravity.CENTER);
            drawable = new LayerDrawable(new Drawable[] { colorChoiceDrawable, checkmark });
        }
        imageView.setImageDrawable(drawable);
    } else if (view instanceof TextView) {
        ((TextView) view).setTextColor(color);
    }
}
Also used : LayerDrawable(android.graphics.drawable.LayerDrawable) LayerDrawable(android.graphics.drawable.LayerDrawable) BitmapDrawable(android.graphics.drawable.BitmapDrawable) Drawable(android.graphics.drawable.Drawable) GradientDrawable(android.graphics.drawable.GradientDrawable) TextView(android.widget.TextView) ImageView(android.widget.ImageView) Resources(android.content.res.Resources) BitmapDrawable(android.graphics.drawable.BitmapDrawable) GradientDrawable(android.graphics.drawable.GradientDrawable)

Example 13 with LayerDrawable

use of android.graphics.drawable.LayerDrawable in project remusic by aa112901.

the class AppCompatProgressBarHelper method getTintTarget.

@Nullable
private Drawable getTintTarget(int layerId, boolean shouldFallback) {
    Drawable layer = null;
    final Drawable d = ((ProgressBar) mView).getProgressDrawable();
    if (d != null) {
        ((ProgressBar) mView).setProgressDrawable(d.mutate());
        if (d instanceof LayerDrawable) {
            layer = ((LayerDrawable) d).findDrawableByLayerId(layerId);
        }
        if (shouldFallback && layer == null) {
            layer = d;
        }
    }
    return layer;
}
Also used : LayerDrawable(android.graphics.drawable.LayerDrawable) LayerDrawable(android.graphics.drawable.LayerDrawable) Drawable(android.graphics.drawable.Drawable) ProgressBar(android.widget.ProgressBar) Nullable(android.support.annotation.Nullable)

Example 14 with LayerDrawable

use of android.graphics.drawable.LayerDrawable in project remusic by aa112901.

the class LayerDrawableUtils method inflateDrawable.

@Override
protected Drawable inflateDrawable(Context context, XmlPullParser parser, AttributeSet attrs) throws IOException, XmlPullParserException {
    final int innerDepth = parser.getDepth() + 1;
    int type;
    int depth;
    int layerAttrUseCount = 0;
    int drawableUseCount = 0;
    int space = STEP << 1;
    //L,T,R,B,S,E,id
    int[][] childLayersAttrs = new int[space][ATTRS.length];
    Drawable[] drawables = new Drawable[space];
    while ((type = parser.next()) != XmlPullParser.END_DOCUMENT && ((depth = parser.getDepth()) >= innerDepth || type != XmlPullParser.END_TAG)) {
        if (type != XmlPullParser.START_TAG) {
            continue;
        }
        if (depth > innerDepth || !parser.getName().equals("item")) {
            continue;
        }
        if (layerAttrUseCount >= childLayersAttrs.length) {
            int[][] dstInt = new int[drawables.length + STEP][ATTRS.length];
            System.arraycopy(childLayersAttrs, 0, dstInt, 0, childLayersAttrs.length);
            childLayersAttrs = dstInt;
        }
        updateLayerAttrs(context, attrs, childLayersAttrs[layerAttrUseCount]);
        layerAttrUseCount++;
        Drawable drawable = getAttrDrawable(context, attrs, android.R.attr.drawable);
        // element.
        if (drawable == null) {
            while ((type = parser.next()) == XmlPullParser.TEXT) {
            }
            if (type != XmlPullParser.START_TAG) {
                throw new XmlPullParserException(parser.getPositionDescription() + ": <item> tag requires a 'drawable' attribute or " + "child tag defining a drawable");
            }
            drawable = createFromXmlInner(context, parser, attrs);
        } else {
            final ColorStateList cls = getTintColorList(context, attrs, R.attr.drawableTint);
            if (cls != null) {
                drawable = ThemeUtils.tintDrawable(drawable, cls, getTintMode(context, attrs, R.attr.drawableTintMode));
            }
        }
        if (drawable != null) {
            if (drawableUseCount >= drawables.length) {
                Drawable[] dst = new Drawable[drawables.length + STEP];
                System.arraycopy(drawables, 0, dst, 0, drawables.length);
                drawables = dst;
            }
            drawables[drawableUseCount] = drawable;
            drawableUseCount++;
        }
    }
    if (drawables[0] == null || drawableUseCount != layerAttrUseCount) {
        return null;
    } else {
        LayerDrawable layerDrawable = new LayerDrawable(drawables);
        for (int i = 0; i < drawables.length; i++) {
            int[] childLayersAttr = childLayersAttrs[i];
            if (childLayersAttr[0] != 0 || childLayersAttr[1] != 0 || childLayersAttr[2] != 0 || childLayersAttr[3] != 0) {
                layerDrawable.setLayerInset(i, childLayersAttr[0], childLayersAttr[1], childLayersAttr[2], childLayersAttr[3]);
            }
            if (childLayersAttr[4] != 0) {
                layerDrawable.setId(i, childLayersAttr[4]);
            }
        }
        return layerDrawable;
    }
}
Also used : LayerDrawable(android.graphics.drawable.LayerDrawable) LayerDrawable(android.graphics.drawable.LayerDrawable) Drawable(android.graphics.drawable.Drawable) ColorStateList(android.content.res.ColorStateList) XmlPullParserException(org.xmlpull.v1.XmlPullParserException)

Example 15 with LayerDrawable

use of android.graphics.drawable.LayerDrawable in project Android-Developers-Samples by johnjohndoe.

the class ThumbnailRadioButton method setThumbnail.

public void setThumbnail(Bitmap bitmap) {
    //Bitmap drawable
    BitmapDrawable bmp = new BitmapDrawable(getResources(), bitmap);
    bmp.setGravity(Gravity.CENTER);
    int strokeWidth = 24;
    //Checked state
    ShapeDrawable rectChecked = new ShapeDrawable(new RectShape());
    rectChecked.getPaint().setColor(0xFFFFFFFF);
    rectChecked.getPaint().setStyle(Paint.Style.STROKE);
    rectChecked.getPaint().setStrokeWidth(strokeWidth);
    rectChecked.setIntrinsicWidth(bitmap.getWidth() + strokeWidth);
    rectChecked.setIntrinsicHeight(bitmap.getHeight() + strokeWidth);
    Drawable[] drawableArray = new Drawable[] { bmp, rectChecked };
    LayerDrawable layerChecked = new LayerDrawable(drawableArray);
    //Unchecked state
    ShapeDrawable rectUnchecked = new ShapeDrawable(new RectShape());
    rectUnchecked.getPaint().setColor(0x0);
    rectUnchecked.getPaint().setStyle(Paint.Style.STROKE);
    rectUnchecked.getPaint().setStrokeWidth(strokeWidth);
    rectUnchecked.setIntrinsicWidth(bitmap.getWidth() + strokeWidth);
    rectUnchecked.setIntrinsicHeight(bitmap.getHeight() + strokeWidth);
    Drawable[] drawableArray2 = new Drawable[] { bmp, rectUnchecked };
    LayerDrawable layerUnchecked = new LayerDrawable(drawableArray2);
    //Statelist drawable
    StateListDrawable states = new StateListDrawable();
    states.addState(new int[] { android.R.attr.state_checked }, layerChecked);
    states.addState(new int[] {}, layerUnchecked);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
        setBackground(states);
    else
        setBackgroundDrawable(states);
    //Offset text to center/bottom of the checkbox
    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setTextSize(getTextSize());
    paint.setTypeface(getTypeface());
    float w = paint.measureText(getText(), 0, getText().length());
    setPadding(getPaddingLeft() + (int) ((bitmap.getWidth() - w) / 2.f + .5f), getPaddingTop() + (int) (bitmap.getHeight() * 0.70), getPaddingRight(), getPaddingBottom());
    setShadowLayer(5, 0, 0, Color.BLACK);
}
Also used : RectShape(android.graphics.drawable.shapes.RectShape) LayerDrawable(android.graphics.drawable.LayerDrawable) LayerDrawable(android.graphics.drawable.LayerDrawable) ShapeDrawable(android.graphics.drawable.ShapeDrawable) BitmapDrawable(android.graphics.drawable.BitmapDrawable) Drawable(android.graphics.drawable.Drawable) StateListDrawable(android.graphics.drawable.StateListDrawable) ShapeDrawable(android.graphics.drawable.ShapeDrawable) BitmapDrawable(android.graphics.drawable.BitmapDrawable) Paint(android.graphics.Paint) StateListDrawable(android.graphics.drawable.StateListDrawable) Paint(android.graphics.Paint)

Aggregations

LayerDrawable (android.graphics.drawable.LayerDrawable)127 Drawable (android.graphics.drawable.Drawable)87 BitmapDrawable (android.graphics.drawable.BitmapDrawable)60 StateListDrawable (android.graphics.drawable.StateListDrawable)44 AnimationDrawable (android.graphics.drawable.AnimationDrawable)42 ClipDrawable (android.graphics.drawable.ClipDrawable)42 ShapeDrawable (android.graphics.drawable.ShapeDrawable)38 Paint (android.graphics.Paint)23 SuppressLint (android.annotation.SuppressLint)19 GradientDrawable (android.graphics.drawable.GradientDrawable)16 Bitmap (android.graphics.Bitmap)14 BitmapShader (android.graphics.BitmapShader)13 ColorDrawable (android.graphics.drawable.ColorDrawable)12 AnimatedVectorDrawable (android.graphics.drawable.AnimatedVectorDrawable)8 OvalShape (android.graphics.drawable.shapes.OvalShape)7 Nullable (android.annotation.Nullable)5 Resources (android.content.res.Resources)5 TypedArray (android.content.res.TypedArray)5 InsetDrawable (android.graphics.drawable.InsetDrawable)5 TransitionDrawable (android.graphics.drawable.TransitionDrawable)5