Example usage for android.graphics Canvas setMatrix

List of usage examples for android.graphics Canvas setMatrix

Introduction

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

Prototype

public void setMatrix(@Nullable Matrix matrix) 

Source Link

Document

Completely replace the current matrix with the specified matrix.

Usage

From source file:Main.java

public static Bitmap BITMAP_RESIZER(Bitmap bitmap, int newWidth, int newHeight) {
    Bitmap scaledBitmap = Bitmap.createBitmap(newWidth, newHeight, Bitmap.Config.ARGB_8888);

    float ratioX = newWidth / (float) bitmap.getWidth();
    float ratioY = newHeight / (float) bitmap.getHeight();
    float middleX = newWidth / 2.0f;
    float middleY = newHeight / 2.0f;

    Matrix scaleMatrix = new Matrix();
    scaleMatrix.setScale(ratioX, ratioY, middleX, middleY);

    Canvas canvas = new Canvas(scaledBitmap);
    canvas.setMatrix(scaleMatrix);
    canvas.drawBitmap(bitmap, middleX - bitmap.getWidth() / 2, middleY - bitmap.getHeight() / 2,
            new Paint(Paint.FILTER_BITMAP_FLAG));

    return scaledBitmap;

}

From source file:Main.java

/**
 * Scales the provided bitmap to the height and width provided (using antialiasing).
 * (Alternative method for scaling bitmaps since Bitmap.createScaledBitmap(...) produces low quality bitmaps.)
 *
 * @param bitmap    is the bitmap to scale.
 * @param newWidth  is the desired width of the scaled bitmap.
 * @param newHeight is the desired height of the scaled bitmap.
 * @return the scaled bitmap./*from w w w. j a v a2 s .c o  m*/
 */
public static Bitmap scaleBitmap(Bitmap bitmap, int newWidth, int newHeight) {
    Bitmap scaledBitmap = Bitmap.createBitmap(newWidth, newHeight, Bitmap.Config.ARGB_8888);

    float scaleX = newWidth / (float) bitmap.getWidth();
    float scaleY = newHeight / (float) bitmap.getHeight();
    float pivotX = 0;
    float pivotY = 0;

    Matrix scaleMatrix = new Matrix();
    scaleMatrix.setScale(scaleX, scaleY, pivotX, pivotY);

    Canvas canvas = new Canvas(scaledBitmap);
    canvas.setMatrix(scaleMatrix);
    canvas.drawBitmap(bitmap, 0, 0, new Paint(Paint.FILTER_BITMAP_FLAG | Paint.ANTI_ALIAS_FLAG));

    return scaledBitmap;
}

From source file:Main.java

public static Bitmap resizeBitmap(Bitmap bitmap, int newWidth, int newHeight) {
    if (bitmap == null) {
        return null;
    }//from   w  w w .j av a2  s  . c  om
    /**
     * http://stackoverflow.com/questions/4821488/bad-image-quality-after-resizing-scaling-bitmap#7468636
     */
    Bitmap scaledBitmap = Bitmap.createBitmap(newWidth, newHeight, Config.ARGB_8888);

    float ratioX = newWidth / (float) bitmap.getWidth();
    float ratioY = newHeight / (float) bitmap.getHeight();
    float middleX = newWidth / 2.0f;
    float middleY = newHeight / 2.0f;

    Matrix scaleMatrix = new Matrix();
    scaleMatrix.setScale(ratioX, ratioY, middleX, middleY);

    Canvas canvas = new Canvas(scaledBitmap);
    canvas.setMatrix(scaleMatrix);
    canvas.drawBitmap(bitmap, middleX - bitmap.getWidth() / 2, middleY - bitmap.getHeight() / 2,
            new Paint(Paint.FILTER_BITMAP_FLAG));

    return scaledBitmap;
}

From source file:Main.java

public static Bitmap scaleBitmapForDevice(Bitmap bitmap) {
    if (bitmap == null) {
        return null;
    }//from   ww w  .jav  a2s  . com

    float density = Resources.getSystem().getDisplayMetrics().density;
    int newWidth = (int) (bitmap.getWidth() * density);
    int newHeight = (int) (bitmap.getHeight() * density);
    /*
    Bitmap resizeBitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);
     */
    /**
     * http://stackoverflow.com/questions/4821488/bad-image-quality-after-resizing-scaling-bitmap#7468636
     */
    Bitmap scaledBitmap = Bitmap.createBitmap(newWidth, newHeight, Config.ARGB_8888);

    float ratioX = newWidth / (float) bitmap.getWidth();
    float ratioY = newHeight / (float) bitmap.getHeight();
    float middleX = newWidth / 2.0f;
    float middleY = newHeight / 2.0f;

    Matrix scaleMatrix = new Matrix();
    scaleMatrix.setScale(ratioX, ratioY, middleX, middleY);

    Canvas canvas = new Canvas(scaledBitmap);
    canvas.setMatrix(scaleMatrix);
    canvas.drawBitmap(bitmap, middleX - bitmap.getWidth() / 2, middleY - bitmap.getHeight() / 2,
            new Paint(Paint.FILTER_BITMAP_FLAG));
    bitmap.recycle();

    return scaledBitmap;
}

From source file:Main.java

