Example usage for android.graphics Canvas translate

List of usage examples for android.graphics Canvas translate

Introduction

In this page you can find the example usage for android.graphics Canvas translate.

Prototype

public void translate(float dx, float dy) 

Source Link

Document

Preconcat the current matrix with the specified translation

Usage

From source file:com.hellofyc.base.util.ViewUtils.java

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
static void blur(Context context, View view) {
    view.buildDrawingCache();//ww  w.  j a  va2  s. c o  m
    Bitmap srcBitmap = view.getDrawingCache();
    float radius = 20f;

    Bitmap overlay = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(),
            Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(overlay);
    canvas.translate(-view.getLeft(), -view.getTop());
    canvas.drawBitmap(srcBitmap, 0, 0, null);

    RenderScript script = RenderScript.create(context);
    Allocation allocation = Allocation.createFromBitmap(script, overlay);
    ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(script, allocation.getElement());
    blur.setInput(allocation);
    blur.setRadius(radius);
    blur.forEach(allocation);
    allocation.copyTo(overlay);

    view.setBackground(new BitmapDrawable(context.getResources(), overlay));

    script.destroy();
}

From source file:com.nadmm.airports.utils.UiUtils.java

public static Drawable combineDrawables(Context context, Drawable d1, Drawable d2, int paddingDp) {
    // Assumes both d1 & d2 are same size and square shaped
    int w = d1.getIntrinsicWidth();
    int h = d1.getIntrinsicHeight();
    int paddingPx = convertDpToPx(context, paddingDp);
    Bitmap result = Bitmap.createBitmap(w + (d2 != null ? w + paddingPx : 0), h, Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(result);
    canvas.setDensity(Bitmap.DENSITY_NONE);
    d1.setBounds(0, 0, w - 1, h - 1);/*from  w ww  .j a v a  2s. co  m*/
    d1.draw(canvas);
    if (d2 != null) {
        canvas.translate(w + paddingPx, 0);
        d2.setBounds(0, 0, w - 1, h - 1);
        d2.draw(canvas);
    }

    return new BitmapDrawable(context.getResources(), result);
}

From source file:Main.java

private static void onCenterDraw(TextView view, Canvas canvas, Drawable drawable, int gravity) {
    int drawablePadding = view.getCompoundDrawablePadding();
    int ratio = 1;
    float total;/*from   w  ww .  j  av a2 s .c o  m*/

    switch (gravity) {
    case Gravity.END:
        ratio = -1;
    case Gravity.START:
        total = view.getPaint().measureText(view.getText().toString()) + drawable.getIntrinsicWidth()
                + drawablePadding + view.getPaddingLeft() + view.getPaddingRight();
        canvas.translate(ratio * (view.getWidth() - total) / 2, 0);
        break;
    case Gravity.BOTTOM:
        ratio = -1;
    case Gravity.TOP:
        Paint.FontMetrics fontMetrics = view.getPaint().getFontMetrics();
        total = fontMetrics.descent - fontMetrics.ascent + drawable.getIntrinsicHeight() + drawablePadding
                + view.getPaddingTop() + view.getPaddingBottom();
        canvas.translate(0, ratio * (view.getHeight() - total) / 2);
        break;
    }
}

From source file:Main.java

/**
 * Creates a mutable bitmap from subset of source bitmap, transformed by the optional matrix.
 *//*  w w w .j  ava 2s .  co  m*/
private static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m) {
    // Re-implement Bitmap createBitmap() to always return a mutable bitmap.
    Canvas canvas = new Canvas();

    Bitmap bitmap;
    Paint paint;
    if ((m == null) || m.isIdentity()) {
        bitmap = Bitmap.createBitmap(width, height, source.getConfig());
        paint = null;
    } else {
        RectF rect = new RectF(0, 0, width, height);
        m.mapRect(rect);
        bitmap = Bitmap.createBitmap(Math.round(rect.width()), Math.round(rect.height()), source.getConfig());

        canvas.translate(-rect.left, -rect.top);
        canvas.concat(m);

        paint = new Paint(Paint.FILTER_BITMAP_FLAG);
        if (!m.rectStaysRect()) {
            paint.setAntiAlias(true);
        }
    }
    bitmap.setDensity(source.getDensity());
    canvas.setBitmap(bitmap);

    Rect srcBounds = new Rect(x, y, x + width, y + height);
    RectF dstBounds = new RectF(0, 0, width, height);
    canvas.drawBitmap(source, srcBounds, dstBounds, paint);
    return bitmap;
}

From source file:Main.java

