Example usage for android.graphics Matrix setScale

List of usage examples for android.graphics Matrix setScale

Introduction

In this page you can find the example usage for android.graphics Matrix setScale.

Prototype

public void setScale(float sx, float sy, float px, float py) 

Source Link

Document

Set the matrix to scale by sx and sy, with a pivot point at (px, py).

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);/* w w w . j  av  a  2s .  co  m*/
    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  av  a2s.  c om*/
 */
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  a  v a2  s.  co  m*/
    /**
     * 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;
    }//ww w  .j ava  2s  . c  o  m

    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:chrisrenke.drawerarrowdrawable.DrawerArrowDrawable.java

/**
 * Scales the paths to the given screen density. If the density matches the
 * {@link DrawerArrowDrawable#PATH_GEN_DENSITY}, no scaling needs to be done.
 *//*  ww  w  . jav  a  2s .  c om*/
private static void scalePath(Path path, float density) {
    if (density == PATH_GEN_DENSITY)
        return;
    Matrix scaleMatrix = new Matrix();
    scaleMatrix.setScale(density / PATH_GEN_DENSITY, density / PATH_GEN_DENSITY, 0, 0);
    path.transform(scaleMatrix);
}

From source file:com.flyn.smartandroid.views.DrawerArrowDrawable.java

/**
 * Scales the paths to the given screen density. If the density matches the
 * {@link DrawerArrowDrawable#PATH_GEN_DENSITY}, no scaling needs to be done.
 *///from  ww w .j a v a  2s.  c o m
private static void scalePath(Path path, float density) {
    if (density == PATH_GEN_DENSITY) {
        return;
    }
    Matrix scaleMatrix = new Matrix();
    scaleMatrix.setScale(density / PATH_GEN_DENSITY, density / PATH_GEN_DENSITY, 0, 0);
    path.transform(scaleMatrix);
}

From source file:com.github.jokar.rxupload.widget.ProgressDownloadView.java

/**
 * MARK: Overrides//from   ww  w .  j  ava 2 s.  c o  m
 */

@Override
protected void onDraw(Canvas canvas) {
    if (mPathWhite != null && mPathBlack != null) {

        float textX = Math.max(getPaddingLeft() - (int) (mBubbleWidth / 4.0f),
                mProgress * mWidth / 100 - (int) (mBubbleWidth / 4.0f));
        float textY = mHeight / 2 - mBubbleHeight / 2 + calculateDeltaY();

        switch (mState) {
        case STATE_WORKING:
            // Save and restore prevent the rest of the canvas to not be rotated
            canvas.save();
            float speed = (getProgress() - mTarget) / 20;
            mBubbleAngle += speed * 10;
            if (mBubbleAngle > 20) {
                mBubbleAngle = 20;
            }
            if (mBubbleAngle < -20) {
                mBubbleAngle = -20;
            }
            if (Math.abs(speed) < 1) {
                mSpeedAngle -= mBubbleAngle / 20;
                mSpeedAngle *= .9f;
            }
            mBubbleAngle += mSpeedAngle;

            canvas.rotate(mBubbleAngle, bubbleAnchorX, bubbleAnchorY);
            canvas.drawPath(mPathBubble, mPaintBubble);
            canvas.drawText(String.valueOf((int) mProgress) + " %", textX, textY, mPaintText);
            canvas.restore();
            break;
        case STATE_FAILED:
            canvas.save();
            canvas.rotate(mFailAngle, bubbleAnchorX, bubbleAnchorY);
            canvas.drawPath(mPathBubble, mPaintBubble);
            canvas.rotate(mFailAngle, bubbleAnchorX, textY - mBubbleHeight / 7);
            //                    mPaintText.setColor(getResources().getColor(R.color.red_wine));
            textX = Math.max(getPaddingLeft() - (int) (mBubbleWidth / 3.2f),
                    mProgress * mWidth / 100 - (int) (mBubbleWidth / 3.2f));
            canvas.drawText(getResources().getString(R.string.failed), textX, textY, mPaintText);
            canvas.restore();
            break;
        case STATE_SUCCESS:
            canvas.save();
            //                    mPaintText.setColor(getResources().getColor(R.color.green_grass));
            textX = Math.max(getPaddingLeft() - (int) (mBubbleWidth / 3.2f),
                    mProgress * mWidth / 100 - (int) (mBubbleWidth / 3.2f));
            Matrix flipMatrix = new Matrix();
            flipMatrix.setScale(mFlipFactor, 1, bubbleAnchorX, bubbleAnchorY);
            canvas.concat(flipMatrix);
            canvas.drawPath(mPathBubble, mPaintBubble);
            canvas.concat(flipMatrix);
            canvas.drawText(getResources().getString(R.string.done), textX, textY, mPaintText);
            canvas.restore();
            break;
        }

        canvas.drawPath(mPathBlack, mPaintBlack);
        canvas.drawPath(mPathWhite, mPaintWhite);
    }
}

From source file:com.jrummyapps.android.widget.AnimatedSvgView.java

/**
 * If you set the SVG data paths more than once using {@link #setGlyphStrings(String...)} you should call this method
 * before playing the animation.//from w  w w  .j  a  va 2  s . c  o m
 */
