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:net.gsantner.opoc.util.ContextUtils.java

/**
 * Scale the bitmap so both dimensions are lower or equal to {@code maxDimen}
 * This keeps the aspect ratio//from   w  w w .ja va2  s .c  o  m
 */
public Bitmap scaleBitmap(Bitmap bitmap, int maxDimen) {
    int picSize = Math.min(bitmap.getHeight(), bitmap.getWidth());
    float scale = 1.f * maxDimen / picSize;
    Matrix matrix = new Matrix();
    matrix.postScale(scale, scale);
    return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}

From source file:com.landenlabs.all_UiDemo.frag.ImageScalesFrag.java

private Bitmap fillLowerLeft(final Drawable drawable, ImageView imageView, Bitmap prevBm) {

    // Compute  matrix to fill viewer with drawable starting with upper left
    // Stretching till 2nd edge is crossed (filling screen) with correct aspect ratio.
    Matrix m = new Matrix();
    m.reset();/*from  ww  w  .  j  av a 2 s . c o m*/
    int imgWidth = drawable.getIntrinsicWidth();
    int imgHeight = drawable.getIntrinsicHeight();
    int viewWidth = imageView.getWidth();
    int viewHeight = imageView.getHeight();
    float xScale = (float) viewWidth / imgWidth;
    float yScale = (float) viewHeight / imgHeight;
    float maxScale = Math.max(xScale, yScale);

    float dy = (imgHeight * maxScale - viewHeight) / maxScale;
    // dy = imgHeight / 2;
    m.preTranslate(0, -dy);
    m.postScale(maxScale, maxScale);

    imageView.setScaleType(ImageView.ScaleType.MATRIX);
    imageView.setImageMatrix(m);
    imageView.setImageDrawable(drawable);
    return prevBm;
}

From source file:com.klinker.android.twitter.activities.compose.Compose.java

