Example usage for android.graphics.drawable Drawable draw

List of usage examples for android.graphics.drawable Drawable draw

Introduction

In this page you can find the example usage for android.graphics.drawable Drawable draw.

Prototype

public abstract void draw(@NonNull Canvas canvas);

Source Link

Document

Draw in its bounds (set via setBounds) respecting optional effects such as alpha (set via setAlpha) and color filter (set via setColorFilter).

Usage

From source file:com.bullmobi.message.graphics.IconFactory.java

@NonNull
private static Bitmap createIcon(@NonNull Drawable drawable, int size) {
    Bitmap icon = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_4444);
    Canvas canvas = new Canvas(icon);

    // Calculate scale ratios
    int drawableWidth = drawable.getIntrinsicWidth();
    int drawableHeight = drawable.getIntrinsicHeight();
    float ratioX = Math.min((float) drawableWidth / drawableHeight, 1f);
    float ratioY = Math.min((float) drawableHeight / drawableWidth, 1f);

    // Calculate new width and height
    int width = Math.round(size * ratioX);
    int height = Math.round(size * ratioY);
    int paddingLeft = (size - width) / 2;
    int paddingTop = (size - height) / 2;

    // Apply size and draw
    canvas.translate(paddingLeft, paddingTop);
    drawable = drawable.mutate();//w w w .jav a 2  s .co m
    drawable.setBounds(0, 0, width, height);
    drawable.draw(canvas);

    return icon;
}

From source file:Main.java

public static Bitmap createRoundedFramedImage(Drawable imageDrawable, int borderThickness) {
    int size = Math.min(imageDrawable.getMinimumWidth(), imageDrawable.getMinimumHeight());
    //      Drawable imageDrawable = (image != null) ? new BitmapDrawable(image) : placeHolder;

    Bitmap output = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    RectF outerRect = new RectF(0, 0, size, size);
    float cornerRadius = size / 18f;

    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(Color.RED);//from ww w .  j a  v  a 2 s.co m
    canvas.drawRoundRect(outerRect, cornerRadius, cornerRadius, paint);

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    imageDrawable.setBounds(0, 0, size, size);

    // Save the layer to apply the paint
    canvas.saveLayer(outerRect, paint, Canvas.ALL_SAVE_FLAG);
    imageDrawable.draw(canvas);
    canvas.restore();

    // FRAMING THE PHOTO
    float border = size / 15f;

    // 1. Create offscreen bitmap link: http://www.youtube.com/watch?feature=player_detailpage&v=jF6Ad4GYjRU#t=1035s
    Bitmap framedOutput = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
    Canvas framedCanvas = new Canvas(framedOutput);
    // End of Step 1

    // Start - TODO IMPORTANT - this section shouldn't be included in the final code
    // It's needed here to differentiate step 2 (red) with the background color of the activity
    // It's should be commented out after the codes includes step 3 onwards
    // Paint squaredPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    // squaredPaint.setColor(Color.BLUE);
    // framedCanvas.drawRoundRect(outerRect, 0f, 0f, squaredPaint);
    // End

    // 2. Draw an opaque rounded rectangle link:
    // http://www.youtube.com/watch?feature=player_detailpage&v=jF6Ad4GYjRU#t=1044s
    RectF innerRect = new RectF(border, border, size - border, size - border);

    Paint innerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    innerPaint.setColor(Color.RED);
    framedCanvas.drawRoundRect(innerRect, cornerRadius, cornerRadius, innerPaint);

    // 3. Set the Power Duff mode link:
    // http://www.youtube.com/watch?feature=player_detailpage&v=jF6Ad4GYjRU#t=1056s
    Paint outerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    outerPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT));

    // 4. Draw a translucent rounded rectangle link:
    // http://www.youtube.com/watch?feature=player_detailpage&v=jF6Ad4GYjRU
    outerPaint.setColor(Color.argb(100, 0, 0, 0));
    framedCanvas.drawRoundRect(outerRect, cornerRadius, cornerRadius, outerPaint);

    // 5. Draw the frame on top of original bitmap
    canvas.drawBitmap(framedOutput, 0f, 0f, null);

    return output;
}

From source file:Main.java

