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.bigpigs.fragments.SearchFragment.java

private Bitmap getMarkerBitmapFromView(@DrawableRes int resId) {

    View customMarkerView = ((LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE))
            .inflate(R.layout.custom_marker, null);
    ImageView markerImageView = (ImageView) customMarkerView.findViewById(R.id.marker_icon);
    markerImageView.setImageResource(resId);
    customMarkerView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
    customMarkerView.layout(0, 0, customMarkerView.getMeasuredWidth(), customMarkerView.getMeasuredHeight());
    customMarkerView.buildDrawingCache();
    Bitmap returnedBitmap = Bitmap.createBitmap(customMarkerView.getMeasuredWidth(),
            customMarkerView.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(returnedBitmap);
    canvas.drawColor(Color.WHITE, PorterDuff.Mode.SRC_IN);
    Drawable drawable = customMarkerView.getBackground();
    if (drawable != null)
        drawable.draw(canvas);
    customMarkerView.draw(canvas);/*from   ww w  .j a  v a2 s.  co  m*/
    return returnedBitmap;
}

From source file:com.example.google.walkway.MainActivity.java

private Bitmap getDotMarkerBitmap() {
    Log.d(LOG_TAG, "getDotMarkerBitmap()");

    if (mDotMarkerBitmap == null || mDotMarkerBitmap.isRecycled()) {
        // Create a marker bitmap from the dot shape drawable.
        int px = getResources().getDimensionPixelSize(R.dimen.map_dot_marker_size);
        mDotMarkerBitmap = Bitmap.createBitmap(px, px, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(mDotMarkerBitmap);
        Drawable shape = getResources().getDrawable(R.drawable.map_dot_red);
        shape.setBounds(0, 0, mDotMarkerBitmap.getWidth(), mDotMarkerBitmap.getHeight());
        shape.draw(canvas);
    }/*w  w  w .  jav  a  2  s .c o m*/

    return mDotMarkerBitmap;
}

From source file:org.ciasaboark.tacere.manager.NotificationManagerWrapper.java

private Bitmap combineDrawablesToBitmap(final Drawable backgroundImage, final BitmapDrawable overlayImage,
        int width, int height) {

    Bitmap canvasBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    // Create a canvas, that will draw on to canvasBitmap.
    Canvas imageCanvas = new Canvas(canvasBitmap);

    // Draw the image to our canvas
    backgroundImage.setBounds(0, 0, width, height);
    backgroundImage.draw(imageCanvas);
    overlayImage.setBounds(0, 0, (int) (overlayImage.getIntrinsicWidth() * 0.5),
            (int) (overlayImage.getIntrinsicHeight() * 0.5));
    final int scaleWidth = 24;
    final int scaleHeight = 24;
    final float scaleWidthPercent = ((float) scaleWidth) / overlayImage.getIntrinsicWidth();
    final float scaleHeightPercent = ((float) scaleHeight) / overlayImage.getIntrinsicHeight();
    ScaleDrawable scaleDrawable = new ScaleDrawable(overlayImage, 0, scaleWidthPercent, scaleHeightPercent);
    Drawable scaledOverlay = scaleDrawable.getDrawable();
    scaledOverlay.setBounds(0, 0, width, height);
    scaledOverlay.draw(imageCanvas);//w  w w.ja va2  s  .com

    // Combine background and text to a LayerDrawable
    LayerDrawable layerDrawable = new LayerDrawable(
            new Drawable[] { backgroundImage, new BitmapDrawable(canvasBitmap) });
    Bitmap newBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    layerDrawable.setBounds(0, 0, width, height);
    layerDrawable.draw(new Canvas(newBitmap));
    return newBitmap;
}

From source file:net.osmand.plus.views.controls.DynamicListView.java

void drawDivider(Canvas canvas, Drawable divider, Rect bounds) {
    divider.setBounds(bounds);
    divider.draw(canvas);
}

From source file:com.cloverstudio.spika.CameraCropActivity.java

/**
 * Get the image from container - it is already cropped and zoomed If the
 * image is smaller than container it will be black color set aside
 * *//*from   ww  w.  j  a v a 2 s.c  o m*/
private Bitmap getBitmapFromView(View view) {
    Bitmap returnedBitmap = Bitmap.createBitmap(crop_container_size, crop_container_size,
            Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(returnedBitmap);
    Drawable bgDrawable = view.getBackground();
    if (bgDrawable != null)
        bgDrawable.draw(canvas);
    else
        canvas.drawColor(Color.BLACK);
    view.draw(canvas);
    return returnedBitmap;
}

From source file:com.alex.view.loop.IndicatorView.java

/**
 * drawablebitmap//from  w ww.java  2s . c  om
 *
 * @param drawable
 * @return
 */
private Bitmap drawableToBitamp(Drawable drawable) {
    if (null == drawable) {
        return null;
    }
    if (drawable instanceof BitmapDrawable) {
        BitmapDrawable bd = (BitmapDrawable) drawable;
        return bd.getBitmap();
    }
    int w = drawable.getIntrinsicWidth();
    int h = drawable.getIntrinsicHeight();
    Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, w, h);
    drawable.draw(canvas);
    return bitmap;
}

From source file:com.fondesa.recyclerviewdivider.RecyclerViewDivider.java

/**
 * Set the Drawable's bounds and draw it on a Canvas
 *
 * @param drawable Drawable to draw/* www.ja v  a  2 s .  c  o  m*/
 * @param canvas   Canvas used to show the Drawable
 * @param left     left position in px
 * @param top      top position in px
 * @param right    right position in px
 * @param bottom   bottom position in px
 */
private void setBoundsAndDraw(@NonNull Drawable drawable, @NonNull Canvas canvas, int left, int top, int right,
        int bottom) {
    drawable.setBounds(left, top, right, bottom);
    drawable.draw(canvas);
}

From source file:com.forrestguice.suntimeswidget.SuntimesUtils.java

/**
 * @param context context used to access resources
 * @param drawable a Drawable//from  w  w  w . j  a v  a2s  . c  o m
 * @param w width (pixels or dp)
 * @param h height (pixels or dp)
 * @param pxValues true w and h are in pixels, false w and h are in dp
 * @return a Bitmap measuring w,h of the specified drawable
 */
public static Bitmap drawableToBitmap(Context context, Drawable drawable, int w, int h, boolean pxValues) {
    if (drawable instanceof BitmapDrawable) {
        return ((BitmapDrawable) drawable).getBitmap();
    }

    if (!pxValues) {
        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        w = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, w, metrics);
        h = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, h, metrics);
    }
    //Log.d("DEBUG", "drawableToBitmap: " + drawable.toString() + "::" + w + ", " + h);

    if (w <= 0 || h <= 0) {
        Log.w("drawableToBitmap", "invalid width or height: " + w + ", " + h);
        w = h = 1;
    }

    Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);
    return bitmap;
}

