Example usage for android.graphics Canvas restore

List of usage examples for android.graphics Canvas restore

Introduction

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

Prototype

public void restore() 

Source Link

Document

This call balances a previous call to save(), and is used to remove all modifications to the matrix/clip state since the last save call.

Usage

From source file:Main.java

public static Bitmap addLabelToBitmap(Bitmap src, String label) {
    float densityFactor = Resources.getSystem().getDisplayMetrics().density;
    final float textPadding = src.getWidth() * 0.05f;

    Bitmap result = Bitmap.createBitmap(src.getWidth(), src.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(result);
    Paint textPaint = new Paint();
    textPaint.setAntiAlias(true);// www .jav  a  2s .  co  m
    textPaint.setTextSize(12 * densityFactor);
    textPaint.setColor(Color.WHITE);
    textPaint.setStrokeWidth(2 * densityFactor);
    textPaint.setShadowLayer(1 * densityFactor, 0, 0, Color.BLACK);

    float textWidth = textPaint.measureText(label);

    float scaleFactor = (src.getWidth() - textPadding * 2) / textWidth;

    canvas.drawBitmap(src, 0, 0, textPaint);

    canvas.save();
    canvas.scale(scaleFactor, scaleFactor);
    float textPosX = (src.getWidth() / scaleFactor - textWidth) / 2;
    float textPosY = (src.getHeight() - textPadding) / scaleFactor;

    canvas.drawText(label, textPosX, textPosY, textPaint);
    canvas.restore();
    return result;
}

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 ww w  . j  av  a2s.  com
    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: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);//w  w  w.  ja  va  2s.c o  m

    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

/**
 * Draw a straight line through the points
 *//*w w  w  .  ja  v  a2 s.co m*/
public 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:app.philm.in.view.BackdropImageView.java

@Override
protected void onDraw(Canvas canvas) {
    if (mOffset != 0) {
        canvas.save();//from   ww w  . j a va2 s . c o m
        canvas.translate(0f, mOffset);
        canvas.clipRect(0f, 0f, canvas.getWidth(), canvas.getHeight() + mOffset);
        super.onDraw(canvas);
        canvas.restore();
    } else {
        super.onDraw(canvas);
    }
}

From source file:com.dbeginc.dbweather.utils.animations.widgets.RainFallView.java

@Override
protected void onDraw(Canvas canvas) {
    for (int i = 0; i < mRainFlakeCount; i++) {
        Drawable drawable = drawables.get(i);
        canvas.save();/*  w  ww.  j a  va 2 s  . c o  m*/
        canvas.translate(coords[i][0], coords[i][1]);
        drawable.draw(canvas);
        canvas.restore();
    }
    invalidate();
}

From source file:com.dbeginc.dbweather.utils.animations.widgets.SnowFallView.java

@Override
protected void onDraw(Canvas canvas) {
    for (int i = 0; i < snow_flake_count; i++) {
        Drawable drawable = drawables.get(i);
        canvas.save();/*w w w  . jav  a  2s . c  o  m*/
        canvas.translate(coords[i][0], coords[i][1]);
        drawable.draw(canvas);
        canvas.restore();
    }
    invalidate();
}

From source file:com.sinenco.sharednews.listviewremovalanimation.BackgroundContainer.java

@Override
protected void onDraw(Canvas canvas) {
    if (mShowing) {
        if (mUpdateBounds) {
            mShadowedBackground.setBounds(0, 0, getWidth(), mOpenAreaHeight);
        }//from   w  w  w  . j  a v a  2s.c o  m
        canvas.save();
        canvas.translate(0, mOpenAreaTop);
        mShadowedBackground.draw(canvas);
        canvas.restore();
    }
}

From source file:im.ene.ribbon.FixedActionTabView.java

@Override
protected void onDraw(final Canvas canvas) {
    super.onDraw(canvas);

    canvas.save();/*from www. j a v  a2 s.c om*/
    canvas.translate(0, iconTranslation);
    icon.draw(canvas);
    drawBadge(canvas);
    canvas.restore();

    canvas.save();
    canvas.scale(canvasTextScale, canvasTextScale, textCenterX, textCenterY);
    canvas.drawText(getAction().getTitle(), textX, textY, textPaint);
    canvas.restore();
}

From source file:com.crs4.roodin.moduletester.CustomView.java

@Override
public void onDraw(Canvas canvas, Matrix matrix) {

    canvas.rotate(currentRotation, translateX, translateY);
    canvas.drawBitmap(blockImg, 0, 0, null);
    drawBarreds().draw(canvas);//from ww w . j  a v  a 2 s.c om
    drawRadar(canvas);

    canvas.restore();
}