public static Bitmap createRoundedFramedImage(Drawable imageDrawable, int borderThickness, int color) {
    int size = Math.min(imageDrawable.getMinimumWidth(), imageDrawable.getMinimumHeight());
    //      Drawable imageDrawable = (image != null) ? new BitmapDrawable(image) : placeHolder;

    Bitmap output = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    RectF outerRect = new RectF(0, 0, size, size);
    float cornerRadius = size / 18f;

    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(Color.RED);/*from w  w  w  .  j  a v  a 2  s  . c  om*/
    canvas.drawRoundRect(outerRect, cornerRadius, cornerRadius, paint);

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    imageDrawable.setBounds(0, 0, size, size);

    // Save the layer to apply the paint
    canvas.saveLayer(outerRect, paint, Canvas.ALL_SAVE_FLAG);
    imageDrawable.draw(canvas);
    canvas.restore();

    // FRAMING THE PHOTO
    float border = size / 15f;

    // 1. Create offscreen bitmap link: http://www.youtube.com/watch?feature=player_detailpage&v=jF6Ad4GYjRU#t=1035s
    Bitmap framedOutput = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
    Canvas framedCanvas = new Canvas(framedOutput);
    // End of Step 1

    // Start - TODO IMPORTANT - this section shouldn't be included in the final code
    // It's needed here to differentiate step 2 (red) with the background color of the activity
    // It's should be commented out after the codes includes step 3 onwards
    // Paint squaredPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    // squaredPaint.setColor(Color.BLUE);
    // framedCanvas.drawRoundRect(outerRect, 0f, 0f, squaredPaint);
    // End

    // 2. Draw an opaque rounded rectangle link:
    // http://www.youtube.com/watch?feature=player_detailpage&v=jF6Ad4GYjRU#t=1044s
    RectF innerRect = new RectF(border, border, size - border, size - border);

    Paint innerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    innerPaint.setColor(Color.RED);
    framedCanvas.drawRoundRect(innerRect, cornerRadius, cornerRadius, innerPaint);

    // 3. Set the Power Duff mode link:
    // http://www.youtube.com/watch?feature=player_detailpage&v=jF6Ad4GYjRU#t=1056s
    Paint outerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    outerPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT));

    // 4. Draw a translucent rounded rectangle link:
    // http://www.youtube.com/watch?feature=player_detailpage&v=jF6Ad4GYjRU
    outerPaint.setColor(color);
    framedCanvas.drawRoundRect(outerRect, cornerRadius, cornerRadius, outerPaint);

    // 5. Draw the frame on top of original bitmap
    canvas.drawBitmap(framedOutput, 0f, 0f, null);

    return output;
}

From source file:jahirfiquitiva.iconshowcase.utilities.utils.Utils.java

public static Bitmap drawableToBitmap(Drawable drawable) {
    Bitmap bitmap;//from  w  ww.  j  av a  2  s .c o m
    if (drawable instanceof BitmapDrawable) {
        BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
        if (bitmapDrawable.getBitmap() != null) {
            return bitmapDrawable.getBitmap();
        }
    }
    if (drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
        bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
    } else {
        bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
                Bitmap.Config.ARGB_8888);
    }
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
    drawable.draw(canvas);
    return bitmap;
}

From source file:com.tafayor.selfcamerashot.taflib.helpers.GraphicsHelper.java

public static Bitmap drawableToBitmap(Drawable drawable, int width, int height) {
    Bitmap bitmap;//www.  j  a v  a2  s  .co m

    bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());

    /*
    The drawable may be composite of bitmap and drawable layers
     */
    if (drawable instanceof BitmapDrawable) {
        Bitmap bmp = ((BitmapDrawable) drawable).getBitmap();
        canvas.drawBitmap(bmp, (canvas.getWidth() - bmp.getWidth()) / 2,
                (canvas.getHeight() - bmp.getHeight()) / 2, null);
    }
    drawable.draw(canvas);

    return bitmap;
}

From source file:com.negusoft.greenmatter.drawable.CompoundDrawableWrapper.java

@Override
public void draw(Canvas canvas) {
    if (!mSecondaryInBackground)
        super.draw(canvas);
    for (Drawable d : mSecondaryDrawables)
        d.draw(canvas);
    if (mSecondaryInBackground)
        super.draw(canvas);
}

From source file:com.dbeginc.dbweather.utils.animations.widgets.RainFallView.java

@Override
protected void onDraw(Canvas canvas) {
    for (int i = 0; i < mRainFlakeCount; i++) {
        Drawable drawable = drawables.get(i);
        canvas.save();/*from   w w w.j a  va  2s  .  c  o m*/
        canvas.translate(coords[i][0], coords[i][1]);
        drawable.draw(canvas);
        canvas.restore();
    }
    invalidate();
}

From source file:com.dbeginc.dbweather.utils.animations.widgets.SnowFallView.java

@Override
protected void onDraw(Canvas canvas) {
    for (int i = 0; i < snow_flake_count; i++) {
        Drawable drawable = drawables.get(i);
        canvas.save();/*from   w  w w .  ja  v a 2  s  .  c o m*/
        canvas.translate(coords[i][0], coords[i][1]);
        drawable.draw(canvas);
        canvas.restore();
    }
    invalidate();
}

From source file:com.cleveroad.slidingtutorial.sample.renderer.DrawableRenderer.java

@Override
public void draw(@NonNull Canvas canvas, @NonNull RectF elementBounds, @NonNull Paint paint, boolean isActive) {
    Drawable drawable = isActive ? mDrawableActive : mDrawable;
    drawable.setBounds((int) elementBounds.left, (int) elementBounds.top, (int) elementBounds.right,
            (int) elementBounds.bottom);
    drawable.draw(canvas);
}

From source file:me.zhang.bingo.Utility.java

public static Bitmap drawableToBitmap(Drawable drawable) {
    Bitmap bitmap;/*w  ww.ja v a2  s.c om*/

    if (drawable instanceof BitmapDrawable) {
        BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
        if (bitmapDrawable.getBitmap() != null) {
            return bitmapDrawable.getBitmap();
        }
    }

    if (drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
        // Single color bitmap will be created of 1x1 pixel.
        bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
    } else {
        bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
                Bitmap.Config.ARGB_8888);
    }

    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);
    return bitmap;
}