public static Bitmap rotateBitmap(Bitmap bitmap, int orientation) {

    Log.v("talon_composing_image", "rotation: " + orientation);

    try {/*w w w  . ja va 2  s  . co  m*/
        Matrix matrix = new Matrix();
        switch (orientation) {
        case ExifInterface.ORIENTATION_NORMAL:
            return bitmap;
        case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
            matrix.setScale(-1, 1);
            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
            matrix.setRotate(180);
            break;
        case ExifInterface.ORIENTATION_FLIP_VERTICAL:
            matrix.setRotate(180);
            matrix.postScale(-1, 1);
            break;
        case ExifInterface.ORIENTATION_TRANSPOSE:
            matrix.setRotate(90);
            matrix.postScale(-1, 1);
            break;
        case ExifInterface.ORIENTATION_ROTATE_90:
            matrix.setRotate(90);
            break;
        case ExifInterface.ORIENTATION_TRANSVERSE:
            matrix.setRotate(-90);
            matrix.postScale(-1, 1);
            break;
        case ExifInterface.ORIENTATION_ROTATE_270:
            matrix.setRotate(-90);
            break;
        default:
            return bitmap;
        }
        try {
            Bitmap bmRotated = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix,
                    true);
            bitmap.recycle();
            return bmRotated;
        } catch (OutOfMemoryError e) {
            e.printStackTrace();
            return null;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return bitmap;
}

From source file:com.grass.caishi.cc.activity.AdAddActivity.java

public void addToListImage(final String path) {
    if (path != null) {
        dialog.setMessage("...");
        dialog.show();/*from w w w. ja v  a  2s.  com*/
        new Thread(new Runnable() {
            @Override
            public void run() {
                // TODO Auto-generated method stub
                File file = new File(path);
                imageUrl = path;
                FileInputStream fis = null;
                try {
                    int quality = 90;
                    fis = new FileInputStream(file);
                    int size = fis.available() / 1024;
                    if (size > 150) {
                        BitmapFactory.Options opts = new BitmapFactory.Options();
                        opts.inJustDecodeBounds = true;
                        resizeBmp = BitmapFactory.decodeFile(path, opts);

                        int pic_width = opts.outWidth;
                        int pic_height = opts.outHeight;
                        int be = 1;
                        if (pic_height > pic_width && pic_width > 1500) {
                            be = (int) Math.rint(pic_width / 1440.0);
                        } else if (pic_height < pic_width && pic_height > 2600) {
                            be = (int) Math.rint(pic_height / 2560.0);
                        }
                        if (be <= 0)
                            be = 1;
                        opts.inSampleSize = be;// 
                        opts.inJustDecodeBounds = false;
                        resizeBmp = BitmapFactory.decodeFile(path, opts);
                        pic_width = opts.outWidth;
                        pic_height = opts.outHeight;
                        File dirFile = new File(Environment.getExternalStorageDirectory(),
                                Constant.CACHE_DIR_IMAGE);
                        if (!dirFile.exists()) {
                            dirFile.mkdirs();
                        }
                        int drive_width = MetricsUnit.getWidth(AdAddActivity.this);
                        int drive_height = MetricsUnit.getHeight(AdAddActivity.this);

                        float my_width = (float) drive_width;
                        float my_height = (float) drive_height;

                        double doo = 0.0;
                        if (pic_width > my_width) {
                            doo = (double) my_width / (double) pic_width;
                            my_height = (int) (pic_height * doo);
                        }

                        int c_h = 0;

                        if (my_height > drive_height) {
                            c_h = (int) ((pic_height - my_height) / 2);
                            if (c_h < 0) {
                                c_h = 0;
                            }
                        }
                        Matrix matrix = new Matrix();
                        matrix.postScale(my_width / pic_width, my_height / pic_height);

                        resizeBmp = Bitmap.createBitmap(resizeBmp, 0, c_h, pic_width, pic_height, matrix, true);
                        File jpegTrueFile = new File(dirFile, "img_" + System.currentTimeMillis() + ".jpg");
                        FileOutputStream fileOutputStream = new FileOutputStream(jpegTrueFile);

                        resizeBmp.compress(CompressFormat.JPEG, quality, fileOutputStream);
                        imageUrl = jpegTrueFile.getAbsolutePath();
                        // MyApplication.getInstance().setMatchImages(jpegTrueFile.getAbsolutePath());

                        //bit.recycle();

                    } else {
                        resizeBmp = BitmapFactory.decodeStream(fis);
                    }

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            // TODO Auto-generated method stub
                            if (select_type == 1) {
                                path_logo = imageUrl;
                                img_logo.setImageBitmap(resizeBmp);
                            } else if (select_type == 2) {
                                path_price = imageUrl;
                                img_price.setImageBitmap(resizeBmp);
                            } else if (select_type == 3) {
                                MyApplication.getInstance().setMatchImages(imageUrl);
                                adapter.notifyDataSetChanged();
                            }
                            if (dialog.isShowing()) {
                                dialog.dismiss();
                            }
                        }
                    });

                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }).start();
    }
}

From source file:com.yk.notification.util.BitmapUtil.java

/**
 * ?/*from ww  w  .j  a  v a 2 s. com*/
 * 
 * @param bitmap
 *            ?Bitmap
 * @param w
 *            
 * @param h
 *            
 * @return Bitmap
 */
public static Bitmap zoom(Bitmap bitmap, int w, int h) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    Matrix matrix = new Matrix();
    float scaleWidht = ((float) w / width);
    float scaleHeight = ((float) h / height);
    matrix.postScale(scaleWidht, scaleHeight);
    Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
    return newbmp;
}

From source file:com.yk.notification.util.BitmapUtil.java

/**
 * /*from w  w  w  .ja  va  2s  .  co m*/
 * 
 * @param src
 *            ??
 * @param newWidth
 *            ?
 * @param newHeight
 *            ?
 */
