Example usage for android.graphics Canvas scale

List of usage examples for android.graphics Canvas scale

Introduction

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

Prototype

public void scale(float sx, float sy) 

Source Link

Document

Preconcat the current matrix with the specified scale.

Usage

From source file:Main.java

public static Bitmap scale(Bitmap b, float scale) {
    Bitmap res = Bitmap.createBitmap((int) (b.getWidth() * scale + .5f), (int) (b.getHeight() * scale + .5f),
            b.getConfig());//from   w  w w.  j  a va  2  s .c om
    Canvas c = new Canvas(res);
    c.scale(scale, scale);
    c.drawBitmap(b, 0, 0, scalePaint);
    return res;
}

From source file:Main.java

public static Bitmap drawableToBitmap(Drawable drawable, float ratio) {
    int width = (int) Math.ceil(drawable.getIntrinsicWidth() * ratio);
    int height = (int) Math.ceil(drawable.getIntrinsicHeight() * ratio);
    Bitmap bitmap = Bitmap.createBitmap(width, height,
            drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
    Canvas canvas = new Canvas(bitmap);
    canvas.scale(ratio, ratio);
    drawable.setBounds(0, 0, width, height);
    drawable.draw(canvas);/*from  w w w  .  j a  v  a  2 s . co  m*/
    return bitmap;
}

From source file:Main.java

private static Bitmap resizeBitmapByScale(final Bitmap bitmap, final float scale, final boolean recycle) {
    final int width = Math.round(bitmap.getWidth() * scale);
    final int height = Math.round(bitmap.getHeight() * scale);
    if (width == bitmap.getWidth() && height == bitmap.getHeight())
        return bitmap;
    final Bitmap target = Bitmap.createBitmap(width, height, getConfig(bitmap));
    final Canvas canvas = new Canvas(target);
    canvas.scale(scale, scale);
    final Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG);
    canvas.drawBitmap(bitmap, 0, 0, paint);
    if (recycle) {
        bitmap.recycle();/*from   ww  w .  j  a v  a2 s  .c o  m*/
    }
    return target;
}

From source file:Main.java

public static Bitmap resizeBitmapByScale(Bitmap bitmap, float scale, boolean recycle) {
    int width = Math.round(bitmap.getWidth() * scale);
    int height = Math.round(bitmap.getHeight() * scale);
    if (width == bitmap.getWidth() && height == bitmap.getHeight())
        return bitmap;
    Bitmap target = Bitmap.createBitmap(width, height, getConfig(bitmap));
    Canvas canvas = new Canvas(target);
    canvas.scale(scale, scale);
    Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG);
    canvas.drawBitmap(bitmap, 0, 0, paint);
    if (recycle)/*from  w  w w.  jav  a  2 s .  co  m*/
        bitmap.recycle();
    return target;
}

From source file:Main.java

public static Bitmap resizeBitmapByScale(final Bitmap bitmap, final float scale, final boolean recycle) {
    final int width = Math.round(bitmap.getWidth() * scale);
    final int height = Math.round(bitmap.getHeight() * scale);
    if (width == bitmap.getWidth() && height == bitmap.getHeight())
        return bitmap;
    final Bitmap target = Bitmap.createBitmap(width, height, getConfig(bitmap));
    final Canvas canvas = new Canvas(target);
    canvas.scale(scale, scale);
    final Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG);
    canvas.drawBitmap(bitmap, 0, 0, paint);
    if (recycle) {
        bitmap.recycle();/*from w  ww  . j  a v  a  2 s .  c  om*/
    }
    return target;
}

From source file:Main.java

public static Bitmap drawableToBitmap(Drawable drawable, int size) {
    int width = size;
    int height = size;
    int ratio = size / drawable.getIntrinsicHeight();
    Bitmap bitmap = Bitmap.createBitmap(width, height,
            drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, width, height);
    canvas.scale(ratio, ratio);
    drawable.draw(canvas);//  ww w .  j  av a  2 s .  c  o  m
    return bitmap;
}

From source file:Main.java

private static Bitmap getResizeBitmap(View view, Bitmap bitmap) {
    Bitmap overlay = Bitmap.createBitmap((int) (view.getMeasuredWidth() / sScaleFactor),
            (int) (view.getMeasuredHeight() / sScaleFactor), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(overlay);
    canvas.translate(-view.getLeft() / sScaleFactor, -view.getTop() / sScaleFactor);
    canvas.scale(1 / sScaleFactor, 1 / sScaleFactor);
    Paint paint = new Paint();
    paint.setFlags(Paint.FILTER_BITMAP_FLAG);
    canvas.drawBitmap(bitmap, 0, 0, paint);
    return overlay;
}

From source file:Main.java

public static Bitmap drawViewToBitmap(View view, int width, int height, float translateX, float translateY,
        int scaleRatio) {
    float scale = 1f / scaleRatio;
    int bmpWidth = (int) (width * scale - translateX / scaleRatio);
    int bmpHeight = (int) (height * scale - translateY / scaleRatio);
    Bitmap dest = Bitmap.createBitmap(bmpWidth, bmpHeight, Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(dest);
    c.translate(-translateX / scaleRatio, -translateY / scaleRatio);
    if (scaleRatio > 1) {
        c.scale(scale, scale);
    }//from   w w w.  j a  v a 2  s.co  m
    view.draw(c);
    return dest;
}

From source file:Main.java

public static Bitmap resizeDownAndCropCenter(Bitmap bitmap, int size, boolean recycle) {
    int w = bitmap.getWidth();
    int h = bitmap.getHeight();
    int minSide = Math.min(w, h);
    if (w == h && minSide <= size)
        return bitmap;
    size = Math.min(size, minSide);

    float scale = Math.max((float) size / bitmap.getWidth(), (float) size / bitmap.getHeight());
    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);
    Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG);
    canvas.drawBitmap(bitmap, 0, 0, paint);
    if (recycle)/*from   ww  w.  j a  va  2  s  .c  o  m*/
        bitmap.recycle();
    return target;
}

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);
    Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG);
    canvas.drawBitmap(bitmap, 0, 0, paint);
    if (recycle)//w w w . j a  va 2s  .co m
        bitmap.recycle();
    return target;
}