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:Main.java

public static Bitmap resizeAndCropCenter(Bitmap bitmap, int size, boolean recycle) {
    int w = bitmap.getWidth();
    int h = bitmap.getHeight();
    if (w == size && h == size)
        return bitmap;

    // scale the image so that the shorter side equals to the target;
    // the longer side will be center-cropped.
    float scale = (float) size / Math.min(w, h);

    Bitmap target = Bitmap.createBitmap(size, size, getConfig(bitmap));
    int width = Math.round(scale * bitmap.getWidth());
    int height = Math.round(scale * bitmap.getHeight());
    Canvas canvas = new Canvas(target);
    canvas.translate((size - width) / 2f, (size - height) / 2f);
    canvas.scale(scale, scale);//from   www .  ja v a  2 s . c  o m
    Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG);
    canvas.drawBitmap(bitmap, 0, 0, paint);
    if (recycle)
        bitmap.recycle();
    return target;
}

From source file:Main.java

@Deprecated
public static Bitmap getCombinedByPieces(List<Bitmap> bitmapList, int numStages) {
    //TODO add here the method to greyscale to use the same canvas but to draw a grayscale version
    //i mean, don't use greyscale, but add here all the functionalities to reuse the canvas
    int originalTotalWidth = bitmapList.get(0).getWidth() * numStages;
    Bitmap finalBitmap = Bitmap.createBitmap(originalTotalWidth, bitmapList.get(0).getHeight(),
            Bitmap.Config.ARGB_8888);/*from  ww  w  . j  av a 2 s . c om*/
    float delta = 0f;
    Canvas comboImage = new Canvas(finalBitmap);
    for (int i = 0; i < numStages; i++) {
        comboImage.translate(delta, 0f);
        comboImage.drawBitmap(bitmapList.get(i), 0f, 0f, null);
        delta = originalTotalWidth / numStages;
    }
    return finalBitmap;
}

From source file:Main.java

private static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, float offset,
        boolean clipShadow, Paint paint) {

    int scaledWidth = width;
    int scaledHeight = height;

    final Canvas canvas = new Canvas();
    canvas.translate(offset / 2.0f, offset / 2.0f);

    Bitmap bitmap;/*from w  w w  .j  a  va  2s .  co m*/

    final Rect from = new Rect(x, y, x + width, y + height);
    final RectF to = new RectF(0, 0, width, height);

    if (m == null || m.isIdentity()) {
        bitmap = Bitmap.createBitmap(scaledWidth + (int) offset,
                scaledHeight + (int) (clipShadow ? (offset / 2.0f) : offset), Bitmap.Config.ARGB_8888);
        paint = null;
    } else {
        RectF mapped = new RectF();
        m.mapRect(mapped, to);

        scaledWidth = Math.round(mapped.width());
        scaledHeight = Math.round(mapped.height());

        bitmap = Bitmap.createBitmap(scaledWidth + (int) offset,
                scaledHeight + (int) (clipShadow ? (offset / 2.0f) : offset), Bitmap.Config.ARGB_8888);
        canvas.translate(-mapped.left, -mapped.top);
        canvas.concat(m);
    }

    canvas.setBitmap(bitmap);
    canvas.drawRect(0.0f, 0.0f, width, height, paint);
    canvas.drawBitmap(source, from, to, SCALE_PAINT);

    return bitmap;
}

From source file:Main.java

public static Bitmap resizeAndCropCenterExt(Bitmap bitmap, int size, boolean recycle) {
    int w = bitmap.getWidth();
    int h = bitmap.getHeight();
    if (w == size && h == size)
        return bitmap;

    // scale the image so that the shorter side equals to the target;
    // the longer side will be center-cropped.
    float scale = (float) size / Math.min(w, h);

    int width = Math.round(scale * bitmap.getWidth());
    int height = Math.round(scale * bitmap.getHeight());
    if (width > height) {
        int largeSize = (int) (width <= size * 1.5 ? width : size * 1.5);
        Bitmap target = Bitmap.createBitmap(largeSize, size, getConfig(bitmap));
        Canvas canvas = new Canvas(target);
        canvas.translate((largeSize - width) / 2f, (size - height) / 2f);
        canvas.scale(scale, scale);//from  w  w  w .  j  av a 2s .  c  o m
        Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG);
        canvas.drawBitmap(bitmap, 0, 0, paint);
        if (recycle)
            bitmap.recycle();
        return target;
    } else {
        int largeSize = (int) (height <= size * 1.5 ? height : size * 1.5);
        Bitmap target = Bitmap.createBitmap(size, largeSize, getConfig(bitmap));
        Canvas canvas = new Canvas(target);
        canvas.translate((size - width) / 2f, (largeSize - height) / 2f);
        canvas.scale(scale, scale);
        Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG);
        canvas.drawBitmap(bitmap, 0, 0, paint);
        if (recycle)
            bitmap.recycle();
        return target;
    }
}

From source file:Main.java