public static Bitmap scale(Bitmap src, double newWidth, double newHeight) {
    // src
    float width = src.getWidth();
    float height = src.getHeight();
    // matrix
    Matrix matrix = new Matrix();
    // 
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;
    // 
    matrix.postScale(scaleWidth, scaleHeight);
    // ?
    return Bitmap.createBitmap(src, 0, 0, (int) width, (int) height, matrix, true);
}

From source file:cw.kop.autobackground.files.DownloadThread.java

private void writeToFileWithThumbnail(Bitmap image, String saveData, String dir, File file, long time) {

    if (file.isFile()) {
        file.delete();/*from  w  ww.j a  v a 2  s  .c o  m*/
    }

    FileOutputStream out = null;
    FileOutputStream thumbnailOut = null;
    try {
        int bitWidth = image.getWidth();
        int bitHeight = image.getHeight();

        float thumbnailSize = (float) AppSettings.getThumbnailSize();

        Bitmap thumbnail;

        if (thumbnailSize < bitWidth && thumbnailSize < bitHeight) {
            Matrix matrix = new Matrix();
            if (bitWidth > bitHeight) {
                matrix.postScale(thumbnailSize / bitWidth, thumbnailSize / bitWidth);
            } else {
                matrix.postScale(thumbnailSize / bitHeight, thumbnailSize / bitHeight);
            }
            thumbnail = Bitmap.createBitmap(image, 0, 0, bitWidth, bitHeight, matrix, false);
        } else {
            thumbnail = image;
        }

        out = new FileOutputStream(file);
        image.compress(Bitmap.CompressFormat.PNG, 90, out);

        File thumbnailCache = new File(dir + "/HistoryCache");

        if (!thumbnailCache.exists() || (thumbnailCache.exists() && !thumbnailCache.isDirectory())) {
            thumbnailCache.mkdir();
        }

        File thumbnailFile = new File(thumbnailCache.getAbsolutePath() + "/" + time + ".png");

        thumbnailOut = new FileOutputStream(thumbnailFile);

        thumbnail.compress(Bitmap.CompressFormat.PNG, 90, thumbnailOut);

        Log.i(TAG, "Thumbnail written: " + thumbnailFile.getAbsolutePath());

        AppSettings.setUrl(file.getName(), saveData);
        Log.i(TAG, file.getName() + " " + saveData);
        thumbnail.recycle();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } finally {
        try {
            if (out != null) {
                out.close();
            }
            if (thumbnailOut != null) {
                thumbnailOut.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    image.recycle();
}

From source file:com.inmobi.nativead.sample.photopages.CustomViewPager.java

@Override
protected boolean getChildStaticTransformation(View child, Transformation t) {
    // We can cast here because CustomPagerAdapter only creates wrappers.
    CustomViewPagerItemWrapper item = (CustomViewPagerItemWrapper) child;

    // Since Jelly Bean children won't get invalidated automatically,
    // needs to be added for the smooth coverflow animation
    if (android.os.Build.VERSION.SDK_INT >= 16) {
        item.invalidate();/*from  ww  w. jav  a  2 s.  com*/
    }

    final int coverFlowWidth = this.getWidth();
    final int coverFlowCenter = coverFlowWidth / 2;
    final int childWidth = item.getWidth();
    final int childHeight = item.getHeight();
    final int childCenter = item.getLeft() + childWidth / 2;

    // Use coverflow width when its defined as automatic.
    final int actionDistance = (this.actionDistance == ACTION_DISTANCE_AUTO)
            ? (int) ((coverFlowWidth + childWidth) / 2.0f)
            : this.actionDistance;

    // Calculate the abstract amount for all effects.
    final float effectsAmount = Math.min(1.0f,
            Math.max(-1.0f, (1.0f / actionDistance) * (childCenter - coverFlowCenter)));

    // Clear previous transformations and set transformation type (matrix + alpha).
    t.clear();
    t.setTransformationType(Transformation.TYPE_BOTH);

    // Alpha
    if (this.unselectedAlpha != 1) {
        final float alphaAmount = (this.unselectedAlpha - 1) * Math.abs(effectsAmount) + 1;
        t.setAlpha(alphaAmount);
    }

    // Saturation
    if (this.unselectedSaturation != 1) {
        // Pass over saturation to the wrapper.
        final float saturationAmount = (this.unselectedSaturation - 1) * Math.abs(effectsAmount) + 1;
        item.setSaturation(saturationAmount);
    }

    final Matrix imageMatrix = t.getMatrix();

    // Apply rotation.
    if (this.maxRotation != 0) {
        final int rotationAngle = (int) (-effectsAmount * this.maxRotation);
        this.transformationCamera.save();
        this.transformationCamera.rotateY(rotationAngle);
        this.transformationCamera.getMatrix(imageMatrix);
        this.transformationCamera.restore();
    }

    // Zoom.
    if (this.unselectedScale != 1) {
        final float zoomAmount = (this.unselectedScale - 1) * Math.abs(effectsAmount) + 1;
        // Calculate the scale anchor (y anchor can be altered)
        final float translateX = childWidth / 2.0f;
        final float translateY = childHeight * this.scaleDownGravity;
        imageMatrix.preTranslate(-translateX, -translateY);
        imageMatrix.postScale(zoomAmount, zoomAmount);
        imageMatrix.postTranslate(translateX, translateY);
    }

    return true;
}

From source file:com.rsmsa.accapp.MainActivity.java

public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {

    int width = bm.getWidth();

    int height = bm.getHeight();

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

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

    // CREATE A MATRIX FOR THE MANIPULATION

    Matrix matrix = new Matrix();

    // RESIZE THE BIT MAP

    matrix.postScale(scaleWidth, scaleHeight);

    // RECREATE THE NEW BITMAP

    Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
    imgPreview.setImageBitmap(resizedBitmap);
    return resizedBitmap;

}

From source file:ir.rasen.charsoo.controller.image_loader.core.decode.BaseImageDecoder.java

protected Bitmap considerExactScaleAndOrientatiton(Bitmap subsampledBitmap, ImageDecodingInfo decodingInfo,
        int rotation, boolean flipHorizontal) {
    Matrix m = new Matrix();
    // Scale to exact size if need
    ImageScaleType scaleType = decodingInfo.getImageScaleType();
    if (scaleType == ImageScaleType.EXACTLY || scaleType == ImageScaleType.EXACTLY_STRETCHED) {
        ImageSize srcSize = new ImageSize(subsampledBitmap.getWidth(), subsampledBitmap.getHeight(), rotation);
        float scale = ImageSizeUtils.computeImageScale(srcSize, decodingInfo.getTargetSize(),
                decodingInfo.getViewScaleType(), scaleType == ImageScaleType.EXACTLY_STRETCHED);
        if (Float.compare(scale, 1f) != 0) {
            m.setScale(scale, scale);/*from  w  w  w  .  j a v a 2 s  .  c om*/

            if (loggingEnabled) {
                L.d(LOG_SCALE_IMAGE, srcSize, srcSize.scale(scale), scale, decodingInfo.getImageKey());
            }
        }
    }
    // Flip bitmap if need
    if (flipHorizontal) {
        m.postScale(-1, 1);

        if (loggingEnabled)
            L.d(LOG_FLIP_IMAGE, decodingInfo.getImageKey());
    }
    // Rotate bitmap if need
    if (rotation != 0) {
        m.postRotate(rotation);

        if (loggingEnabled)
            L.d(LOG_ROTATE_IMAGE, rotation, decodingInfo.getImageKey());
    }

    Bitmap finalBitmap = Bitmap.createBitmap(subsampledBitmap, 0, 0, subsampledBitmap.getWidth(),
            subsampledBitmap.getHeight(), m, true);
    if (finalBitmap != subsampledBitmap) {
        subsampledBitmap.recycle();
    }
    return finalBitmap;
}