private static void onCenterDraw(TextView view, Canvas canvas, Drawable drawable, int gravity) {
    int drawablePadding = view.getCompoundDrawablePadding();
    int ratio = 1;
    float total;//from  w  w  w. ja v a2s .c o m

    switch (gravity) {
    case Gravity.RIGHT:
        ratio = -1;
    case Gravity.LEFT:
        total = view.getPaint().measureText(view.getText().toString()) + drawable.getIntrinsicWidth()
                + drawablePadding + view.getPaddingLeft() + view.getPaddingRight();
        canvas.translate(ratio * (view.getWidth() - total) / 2, 0);
        break;
    case Gravity.BOTTOM:
        ratio = -1;
    case Gravity.TOP:
        Paint.FontMetrics fontMetrics0 = view.getPaint().getFontMetrics();
        total = fontMetrics0.descent - fontMetrics0.ascent + drawable.getIntrinsicHeight() + drawablePadding
                + view.getPaddingTop() + view.getPaddingBottom();
        canvas.translate(0, ratio * (view.getHeight() - total) / 2);
        break;
    }
}

From source file:com.android.mail.ui.FolderDisplayer.java

public static void drawFolder(Canvas canvas, float x, float y, int width, int height, String name, int fgColor,
        int bgColor, FolderDisplayer.FolderDrawableResources res, BidiFormatter formatter, Paint paint) {
    canvas.save();/*w ww. j a v a 2 s.c o  m*/
    canvas.translate(x, y + res.folderVerticalOffset);

    // Draw the box.
    paint.setColor(bgColor);
    paint.setStyle(Paint.Style.FILL);
    final RectF rect = new RectF(0, 0, width, height);
    canvas.drawRoundRect(rect, res.folderRoundedCornerRadius, res.folderRoundedCornerRadius, paint);

    // Draw the text based on the language locale and layout direction.
    paint.setColor(fgColor);
    paint.setStyle(Paint.Style.FILL);

    // Compute the text/gradient indices
    final int textLength = (int) paint.measureText(name);
    final int gradientX0;
    final int gradientX1;
    final int textX;

    /***************************************************************************************************
     * width               - the actual folder chip rectangle.                                         *
     * textLength          - the length of the folder's full name (can be longer than                  *
     *                         the actual chip, which is what overflow gradient is for).               *
     * innerPadding        - the padding between the text and the chip edge.                           *
     * overflowPadding     - the padding between start of overflow and the chip edge.                  *
     *                                                                                                 *
     *                                                                                                 *
     * text is in a RTL language                                                                       *
     *                                                                                                 *
     *                   index-0                                                                       *
     *                      |<---------------------------- width ---------------------------->|        *
     *        |<-------------------------textLength------------------>|                       |        *
     *        |             |<----- overflowPadding ----->|                                   |        *
     *        |             |<- innerPadding ->|<-------->|<--------->|<- horizontalPadding ->|        *
     *       textX                            gX1        gX0                                           *
     *                                                                                                 *
     *                                                                                                 *
     * text is in a LTR language.                                                                      *
     *                                                                                                 *
     *     index-0                                                                                     *
     *        |<------------------------------ width ------------------------------->|                 *
     *        |                       |<-------------------------textLength-------------------->|      *
     *        |                                   |<-------- overflowPadding ------->|                 *
     *        |<- horizontalPadding ->|<--------->|<-------->|<- horizontalPadding ->|                 *
     *                              textX        gX0        gX1                                        *
     *                                                                                                 *
     **************************************************************************************************/
    if (formatter.isRtl(name)) {
        gradientX0 = res.overflowGradientPadding;
        gradientX1 = res.folderHorizontalPadding;
        textX = width - res.folderHorizontalPadding - textLength;
    } else {
        gradientX0 = width - res.overflowGradientPadding;
        gradientX1 = width - res.folderHorizontalPadding;
        textX = res.folderHorizontalPadding;
    }

    // Draw the text and the possible overflow gradient
    // Overflow happens when the text is longer than the chip width minus side paddings.
    if (textLength > width - 2 * res.folderHorizontalPadding) {
        final Shader shader = new LinearGradient(gradientX0, 0, gradientX1, 0, fgColor,
                Utils.getTransparentColor(fgColor), Shader.TileMode.CLAMP);
        paint.setShader(shader);
    }
    final int textY = height / 2 - (int) (paint.descent() + paint.ascent()) / 2;
    canvas.drawText(name, textX, textY, paint);
    paint.setShader(null);

    canvas.restore();
}

From source file:com.github.shareme.gwsmaterialuikit.library.advancerv.draggable.BaseEdgeEffectDecorator.java

