Example usage for android.graphics Canvas save

List of usage examples for android.graphics Canvas save

Introduction

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

Prototype

public int save() 

Source Link

Document

Saves the current matrix and clip onto a private stack.

Usage

From source file:Main.java

public static Drawable getRotateDrawable(Resources res, final Bitmap bitmap, final float angle) {
    final BitmapDrawable drawable = new BitmapDrawable(res, bitmap) {
        @Override//from ww  w.  j  av a  2s  . c o  m
        public void draw(final Canvas canvas) {
            canvas.save();
            canvas.rotate(angle, bitmap.getWidth() / 2, bitmap.getHeight() / 2);
            super.draw(canvas);
            canvas.restore();
        }
    };
    return drawable;
}

From source file:Main.java

/**
 * Draw a straight line through the points.
 *///from  ww w. j av  a 2s. c o m
public static void drawLine(Point p1, Point p2, Canvas canvas, Paint paint) {
    canvas.save();
    canvas.drawLine(p1.x, p1.y, p2.x, p2.y, paint);
    canvas.restore();
}

From source file:Main.java

/**
 * Draw a straight line through the points
 *//*from   w ww  . j  a  v a 2s  .  c  o m*/
public static void drawLine(PointF p1, PointF p2, Canvas canvas, Paint paint) {
    canvas.save();
    canvas.drawLine(p1.x, p1.y, p2.x, p2.y, paint);
    canvas.restore();
}

From source file:Main.java

public static Bitmap getWebViewShopshat(WebView webView) {
    webView.setDrawingCacheEnabled(true);
    webView.buildDrawingCache();// ww  w.j  a v  a  2  s .  c  o  m
    Picture snapShot = webView.capturePicture();
    Bitmap bitmap = Bitmap.createBitmap(snapShot.getWidth(), snapShot.getHeight(), Bitmap.Config.ARGB_8888);
    //bitmap.eraseColor(Color.WHITE);

    Canvas c = new Canvas(bitmap);
    int state = c.save();
    webView.draw(c);
    //c.restoreToCount(state);
    c.restore();
    webView.destroyDrawingCache();

    return bitmap;
}

From source file:Main.java

public static Bitmap overlay(Bitmap b, int x, int y) {
    Bitmap empty = Bitmap.createBitmap(x, y, Bitmap.Config.ARGB_4444);
    Canvas cv = new Canvas(empty);
    cv.drawBitmap(b, 0, 0, null);//w  ww.  j ava 2 s  . c om
    cv.save();
    return empty;
}

From source file:Main.java

public static void drawImage(Canvas canvas, Paint paint, Bitmap bitmap, float x, float y, float r) {
    canvas.save();
    Path path = new Path();
    path.addCircle(x, y, r, Path.Direction.CCW);
    canvas.clipPath(path);//from  www .java 2 s  .c  om
    canvas.drawBitmap(bitmap, x - r, y - r, paint);
    canvas.restore();
}

From source file:Main.java

/**
 * Draws a string that is aligned by one anchor point and rotated about
 * another anchor point./*from   w  w  w  .j a  va  2  s  .c  o  m*/
 * 
 * @param text
 *            the text.
 * @param g2
 *            the graphics device.
 * @param x
 *            the location of the text anchor.
 * @param y
 *            the location of the text anchor.
 * @param textAnchor
 *            the text anchor.
 * @param rotationX
 *            the x-coordinate for the rotation anchor point.
 * @param rotationY
 *            the y-coordinate for the rotation anchor point.
 * @param angle
 *            the rotation angle.
 */
public static void drawRotatedString(final String text, final Canvas g2, final float x, final float y,
        final Paint paint, float angle) {

    //g2.drawCircle(x, y, 2, paint);
    g2.save();
    if (angle == 0.0)
        g2.drawText(text, x, y, paint);
    else {
        g2.rotate((float) Math.toDegrees(angle), x, y);
        g2.drawText(text, x, y, paint);
    }
    g2.restore();
}

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);//  www .  j a va  2s  .  com

    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;
}

From source file:Main.java

public static void fillRect(Canvas canvas, int left, int top, int right, int bottom, boolean drawBorder,
        int fillColor, int borderColor, float borderSize) {
    if (borderSize == 0)
        borderSize = 0.5f;/*from w ww  .  j a v a  2s.  c  o  m*/
    paint.setAntiAlias(true);
    paint.setStyle(Paint.Style.FILL);
    canvas.save();
    canvas.clipRect(new Rect(left, top, right, bottom));
    if (drawBorder) {
        paint.setColor(borderColor);
        canvas.drawRect(left, top, right, bottom, paint);
        paint.setColor(fillColor);
        canvas.drawRect(left + borderSize, top + borderSize, right - (borderSize * 2),
                bottom - (borderSize * 2), paint);
    } else {
        paint.setColor(fillColor);
        canvas.drawRect(left, top, right, bottom, paint);
    }
    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;
    }/*ww  w . j  av a  2  s .  c o  m*/

    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;
}