public static Bitmap getNewCombinedByPiecesAlsoGrayscaled(List<Bitmap> bitmapList, int currentStage,
        int numStages) {
    Paint paint = new Paint();
    paint.setAntiAlias(true);//from  w ww.j a v a2s  . com
    ColorMatrix cm = new ColorMatrix();
    cm.setSaturation(0);
    ColorMatrixColorFilter colorMatrixColorFilter = new ColorMatrixColorFilter(cm);
    paint.setColorFilter(colorMatrixColorFilter);

    //i mean, don't use greyscale, but add here all the functionalities to reuse the canvas
    int originalTotalWidth = bitmapList.get(0).getWidth() * numStages;
    Bitmap finalBitmap = Bitmap.createBitmap(originalTotalWidth, bitmapList.get(0).getHeight(),
            Bitmap.Config.ARGB_8888);
    float delta = 0f;
    Canvas comboImage = new Canvas(finalBitmap);
    for (int i = 0; i < numStages; i++) {
        comboImage.translate(delta, 0f);
        if (i > currentStage) {
            comboImage.drawBitmap(bitmapList.get(i), 0f, 0f, paint);
        } else {
            comboImage.drawBitmap(bitmapList.get(i), 0f, 0f, null);
        }
        delta = originalTotalWidth / numStages;
    }
    return finalBitmap;
}

From source file:Main.java

/**
 * This is only used when the launcher shortcut is created.
 * //from   w  w w  .  j a v a  2s  .  co m
 * @param bitmap The artist, album, genre, or playlist image that's going to
 *            be cropped.
 * @param size The new size.
 * @return A {@link Bitmap} that has been resized and cropped for a launcher
 *         shortcut.
 */
public static final Bitmap resizeAndCropCenter(final Bitmap bitmap, final int size) {
    Bitmap blurbitmap = null;

    final int w = bitmap.getWidth();
    final int h = bitmap.getHeight();
    if (w == size && h == size) {
        return bitmap;
    }

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
    byte[] bitmapdata = bos.toByteArray();
    ByteArrayInputStream bs = new ByteArrayInputStream(bitmapdata);

    try {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 8;
        options.inJustDecodeBounds = true;
        blurbitmap = BitmapFactory.decodeStream(bs, null, options);
        options.inJustDecodeBounds = false;

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (bs != null) {
            try {
                bs.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    final float mScale = (float) size / Math.min(w, h);

    final Bitmap mTarget = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
    final int mWidth = Math.round(mScale * blurbitmap.getWidth());
    final int mHeight = Math.round(mScale * blurbitmap.getHeight());
    final Canvas mCanvas = new Canvas(mTarget);
    mCanvas.translate((size - mWidth) / 2f, (size - mHeight) / 2f);
    mCanvas.scale(mScale, mScale);
    final Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG);
    mCanvas.drawBitmap(bitmap, 0, 0, paint);
    return mTarget;

}

From source file:Main.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.
 *
 * @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
 *//*from www.  j a  v a2 s  .  c o m*/
public static Bitmap createBookCover(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);
    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:Main.java

public static Bitmap createScreenshot3(View view, int thumbnailWidth, int thumbnailHeight, int top) {
    if (view != null) {
        Bitmap mCapture;//from   w ww  .jav a 2  s  . c om
        try {
            mCapture = Bitmap.createBitmap(thumbnailWidth, thumbnailHeight, Bitmap.Config.RGB_565);
        } catch (OutOfMemoryError e) {
            return null;
        }
        Canvas c = new Canvas(mCapture);
        //            final int left = view.getScrollX();
        //            final int top = view.getScrollY();
        c.translate(0, -top);
        //c.scale(0.65f, 0.65f, left, top);
        try {
            // draw webview may nullpoint
            view.draw(c);
        } catch (Exception e) {
        }
        return mCapture;
    }
    return null;
}

From source file:Main.java

public static Bitmap createPngScreenshot2(View view, int thumbnailWidth, int thumbnailHeight, int top) {
    if (view != null) {
        Bitmap mCapture;/*from  w  w w.j  a  v  a2 s.  c  o  m*/
        try {
            mCapture = Bitmap.createBitmap(thumbnailWidth, thumbnailHeight, Bitmap.Config.ARGB_8888);
        } catch (OutOfMemoryError e) {
            return null;
        }
        Canvas c = new Canvas(mCapture);
        c.drawColor(Color.BLACK);
        //            final int left = view.getScrollX();
        //            final int top = view.getScrollY();
        c.translate(0, -top);
        //c.scale(0.65f, 0.65f, left, top);
        try {
            // draw webview may nullpoint               
            view.draw(c);
        } catch (Exception e) {
        }
        return mCapture;
    }
    return null;
}

From source file:Main.java

/**
 * This is only used when the launcher shortcut is created.
 *
 * @param bitmap The artist, album, genre, or playlist image that's going to
 *               be cropped.//from   w  ww  . j  a v  a  2 s  .com
 * @param size   The new size.
 * @return A {@link Bitmap} that has been resized and cropped for a launcher
 * shortcut.
 */
public static final Bitmap resizeAndCropCenter(final Bitmap bitmap, final int size) {
    final int w = bitmap.getWidth();
    final int h = bitmap.getHeight();
    if (w == size && h == size) {
        return bitmap;
    }

    final float mScale = (float) size / Math.min(w, h);

    final Bitmap mTarget = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
    final int mWidth = Math.round(mScale * bitmap.getWidth());
    final int mHeight = Math.round(mScale * bitmap.getHeight());
    final Canvas mCanvas = new Canvas(mTarget);
    mCanvas.translate((size - mWidth) / 2f, (size - mHeight) / 2f);
    mCanvas.scale(mScale, mScale);
    final Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG);
    mCanvas.drawBitmap(bitmap, 0, 0, paint);
    return mTarget;
}