Search in sources :

Example 1 with RectShape

use of android.graphics.drawable.shapes.RectShape 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)

Example 2 with RectShape

use of android.graphics.drawable.shapes.RectShape in project Klyph by jonathangerbaud.

the class DefaultHeaderTransformer method applyProgressBarSettings.

private void applyProgressBarSettings() {
    if (mHeaderProgressBar != null) {
        final int strokeWidth = mHeaderProgressBar.getResources().getDimensionPixelSize(R.dimen.ptr_progress_bar_stroke_width);
        mHeaderProgressBar.setIndeterminateDrawable(new SmoothProgressDrawable.Builder(mHeaderProgressBar.getContext()).color(mProgressDrawableColor).width(strokeWidth).build());
        ShapeDrawable shape = new ShapeDrawable();
        shape.setShape(new RectShape());
        shape.getPaint().setColor(mProgressDrawableColor);
        ClipDrawable clipDrawable = new ClipDrawable(shape, Gravity.CENTER, ClipDrawable.HORIZONTAL);
        mHeaderProgressBar.setProgressDrawable(clipDrawable);
    }
}
Also used : RectShape(android.graphics.drawable.shapes.RectShape) ShapeDrawable(android.graphics.drawable.ShapeDrawable) ClipDrawable(android.graphics.drawable.ClipDrawable)

Example 3 with RectShape

use of android.graphics.drawable.shapes.RectShape in project muzei by romannurik.

the class ScrimUtil method makeCubicGradientScrimDrawable.

/**
     * Creates an approximated cubic gradient using a multi-stop linear gradient. See
     * <a href="https://plus.google.com/+RomanNurik/posts/2QvHVFWrHZf">this post</a> for more
     * details.
     */
public static Drawable makeCubicGradientScrimDrawable(int baseColor, int numStops, int gravity) {
    // Generate a cache key by hashing together the inputs, based on the method described in the Effective Java book
    int cacheKeyHash = baseColor;
    cacheKeyHash = 31 * cacheKeyHash + numStops;
    cacheKeyHash = 31 * cacheKeyHash + gravity;
    Drawable cachedGradient = cubicGradientScrimCache.get(cacheKeyHash);
    if (cachedGradient != null) {
        return cachedGradient;
    }
    numStops = Math.max(numStops, 2);
    PaintDrawable paintDrawable = new PaintDrawable();
    paintDrawable.setShape(new RectShape());
    final int[] stopColors = new int[numStops];
    int red = Color.red(baseColor);
    int green = Color.green(baseColor);
    int blue = Color.blue(baseColor);
    int alpha = Color.alpha(baseColor);
    for (int i = 0; i < numStops; i++) {
        float x = i * 1f / (numStops - 1);
        float opacity = MathUtil.constrain(0, 1, (float) Math.pow(x, 3));
        stopColors[i] = Color.argb((int) (alpha * opacity), red, green, blue);
    }
    final float x0, x1, y0, y1;
    switch(gravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
        case Gravity.LEFT:
            x0 = 1;
            x1 = 0;
            break;
        case Gravity.RIGHT:
            x0 = 0;
            x1 = 1;
            break;
        default:
            x0 = 0;
            x1 = 0;
            break;
    }
    switch(gravity & Gravity.VERTICAL_GRAVITY_MASK) {
        case Gravity.TOP:
            y0 = 1;
            y1 = 0;
            break;
        case Gravity.BOTTOM:
            y0 = 0;
            y1 = 1;
            break;
        default:
            y0 = 0;
            y1 = 0;
            break;
    }
    paintDrawable.setShaderFactory(new ShapeDrawable.ShaderFactory() {

        @Override
        public Shader resize(int width, int height) {
            return new LinearGradient(width * x0, height * y0, width * x1, height * y1, stopColors, null, Shader.TileMode.CLAMP);
        }
    });
    cubicGradientScrimCache.put(cacheKeyHash, paintDrawable);
    return paintDrawable;
}
Also used : LinearGradient(android.graphics.LinearGradient) RectShape(android.graphics.drawable.shapes.RectShape) PaintDrawable(android.graphics.drawable.PaintDrawable) ShapeDrawable(android.graphics.drawable.ShapeDrawable) Drawable(android.graphics.drawable.Drawable) ShapeDrawable(android.graphics.drawable.ShapeDrawable) PaintDrawable(android.graphics.drawable.PaintDrawable) Shader(android.graphics.Shader)

Example 4 with RectShape

use of android.graphics.drawable.shapes.RectShape in project ListenerMusicPlayer by hefuyicoder.

the class ScrimUtil method makeCubicGradientScrimDrawable.

/**
     * Creates an approximated cubic gradient using a multi-stop linear gradient. See
     * <a href="https://plus.google.com/+RomanNurik/posts/2QvHVFWrHZf">this post</a> for more
     * details.
     */