private static boolean drawGlow(Canvas c, RecyclerView parent, int dir, EdgeEffectCompat edge) {
    if (edge.isFinished()) {
        return false;
    }//from  ww  w.  j  ava  2  s. c om

    final int restore = c.save();
    final boolean clipToPadding = getClipToPadding(parent);

    switch (dir) {
    case EDGE_TOP:
        if (clipToPadding) {
            c.translate(parent.getPaddingLeft(), parent.getPaddingTop());
        }
        break;
    case EDGE_BOTTOM:
        c.rotate(180);
        if (clipToPadding) {
            c.translate(-parent.getWidth() + parent.getPaddingRight(),
                    -parent.getHeight() + parent.getPaddingBottom());
        } else {
            c.translate(-parent.getWidth(), -parent.getHeight());
        }
        break;
    case EDGE_LEFT:
        c.rotate(-90);
        if (clipToPadding) {
            c.translate(-parent.getHeight() + parent.getPaddingTop(), parent.getPaddingLeft());
        } else {
            c.translate(-parent.getHeight(), 0);
        }
        break;
    case EDGE_RIGHT:
        c.rotate(90);
        if (clipToPadding) {
            c.translate(parent.getPaddingTop(), -parent.getWidth() + parent.getPaddingRight());
        } else {
            c.translate(0, -parent.getWidth());
        }
        break;
    }

    boolean needsInvalidate = edge.draw(c);

    c.restoreToCount(restore);

    return needsInvalidate;
}

From source file:biz.varkon.shelvesom.util.ImageUtilities.java

/**
 * Create a book cover with the specified bitmap. This method applies
 * several lighting effects to the original bitmap and returns a new decored
 * bitmap.//w w w  .  j  a  v a 2s  .c  o m
 * 
 * @param bitmap
 *            The bitmap to decor with lighting effects
 * @param width
 *            The target width of the decored bitmap
 * @param height
 *            The target height of the decored bitmap
 * 
 * @return A new Bitmap based on the original bitmap
 */
public static Bitmap createCover(Bitmap bitmap, int width, int height) {
    final int bitmapWidth = bitmap.getWidth();
    final int bitmapHeight = bitmap.getHeight();

    final float scale = Math.min((float) width / (float) bitmapWidth, (float) height / (float) bitmapHeight);

    final int scaledWidth = (int) (bitmapWidth * scale);
    final int scaledHeight = (int) (bitmapHeight * scale);

    final Bitmap decored = createScaledBitmap(bitmap, scaledWidth, scaledHeight, SHADOW_RADIUS, true,
            SHADOW_PAINT);
    bitmap.recycle();

    final Canvas canvas = new Canvas(decored);

    canvas.translate(SHADOW_RADIUS / 2.0f, SHADOW_RADIUS / 2.0f);
    canvas.drawRect(EDGE_START, 0.0f, EDGE_END, scaledHeight, EDGE_PAINT);
    canvas.drawRect(FOLD_START, 0.0f, FOLD_END, scaledHeight, FOLD_PAINT);
    // noinspection PointlessArithmeticExpression
    canvas.translate(scaledWidth - (EDGE_END - EDGE_START), 0.0f);
    canvas.drawRect(EDGE_START, 0.0f, EDGE_END, scaledHeight, END_EDGE_PAINT);

    return decored;
}

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();//from  ww  w . j a  va 2 s.  c o m
    drawable.setBounds(0, 0, width, height);
    drawable.draw(canvas);

    return icon;
}

From source file:Main.java

public static Bitmap transferMode(Bitmap src, Bitmap mask, Xfermode xfermode) {
    final int width = mask.getWidth();
    final int height = mask.getHeight();
    final Bitmap dst = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    final Canvas canvas = new Canvas(dst);
    final Paint paint = new Paint();
    paint.setAntiAlias(true);/*from   w  w  w. j  a  v  a2 s  .  c  om*/

    final int srcWidth = src.getWidth();
    final int srcHeight = src.getHeight();

    canvas.save();

    // Scale down the image first if required.
    if ((width < srcWidth) || (height < srcHeight)) {
        float radioX = (float) width / srcWidth;
        float radioY = (float) height / srcHeight;
        float radio = 1f;
        float dx = 0f;
        float dy = 0f;

        if (radioX > radioY) {
            radio = radioX;
            dy = (height / radioX - srcHeight) / 2.0f;
        } else {
            radio = radioY;
            dx = (width / radioX - srcWidth) / 2.0f;
        }

        canvas.scale(radio, radio);
        canvas.translate(dx, dy);
    }
    canvas.drawBitmap(src, 0, 0, paint);
    canvas.restore();

    if (xfermode != null) {
        paint.setXfermode(xfermode);
        canvas.drawBitmap(mask, 0, 0, paint);
    }

    return dst;
}