Example usage for android.graphics Matrix postScale

List of usage examples for android.graphics Matrix postScale

Introduction

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

Prototype

public boolean postScale(float sx, float sy) 

Source Link

Document

Postconcats the matrix with the specified scale.

Usage

From source file:Main.java

public static Bitmap zoomBMP(Bitmap bitmap, int height, int width) {
    int w = bitmap.getWidth();
    int h = bitmap.getHeight() - 1;
    Matrix matrix = new Matrix();
    float scaleHeight = ((float) height / h);
    matrix.postScale(scaleHeight, scaleHeight);
    return Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);
}

From source file:Main.java

public static Bitmap getScaleImage(final Bitmap bitmap, final float boundBoxInDp) {
    // Get current dimensions
    final int width = bitmap.getWidth();
    final int height = bitmap.getHeight();
    // Determine how much to scale: the dimension requiring
    // less scaling is.
    // closer to the its side. This way the image always
    // stays inside your.
    // bounding box AND either x/y axis touches it.
    final float xScale = boundBoxInDp / width;
    final float yScale = boundBoxInDp / height;
    final float scale = xScale <= yScale ? xScale : yScale;
    // Create a matrix for the scaling and add the scaling data
    final Matrix matrix = new Matrix();
    matrix.postScale(scale, scale);
    // matrix.postRotate(rotate);
    // Create a new bitmap and convert it to a format understood
    // by the/*www .  j a v a 2  s.c o  m*/
    // ImageView
    final Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, false);
    // Apply the scaled bitmap
    return scaledBitmap;
}

From source file:Main.java

/**
 * get a screen shot with size : width X height.
 *//*from   w ww.  ja  v a 2s  . c  o m*/
public static Bitmap getScreenShot(Activity activity, int width, int height) {
    if (activity == null || width < 1 || height < 1) {
        return null;
    }
    Window window = activity.getWindow();
    if (window == null) {
        return null;
    }
    View decorView = window.getDecorView();
    if (decorView == null) {
        return null;
    }
    decorView.setDrawingCacheEnabled(true);
    Bitmap screenShot = decorView.getDrawingCache(true);
    if (screenShot == null) {
        return null;
    }
    Matrix matrix = new Matrix();
    matrix.postScale((float) width / screenShot.getWidth(), (float) height / screenShot.getHeight());
    Bitmap drawingCache = Bitmap.createBitmap(screenShot, 0, 0, screenShot.getWidth(), screenShot.getHeight(),
            matrix, true);
    decorView.destroyDrawingCache();
    screenShot.recycle();
    return drawingCache;
}

From source file:Main.java

private static Bitmap scaleBitmap(Bitmap bitmap, float ratio) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();

    Matrix matrix = new Matrix();
    matrix.postScale(1f / ratio, 1f / ratio);

    Bitmap result = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
    return result;
}

From source file:Main.java

/**
 * Scales a bitmap to fit required ratio
 *///from ww w . jav  a2 s  .co  m
@SuppressWarnings("unused")
private static Bitmap scaleImage(Context mContext, Bitmap bitmap, int reqWidth, int reqHeight) {

    // Get current dimensions AND the desired bounding box
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    int boundingX = dpToPx(mContext, reqWidth);
    int boundingY = dpToPx(mContext, reqHeight);

    // Determine how much to scale: the dimension requiring less scaling is
    // closer to the its side. This way the image always stays inside your
    // bounding box AND either x/y axis touches it.
    float xScale = ((float) boundingX) / width;
    float yScale = ((float) boundingY) / height;
    float scale = (xScale >= yScale) ? xScale : yScale;

    // Create a matrix for the scaling and add the scaling data
    Matrix matrix = new Matrix();
    matrix.postScale(scale, scale);

    // Create a new bitmap and convert it to a format understood by the
    // ImageView
    Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);

    return scaledBitmap;
}

From source file:Main.java

public static Bitmap getScaleImage(Bitmap bitmap, float boundBoxInDp) {

    // Get current dimensions
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();

    // Determine how much to scale: the dimension requiring
    // less scaling is.
    // closer to the its side. This way the image always
    // stays inside your.
    // bounding box AND either x/y axis touches it.
    float xScale = boundBoxInDp / width;
    float yScale = boundBoxInDp / height;
    float scale = xScale <= yScale ? xScale : yScale;

    // Create a matrix for the scaling and add the scaling data
    Matrix matrix = new Matrix();
    matrix.postScale(scale, scale);
    // matrix.postRotate(rotate);

    // Create a new bitmap and convert it to a format understood

    // by the//from  w  w w  .j a va  2  s .  co m
    // ImageView
    Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, false);

    // Apply the scaled bitmap
    return scaledBitmap;

}

From source file:Main.java