public static Drawable makeCubicGradientScrimDrawable(int baseColor, int numStops, final int gravity) {
    // Generate a cache key by hashing together the inputs, based on the method described in the Effective Java book
    int cacheKeyHash = baseColor;
    cacheKeyHash = 31 * cacheKeyHash + numStops;
    cacheKeyHash = 31 * cacheKeyHash + gravity;
    Drawable cachedGradient = cubicGradientScrimCache.get(cacheKeyHash);
    if (cachedGradient != null) {
        return cachedGradient;
    }
    numStops = Math.max(numStops, 2);
    PaintDrawable paintDrawable = new PaintDrawable();
    paintDrawable.setShape(new RectShape());
    final int[] stopColors = new int[numStops];
    int red = Color.red(baseColor);
    int green = Color.green(baseColor);
    int blue = Color.blue(baseColor);
    int alpha = Color.alpha(baseColor);
    for (int i = 0; i < numStops; i++) {
        float x = i * 1f / (numStops - 1);
        float opacity = MathUtil.constrain(0, 1, (float) Math.pow(x, 3));
        stopColors[i] = Color.argb((int) (alpha * opacity), red, green, blue);
    }
    final float x0, x1, y0, y1;
    switch(gravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
        case Gravity.LEFT:
            x0 = 1;
            x1 = 0;
            break;
        case Gravity.RIGHT:
            x0 = 0;
            x1 = 1;
            break;
        case Gravity.CENTER_HORIZONTAL:
        default:
            x0 = 0;
            x1 = 0;
            break;
    }
    switch(gravity & Gravity.VERTICAL_GRAVITY_MASK) {
        case Gravity.TOP:
            y0 = 1;
            y1 = 0;
            break;
        case Gravity.BOTTOM:
            y0 = 0;
            y1 = 1;
            break;
        case Gravity.CENTER_VERTICAL:
        default:
            y0 = 0;
            y1 = 0;
            break;
    }
    paintDrawable.setShaderFactory(new ShapeDrawable.ShaderFactory() {

        @Override
        public Shader resize(int width, int height) {
            return new LinearGradient(width * x0, height * y0, gravity == Gravity.CENTER_VERTICAL ? width : width * x1, gravity == Gravity.CENTER_HORIZONTAL ? height : height * y1, gravity == Gravity.CENTER_HORIZONTAL || gravity == Gravity.CENTER_VERTICAL ? makeDoubleStopColors(stopColors) : stopColors, null, Shader.TileMode.CLAMP);
        }
    });
    cubicGradientScrimCache.put(cacheKeyHash, paintDrawable);
    return paintDrawable;
}
Also used : LinearGradient(android.graphics.LinearGradient) RectShape(android.graphics.drawable.shapes.RectShape) PaintDrawable(android.graphics.drawable.PaintDrawable) ShapeDrawable(android.graphics.drawable.ShapeDrawable) Drawable(android.graphics.drawable.Drawable) ShapeDrawable(android.graphics.drawable.ShapeDrawable) PaintDrawable(android.graphics.drawable.PaintDrawable) Shader(android.graphics.Shader)

Example 5 with RectShape

use of android.graphics.drawable.shapes.RectShape in project Bitocle by mthli.

the class DefaultHeaderTransformer method applyProgressBarSettings.

private void applyProgressBarSettings() {
    if (mHeaderProgressBar != null) {
        ShapeDrawable shape = new ShapeDrawable();
        shape.setShape(new RectShape());
        shape.getPaint().setColor(mProgressDrawableColor);
        ClipDrawable clipDrawable = new ClipDrawable(shape, Gravity.CENTER, ClipDrawable.HORIZONTAL);
        mHeaderProgressBar.setProgressDrawable(clipDrawable);
    }
}
Also used : RectShape(android.graphics.drawable.shapes.RectShape) ShapeDrawable(android.graphics.drawable.ShapeDrawable) ClipDrawable(android.graphics.drawable.ClipDrawable)

Aggregations

ShapeDrawable (android.graphics.drawable.ShapeDrawable)13 RectShape (android.graphics.drawable.shapes.RectShape)13 Shader (android.graphics.Shader)6 LinearGradient (android.graphics.LinearGradient)5 PaintDrawable (android.graphics.drawable.PaintDrawable)5 Drawable (android.graphics.drawable.Drawable)4 ClipDrawable (android.graphics.drawable.ClipDrawable)3 LayoutParams (android.view.ViewGroup.LayoutParams)2 TextView (android.widget.TextView)2 Bitmap (android.graphics.Bitmap)1 BitmapShader (android.graphics.BitmapShader)1 Paint (android.graphics.Paint)1 BitmapDrawable (android.graphics.drawable.BitmapDrawable)1 LayerDrawable (android.graphics.drawable.LayerDrawable)1 StateListDrawable (android.graphics.drawable.StateListDrawable)1 View (android.view.View)1 OnClickListener (android.view.View.OnClickListener)1 Button (android.widget.Button)1 Field (java.lang.reflect.Field)1