@SuppressWarnings("SuspiciousNameCombination")
public void rebuildGlyphData() {

    float X = mWidth / mViewport.x;
    float Y = mHeight / mViewport.y;

    Matrix scaleMatrix = new Matrix();
    RectF outerRect = new RectF(X, X, Y, Y);
    scaleMatrix.setScale(X, Y, outerRect.centerX(), outerRect.centerY());

    mGlyphData = new GlyphData[mGlyphStrings.length];
    for (int i = 0; i < mGlyphStrings.length; i++) {
        mGlyphData[i] = new GlyphData();
        try {
            mGlyphData[i].path = ExposedPathParser.createPathFromPathData(mGlyphStrings[i]);
            mGlyphData[i].path.transform(scaleMatrix);
        } catch (Exception e) {
            mGlyphData[i].path = new Path();
            Log.e(TAG, "Couldn't parse path", e);
        }
        PathMeasure pm = new PathMeasure(mGlyphData[i].path, true);
        while (true) {
            mGlyphData[i].length = Math.max(mGlyphData[i].length, pm.getLength());
            if (!pm.nextContour()) {
                break;
            }
        }
        mGlyphData[i].paint = new Paint();
        mGlyphData[i].paint.setStyle(Paint.Style.STROKE);
        mGlyphData[i].paint.setAntiAlias(true);
        mGlyphData[i].paint.setColor(Color.WHITE);
        mGlyphData[i].paint.setStrokeWidth(
                TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, getResources().getDisplayMetrics()));
    }
}

From source file:org.egov.android.view.activity.CreateComplaintActivity.java

public String compressImage(String fromfilepath, String tofilepath) {

    Bitmap scaledBitmap = null;/*  w  ww .j  a  v a 2  s  .  c o m*/

    BitmapFactory.Options options = new BitmapFactory.Options();

    //      by setting this field as true, the actual bitmap pixels are not loaded in the memory. Just the bounds are loaded. If
    //      you try the use the bitmap here, you will get null.
    options.inJustDecodeBounds = true;
    Bitmap bmp = BitmapFactory.decodeFile(fromfilepath, options);

    int actualHeight = options.outHeight;
    int actualWidth = options.outWidth;

    //      max Height and width values of the compressed image is taken as 816x612

    float maxHeight = 816.0f;
    float maxWidth = 612.0f;
    float imgRatio = actualWidth / actualHeight;
    float maxRatio = maxWidth / maxHeight;

    //      width and height values are set maintaining the aspect ratio of the image

    if (actualHeight > maxHeight || actualWidth > maxWidth) {
        if (imgRatio < maxRatio) {
            imgRatio = maxHeight / actualHeight;
            actualWidth = (int) (imgRatio * actualWidth);
            actualHeight = (int) maxHeight;
        } else if (imgRatio > maxRatio) {
            imgRatio = maxWidth / actualWidth;
            actualHeight = (int) (imgRatio * actualHeight);
            actualWidth = (int) maxWidth;
        } else {
            actualHeight = (int) maxHeight;
            actualWidth = (int) maxWidth;

        }
    }

    //      setting inSampleSize value allows to load a scaled down version of the original image

    options.inSampleSize = calculateInSampleSize(options, actualWidth, actualHeight);

    //      inJustDecodeBounds set to false to load the actual bitmap
    options.inJustDecodeBounds = false;

    //      this options allow android to claim the bitmap memory if it runs low on memory
    options.inPurgeable = true;
    options.inInputShareable = true;
    options.inTempStorage = new byte[16 * 1024];

    try {
        //          load the bitmap from its path
        bmp = BitmapFactory.decodeFile(tofilepath, options);
    } catch (OutOfMemoryError exception) {
        exception.printStackTrace();
    }
    try {
        scaledBitmap = Bitmap.createBitmap(actualWidth, actualHeight, Bitmap.Config.ARGB_8888);
    } catch (OutOfMemoryError exception) {
        exception.printStackTrace();
    }

    float ratioX = actualWidth / (float) options.outWidth;
    float ratioY = actualHeight / (float) options.outHeight;
    float middleX = actualWidth / 2.0f;
    float middleY = actualHeight / 2.0f;

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

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

    String attrLatitute = null;
    String attrLatituteRef = null;
    String attrLONGITUDE = null;
    String attrLONGITUDEREf = null;

    //      check the rotation of the image and display it properly
    ExifInterface exif;
    try {
        exif = new ExifInterface(fromfilepath);

        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 0);
        Log.d("EXIF", "Exif: " + orientation);
        Matrix matrix = new Matrix();
        if (orientation == 6) {
            matrix.postRotate(90);
            Log.d("EXIF", "Exif: " + orientation);
        } else if (orientation == 3) {
            matrix.postRotate(180);
            Log.d("EXIF", "Exif: " + orientation);
        } else if (orientation == 8) {
            matrix.postRotate(270);
            Log.d("EXIF", "Exif: " + orientation);
        }

        attrLatitute = exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE);
        attrLatituteRef = exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE_REF);
        attrLONGITUDE = exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE);
        attrLONGITUDEREf = exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF);

        scaledBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0, scaledBitmap.getWidth(),
                scaledBitmap.getHeight(), matrix, true);
    } catch (IOException e) {
        e.printStackTrace();
    }

    FileOutputStream out = null;
    try {
        out = new FileOutputStream(tofilepath);

        //          write the compressed bitmap at the destination specified by filename.
        scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 80, out);

        ExifInterface exif2 = new ExifInterface(tofilepath);

        if (attrLatitute != null) {
            exif2.setAttribute(ExifInterface.TAG_GPS_LATITUDE, attrLatitute);
            exif2.setAttribute(ExifInterface.TAG_GPS_LONGITUDE, attrLONGITUDE);
        }

        if (attrLatituteRef != null) {
            exif2.setAttribute(ExifInterface.TAG_GPS_LATITUDE_REF, attrLatituteRef);
            exif2.setAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF, attrLONGITUDEREf);
        }
        exif2.saveAttributes();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return tofilepath;
}