public static void scaleImageWithOriRatio(ImageView view, int boundBoxInDp) {
    // Get the ImageView and its bitmap
    Drawable drawing = view.getDrawable();
    Bitmap bitmap = ((BitmapDrawable) drawing).getBitmap();

    // Get current dimensions
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();

    // Determine how much to scale: the dimension requiring less scaling is
    // closer to the its side. This way the image always stays inside your
    // bounding box AND either x/y axis touches it.
    float xScale = ((float) boundBoxInDp) / width;
    float yScale = ((float) boundBoxInDp) / height;
    float scale = (xScale <= yScale) ? xScale : yScale;

    // Create a matrix for the scaling and add the scaling data
    Matrix matrix = new Matrix();
    matrix.postScale(scale, scale);

    // Create a new bitmap and convert it to a format understood by the
    // ImageView/*from  w  w  w  .j  a v  a  2  s  .c  om*/
    Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
    BitmapDrawable result = new BitmapDrawable(scaledBitmap);
    width = scaledBitmap.getWidth();
    height = scaledBitmap.getHeight();

    // Apply the scaled bitmap
    view.setImageDrawable(result);

    // Now change ImageView's dimensions to match the scaled image
    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();
    params.width = width;
    params.height = height;
    view.setLayoutParams(params);
}

From source file:Main.java

public static Bitmap getDrawable(String path, int zoom, int mItemwidth, int mItemHerght) {

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;/* ww  w .  ja  v  a2s .  com*/
    BitmapFactory.decodeFile(path, options);
    int mWidth = options.outWidth;
    int mHeight = options.outHeight;
    int s = 1;
    while ((mWidth / s > mItemwidth * 2 * zoom) || (mHeight / s > mItemHerght * 2 * zoom)) {
        s *= 2;
    }

    options = new BitmapFactory.Options();
    options.inPreferredConfig = Config.ARGB_8888;
    options.inSampleSize = s;
    Bitmap bm = BitmapFactory.decodeFile(path, options);

    if (bm != null) {
        int h = bm.getHeight();
        int w = bm.getWidth();

        float ft = (float) ((float) w / (float) h);
        float fs = (float) ((float) mItemwidth / (float) mItemHerght);

        int neww = ft >= fs ? mItemwidth * zoom : (int) (mItemHerght * zoom * ft);
        int newh = ft >= fs ? (int) (mItemwidth * zoom / ft) : mItemHerght * zoom;

        float scaleWidth = ((float) neww) / w;
        float scaleHeight = ((float) newh) / h;

        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        bm = Bitmap.createBitmap(bm, 0, 0, w, h, matrix, true);
        return bm;
    }
    return null;
}

From source file:Main.java

/**
 * create scaled bitmap with required width and height
 *
 * @param srcBitmap//w ww  .  j ava 2s. c  o m
 * @param reqWidth
 * @param reqHeight
 * @param recycleOrig
 * @param scaleType
 * @return
 */
public synchronized static Bitmap createBitmap(Bitmap srcBitmap, int reqWidth, int reqHeight,
        boolean recycleOrig, int scaleType) {
    int bitmapWidth = srcBitmap.getWidth();
    int bitmapHeight = srcBitmap.getHeight();
    if (reqWidth == 0)
        reqWidth = bitmapWidth;
    if (reqHeight == 0)
        reqHeight = bitmapHeight;

    //        final Rect srcRect = new Rect(0, 0, srcBitmap.getWidth(), srcBitmap.getHeight());
    //        final Rect dstRect = new Rect(0, 0, reqWidth, reqHeight);
    float scaleWidth = 1;
    float scaleHeight = 1;
    if (scaleType == SCALE_TYPE_FIT_START) {
        scaleWidth = (reqWidth / bitmapWidth < reqHeight / bitmapHeight)
                ? (float) reqWidth / (float) bitmapWidth
                : (float) reqHeight / (float) bitmapHeight;
        scaleHeight = scaleWidth;
    } else if (scaleType == SCALE_TYPE_FIT_XY) {
        scaleWidth = (float) reqWidth / (float) bitmapWidth;
        scaleHeight = (float) reqHeight / (float) bitmapHeight;
    }

    Matrix matrix = new Matrix();
    matrix.postScale(scaleWidth, scaleHeight);

    Bitmap resizedBitmap = Bitmap.createBitmap(srcBitmap, 0, 0, bitmapWidth, bitmapHeight, matrix, true);

    if (recycleOrig) {
        srcBitmap.recycle();
    }

    return resizedBitmap;
}

From source file:Main.java

public static Bitmap ResizeBitmap(Bitmap bitmap, int newWidth) {

    int width = bitmap.getWidth();

    int height = bitmap.getHeight();

    float temp = ((float) height) / ((float) width);

    int newHeight = (int) ((newWidth) * temp);

    float scaleWidth = ((float) newWidth) / width;

    float scaleHeight = ((float) newHeight) / height;

    Matrix matrix = new Matrix();

    // resize the bit map

    matrix.postScale(scaleWidth, scaleHeight);

    //         matrix.postRotate(90);

    Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);

    bitmap.recycle();// w  w  w  .  j  a va 2s . com

    return resizedBitmap;

}