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 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  ww w .j av  a2  s.  c  om*/
    }
    view.draw(c);
    return dest;
}

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 getBitmapFromView(View v) {
    if (v == null) {
        return null;
    }/*www . j a va 2  s .  c o  m*/
    Bitmap screenShot;
    screenShot = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas mCanvas = new Canvas(screenShot);
    mCanvas.translate(-v.getScrollX(), -v.getScrollY());
    v.draw(mCanvas);
    return screenShot;
}

From source file:Main.java

public static Drawable convertViewToDrawable(View view) {
    int spec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    view.measure(spec, spec);//from  w w w.j av  a2s . c  o  m
    view.layout(UPPER_LEFT_X, UPPER_LEFT_Y, view.getMeasuredWidth(), view.getMeasuredHeight());
    Bitmap b = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(b);
    c.translate(-view.getScrollX(), -view.getScrollY());
    view.draw(c);
    view.setDrawingCacheEnabled(true);
    Bitmap cacheBmp = view.getDrawingCache();
    Bitmap viewBmp = cacheBmp.copy(Bitmap.Config.ARGB_8888, true);
    view.destroyDrawingCache();
    return new BitmapDrawable(viewBmp);
}

From source file:Main.java

public static Bitmap getBitmap(View view) {
    int width = view.getWidth();
    int height = view.getHeight();
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
    Canvas canvas = new Canvas(bitmap);
    canvas.translate(-view.getScrollX(), -view.getScrollY());
    view.draw(canvas);//from  w w w  .  j a v a  2  s  .c o m
    return bitmap;
}

From source file:Main.java

public static int drawStaticLayout(Canvas canvas, StaticLayout layout, int x, int y) {
    canvas.translate(x, y);
    layout.draw(canvas);/*  w ww.  j a  v  a  2 s  .  com*/
    canvas.translate(-x, -y);
    return layout.getHeight();
}

From source file:Main.java

public static void blur(Context context, Bitmap bkg, View view, int width, int height) {
    float radius = 20;
    Log.i("hulixia", bkg.getWidth() + "w,h" + bkg.getDensity());
    Bitmap overlay = Bitmap.createBitmap(bkg.getWidth(), bkg.getHeight(), Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(overlay);
    canvas.translate(-view.getLeft(), -view.getTop());
    canvas.drawBitmap(bkg, 0, 0, null);/*from   ww w.  ja v a 2  s .co  m*/

    RenderScript rs = RenderScript.create(context);

    Allocation overlayAlloc = Allocation.createFromBitmap(rs, overlay);
    ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(rs, overlayAlloc.getElement());
    blur.setInput(overlayAlloc);
    blur.setRadius(radius);
    blur.forEach(overlayAlloc);
    overlayAlloc.copyTo(overlay);
    view.setBackground(new BitmapDrawable(context.getResources(), overlay));
    rs.destroy();
}

From source file:Main.java

public static Bitmap drawViewToBitmap(View view, int width, int height, float translateX, float translateY,
        int downSampling, String color) {
    float scale = 1f / downSampling;
    int bmpWidth = (int) (width * scale - translateX / downSampling);
    int bmpHeight = (int) (height * scale - translateY / downSampling);
    Bitmap dest = Bitmap.createBitmap(bmpWidth, bmpHeight, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(dest);
    canvas.translate(-translateX / downSampling, -translateY / downSampling);
    if (downSampling > 1) {
        canvas.scale(scale, scale);//from  www .  j a  v  a  2s . c  om
    }
    Paint paint = new Paint();
    paint.setFlags(Paint.FILTER_BITMAP_FLAG | Paint.ANTI_ALIAS_FLAG);
    PorterDuffColorFilter filter = new PorterDuffColorFilter(Color.parseColor(color), PorterDuff.Mode.SRC_ATOP);
    paint.setColorFilter(filter);
    view.buildDrawingCache();
    Bitmap cache = view.getDrawingCache();
    canvas.drawBitmap(cache, 0, 0, paint);
    cache.recycle();
    view.destroyDrawingCache();

    return dest;
}

From source file:Main.java

public static Bitmap cropCenter(Bitmap bitmap, boolean recycle) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    if (width == height)
        return bitmap;
    int size = Math.min(width, height);

    Bitmap target = Bitmap.createBitmap(size, size, getConfig(bitmap));
    Canvas canvas = new Canvas(target);
    canvas.translate((size - width) / 2, (size - height) / 2);
    Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG);
    canvas.drawBitmap(bitmap, 0, 0, paint);
    if (recycle)// ww w .  j a v  a 2 s .  co  m
        bitmap.recycle();
    return target;
}

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