From source file:cz.maresmar.sfm.view.user.UserDetailFragment.java

/**
 * Transform any Drawable to bitmap//  w  w  w  . j a va  2s .c  om
 * <p>
 * taken from https://stackoverflow.com/a/35574775/1392034
 *
 * @param drawable Input Drawable
 * @return Transformed Bitmap
 */
private Bitmap getBitmap(@NonNull Drawable drawable) {
    Canvas canvas = new Canvas();
    int imageSize = getResources().getDimensionPixelSize(R.dimen.user_image_size);
    Bitmap bitmap = Bitmap.createBitmap(imageSize, imageSize, Bitmap.Config.ARGB_8888);
    canvas.setBitmap(bitmap);
    drawable.setBounds(0, 0, imageSize, imageSize);
    drawable.draw(canvas);

    return bitmap;
}

From source file:com.achep.acdisplay.ui.widgets.CircleView.java

private void drawCornerIcon(@NonNull Canvas canvas, @NonNull Drawable drawable, int xm, int ym) {
    int width = getMeasuredWidth() - drawable.getBounds().width();
    int height = getMeasuredHeight() - drawable.getBounds().height();
    float margin = (1 - 2 * xm) * mCornerMargin;
    // Draw// www . j  a  va 2s. c  o m
    canvas.save();
    canvas.translate(xm * width + margin, ym * height + margin);
    drawable.draw(canvas);
    canvas.restore();
}