public static Bitmap createCircularClip(Bitmap input, int width, int height) {
    if (input == null)
        return null;

    final int inWidth = input.getWidth();
    final int inHeight = input.getHeight();
    final Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(output);
    final Paint paint = new Paint();
    paint.setShader(new BitmapShader(input, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
    paint.setAntiAlias(true);/*from  w  ww.  j  a  v a  2s  .c o  m*/
    final RectF srcRect = new RectF(0, 0, inWidth, inHeight);
    final RectF dstRect = new RectF(0, 0, width, height);
    final Matrix m = new Matrix();
    m.setRectToRect(srcRect, dstRect, Matrix.ScaleToFit.CENTER);
    canvas.setMatrix(m);
    canvas.drawCircle(inWidth / 2, inHeight / 2, inWidth / 2, paint);
    return output;
}

From source file:com.google.android.marvin.utils.HighlightBoundsView.java

@Override
public void onDraw(Canvas c) {
    final int saveCount = c.save();
    c.translate(-SCREEN_LOCATION[0], -SCREEN_LOCATION[1]);
    c.setMatrix(mMatrix);

    mPaint.setColor(mHighlightColor);/*from w ww .j  a v a  2s. co m*/

    for (AccessibilityNodeInfoCompat node : mNodes) {
        node.getBoundsInScreen(mTemp);
        c.drawRect(mTemp, mPaint);
    }

    c.restoreToCount(saveCount);
}

From source file:com.nextgis.maplibui.formcontrol.Sign.java

public void save(int width, int height, boolean transparentBackground, File sigFile) throws IOException {
    if (mNotInitialized)
        return;/*ww w  .j  av  a2 s  .  c  om*/

    float scale = Math.min((float) width / getWidth(), (float) height / getHeight());
    Matrix matrix = new Matrix();
    matrix.setScale(scale, scale);

    Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bmp);
    canvas.setMatrix(matrix);

    int color = transparentBackground ? Color.TRANSPARENT : 0xFFFFFF - mPaint.getColor();
    drawSign(canvas, color, mPaint);
    if (sigFile.exists() || sigFile.createNewFile()) {
        FileOutputStream out = new FileOutputStream(sigFile);
        bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
    }
}

From source file:android.support.transition.GhostViewApi14.java

@Override
protected void onDraw(Canvas canvas) {
    // Apply the matrix while adjusting the coordinates
    mMatrix.set(mCurrentMatrix);/*from w  ww.j av  a 2 s .  c  o m*/
    mMatrix.postTranslate(mDeltaX, mDeltaY);
    canvas.setMatrix(mMatrix);
    // Draw the target
    mView.draw(canvas);
}

From source file:org.uoyabause.android.PadButton.java

@Override
public void onDraw(Canvas canvas) {

    if (bitmap_pad_left == null || bitmap_pad_right == null) {
        return;// w  w w.  j a  v a2  s  .  co m
    }

    mPaint.setAlpha((int) (255.0f * _transparent));

    canvas.drawBitmap(bitmap_pad_left, matrix_left, mPaint);
    canvas.drawBitmap(bitmap_pad_right, matrix_right, mPaint);

    canvas.setMatrix(null);

    //canvas.save();
    //canvas.concat(matrix_left);
    //buttons[PadEvent.BUTTON_UP].draw(canvas, paint, apaint, tpaint);
    //buttons[PadEvent.BUTTON_DOWN].draw(canvas, paint, apaint, tpaint);
    //buttons[PadEvent.BUTTON_LEFT].draw(canvas, paint, apaint, tpaint);
    //buttons[PadEvent.BUTTON_RIGHT].draw(canvas, paint, apaint, tpaint);
    //buttons[PadEvent.BUTTON_START].draw(canvas, paint, apaint, tpaint);
    //buttons[PadEvent.BUTTON_LEFT_TRIGGER].draw(canvas, paint, apaint, tpaint);

    //canvas.restore();
    //canvas.concat(matrix_right);
    //buttons[PadEvent.BUTTON_A].draw(canvas, paint, apaint, tpaint);
    //buttons[PadEvent.BUTTON_B].draw(canvas, paint, apaint, tpaint);
    //buttons[PadEvent.BUTTON_C].draw(canvas, paint, apaint, tpaint);
    //buttons[PadEvent.BUTTON_X].draw(canvas, paint, apaint, tpaint);
    //buttons[PadEvent.BUTTON_Y].draw(canvas, paint, apaint, tpaint);
    //buttons[PadEvent.BUTTON_Z].draw(canvas, paint, apaint, tpaint);
    //buttons[PadEvent.BUTTON_RIGHT_TRIGGER].draw(canvas, paint, apaint, tpaint);

    if (_pad_mode == 1) {
        _analog_pad.draw(canvas, _axi_x, _axi_y, paint, apaint, tpaint);
    }
}

From source file:com.codegarden.nativenavigation.JuceActivity.java

public final int[] renderGlyph(char glyph, Paint paint, android.graphics.Matrix matrix, Rect bounds) {
    Path p = new Path();
    paint.getTextPath(String.valueOf(glyph), 0, 1, 0.0f, 0.0f, p);

    RectF boundsF = new RectF();
    p.computeBounds(boundsF, true);//from w w  w .  j av a  2  s  . c  om
    matrix.mapRect(boundsF);

    boundsF.roundOut(bounds);
    bounds.left--;
    bounds.right++;

    final int w = bounds.width();
    final int h = Math.max(1, bounds.height());

    Bitmap bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);

    Canvas c = new Canvas(bm);
    matrix.postTranslate(-bounds.left, -bounds.top);
    c.setMatrix(matrix);
    c.drawPath(p, paint);

    final int sizeNeeded = w * h;
    if (cachedRenderArray.length < sizeNeeded)
        cachedRenderArray = new int[sizeNeeded];

    bm.getPixels(cachedRenderArray, 0, w, 0, 0, w, h);
    bm.recycle();
    return cachedRenderArray;
}