List of usage examples for android.graphics Matrix setScale
public void setScale(float sx, float sy)
From source file:Main.java
/** * Transform source Bitmap to targeted width and height. *//*from w w w .j a v a 2s . com*/ private static Bitmap transform(Matrix scaler, Bitmap source, int targetWidth, int targetHeight, int options) { boolean scaleUp = (options & OPTIONS_SCALE_UP) != 0; boolean recycle = (options & OPTIONS_RECYCLE_INPUT) != 0; int deltaX = source.getWidth() - targetWidth; int deltaY = source.getHeight() - targetHeight; if (!scaleUp && (deltaX < 0 || deltaY < 0)) { /* * In this case the bitmap is smaller, at least in one dimension, * than the target. Transform it by placing as much of the image * as possible into the target and leaving the top/bottom or * left/right (or both) black. */ Bitmap b2 = Bitmap.createBitmap(targetWidth, targetHeight, Bitmap.Config.ARGB_8888); Canvas c = new Canvas(b2); int deltaXHalf = Math.max(0, deltaX / 2); int deltaYHalf = Math.max(0, deltaY / 2); Rect src = new Rect(deltaXHalf, deltaYHalf, deltaXHalf + Math.min(targetWidth, source.getWidth()), deltaYHalf + Math.min(targetHeight, source.getHeight())); int dstX = (targetWidth - src.width()) / 2; int dstY = (targetHeight - src.height()) / 2; Rect dst = new Rect(dstX, dstY, targetWidth - dstX, targetHeight - dstY); c.drawBitmap(source, src, dst, null); if (recycle) { source.recycle(); } c.setBitmap(null); return b2; } float bitmapWidthF = source.getWidth(); float bitmapHeightF = source.getHeight(); float bitmapAspect = bitmapWidthF / bitmapHeightF; float viewAspect = (float) targetWidth / targetHeight; if (bitmapAspect > viewAspect) { float scale = targetHeight / bitmapHeightF; if (scale < .9F || scale > 1F) { scaler.setScale(scale, scale); } else { scaler = null; } } else { float scale = targetWidth / bitmapWidthF; if (scale < .9F || scale > 1F) { scaler.setScale(scale, scale); } else { scaler = null; } } Bitmap b1; if (scaler != null) { // this is used for minithumb and crop, so we want to filter here. b1 = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), scaler, true); } else { b1 = source; } if (recycle && b1 != source) { source.recycle(); } int dx1 = Math.max(0, b1.getWidth() - targetWidth); int dy1 = Math.max(0, b1.getHeight() - targetHeight); Bitmap b2 = Bitmap.createBitmap(b1, dx1 / 2, 0, targetWidth, targetHeight); if (b2 != b1) { if (recycle || b1 != source) { b1.recycle(); } } return b2; }
From source file:Main.java
/** * Transform source Bitmap to targeted width and height. *//*from w w w . j a v a 2 s . c o m*/ private static Bitmap transform(Matrix scaler, Bitmap source, int targetWidth, int targetHeight, int options) { boolean scaleUp = (options & OPTIONS_SCALE_UP) != 0; boolean recycle = (options & OPTIONS_RECYCLE_INPUT) != 0; int deltaX = source.getWidth() - targetWidth; int deltaY = source.getHeight() - targetHeight; if (!scaleUp && (deltaX < 0 || deltaY < 0)) { /* * In this case the bitmap is smaller, at least in one dimension, * than the target. Transform it by placing as much of the image as * possible into the target and leaving the top/bottom or left/right * (or both) black. */ Bitmap b2 = Bitmap.createBitmap(targetWidth, targetHeight, Bitmap.Config.ARGB_8888); Canvas c = new Canvas(b2); int deltaXHalf = Math.max(0, deltaX / 2); int deltaYHalf = Math.max(0, deltaY / 2); Rect src = new Rect(deltaXHalf, deltaYHalf, deltaXHalf + Math.min(targetWidth, source.getWidth()), deltaYHalf + Math.min(targetHeight, source.getHeight())); int dstX = (targetWidth - src.width()) / 2; int dstY = (targetHeight - src.height()) / 2; Rect dst = new Rect(dstX, dstY, targetWidth - dstX, targetHeight - dstY); c.drawBitmap(source, src, dst, null); if (recycle) { source.recycle(); } c.setBitmap(null); return b2; } float bitmapWidthF = source.getWidth(); float bitmapHeightF = source.getHeight(); float bitmapAspect = bitmapWidthF / bitmapHeightF; float viewAspect = (float) targetWidth / targetHeight; if (bitmapAspect > viewAspect) { float scale = targetHeight / bitmapHeightF; if (scale < .9F || scale > 1F) { scaler.setScale(scale, scale); } else { scaler = null; } } else { float scale = targetWidth / bitmapWidthF; if (scale < .9F || scale > 1F) { scaler.setScale(scale, scale); } else { scaler = null; } } Bitmap b1; if (scaler != null) { // this is used for minithumb and crop, so we want to filter here. b1 = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), scaler, true); } else { b1 = source; } if (recycle && b1 != source) { source.recycle(); } int dx1 = Math.max(0, b1.getWidth() - targetWidth); int dy1 = Math.max(0, b1.getHeight() - targetHeight); Bitmap b2 = Bitmap.createBitmap(b1, dx1 / 2, dy1 / 2, targetWidth, targetHeight); if (b2 != b1) { if (recycle || b1 != source) { b1.recycle(); } } return b2; }
From source file:com.pablog178.pdfcreator.android.PdfcreatorModule.java
private void generateImageFunction(HashMap args) { if (args.containsKey("fileName")) { Object fileName = args.get("fileName"); if (fileName instanceof String) { this.fileName = (String) fileName; Log.i(PROXY_NAME, "fileName: " + this.fileName); }//from w w w .ja v a 2s .c o m } else return; if (args.containsKey("view")) { Object viewObject = args.get("view"); if (viewObject instanceof TiViewProxy) { TiViewProxy viewProxy = (TiViewProxy) viewObject; this.view = viewProxy.getOrCreateView(); if (this.view == null) { Log.e(PROXY_NAME, "NO VIEW was created!!"); return; } Log.i(PROXY_NAME, "view: " + this.view.toString()); } } else return; TiBaseFile file = TiFileFactory.createTitaniumFile(this.fileName, true); Log.i(PROXY_NAME, "file full path: " + file.nativePath()); try { final int PDF_WIDTH = 612; final int PDF_HEIGHT = 792; Resources appResources = app.getResources(); OutputStream outputStream = file.getOutputStream(); int viewWidth = 1600; int viewHeight = 1; WebView view = (WebView) this.view.getNativeView(); if (TiApplication.isUIThread()) { viewWidth = view.capturePicture().getWidth(); viewHeight = view.capturePicture().getHeight(); if (viewWidth <= 0) { viewWidth = 1300; } if (viewHeight <= 0) { viewHeight = 2300; } } else { Log.e(PROXY_NAME, "NO UI THREAD"); } Log.i(PROXY_NAME, "viewWidth: " + viewWidth); Log.i(PROXY_NAME, "viewHeight: " + viewHeight); Bitmap viewBitmap = Bitmap.createBitmap(viewWidth, viewHeight, Bitmap.Config.ARGB_8888); float density = appResources.getDisplayMetrics().density; Canvas canvas = new Canvas(viewBitmap); Matrix matrix = new Matrix(); Drawable bgDrawable = view.getBackground(); if (bgDrawable != null) { bgDrawable.draw(canvas); } else { canvas.drawColor(Color.WHITE); } view.draw(canvas); float scaleFactorWidth = 1 / ((float) viewWidth / (float) PDF_WIDTH); float scaleFactorHeight = 1 / ((float) viewHeight / (float) PDF_HEIGHT); Log.i(PROXY_NAME, "scaleFactorWidth: " + scaleFactorWidth); Log.i(PROXY_NAME, "scaleFactorHeight: " + scaleFactorHeight); matrix.setScale(scaleFactorWidth, scaleFactorWidth); Bitmap imageBitmap = Bitmap.createBitmap(PDF_WIDTH, PDF_HEIGHT, Bitmap.Config.ARGB_8888); Canvas imageCanvas = new Canvas(imageBitmap); imageCanvas.drawBitmap(viewBitmap, matrix, null); imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream); sendCompleteEvent(); } catch (Exception exception) { Log.e(PROXY_NAME, "Error: " + exception.toString()); sendErrorEvent(exception.toString()); } }
From source file:cn.jmessage.android.uikit.pickerimage.view.BaseZoomableImageView.java
private void setBaseMatrix(Bitmap bitmap, Matrix matrix) { float viewWidth = getWidth(); float viewHeight = getHeight(); matrix.reset();/*from ww w.j ava 2 s. co m*/ float widthScale = Math.min(viewWidth / (float) bitmap.getWidth(), 1.0f); float heightScale = Math.min(viewHeight / (float) bitmap.getHeight(), 1.0f); float scale; if (widthScale > heightScale) { scale = heightScale; } else { scale = widthScale; } matrix.setScale(scale, scale); matrix.postTranslate((viewWidth - ((float) bitmap.getWidth() * scale)) / 2F, (viewHeight - ((float) bitmap.getHeight() * scale)) / 2F); }
From source file:Main.java
/** * Transform source Bitmap to targeted width and height. *//*from w w w . ja v a 2 s . c o m*/ private static Bitmap transform(Matrix scaler, Bitmap source, int targetWidth, int targetHeight, int options) { boolean scaleUp = (options & OPTIONS_SCALE_UP) != 0; boolean recycle = (options & OPTIONS_RECYCLE_INPUT) != 0; int deltaX = source.getWidth() - targetWidth; int deltaY = source.getHeight() - targetHeight; if (!scaleUp && (deltaX < 0 || deltaY < 0)) { /* * In this case the bitmap is smaller, at least in one dimension, * than the target. Transform it by placing as much of the image * as possible into the target and leaving the top/bottom or * left/right (or both) black. */ Bitmap b2 = Bitmap.createBitmap(targetWidth, targetHeight, Bitmap.Config.ARGB_8888); Canvas c = new Canvas(b2); int deltaXHalf = Math.max(0, deltaX / 2); int deltaYHalf = Math.max(0, deltaY / 2); Rect src = new Rect(deltaXHalf, deltaYHalf, deltaXHalf + Math.min(targetWidth, source.getWidth()), deltaYHalf + Math.min(targetHeight, source.getHeight())); int dstX = (targetWidth - src.width()) / 2; int dstY = (targetHeight - src.height()) / 2; Rect dst = new Rect(dstX, dstY, targetWidth - dstX, targetHeight - dstY); c.drawBitmap(source, src, dst, null); if (recycle) { source.recycle(); } return b2; } float bitmapWidthF = source.getWidth(); float bitmapHeightF = source.getHeight(); float bitmapAspect = bitmapWidthF / bitmapHeightF; float viewAspect = (float) targetWidth / targetHeight; if (bitmapAspect > viewAspect) { float scale = targetHeight / bitmapHeightF; if (scale < .9F || scale > 1F) { scaler.setScale(scale, scale); } else { scaler = null; } } else { float scale = targetWidth / bitmapWidthF; if (scale < .9F || scale > 1F) { scaler.setScale(scale, scale); } else { scaler = null; } } Bitmap b1; if (scaler != null) { // this is used for minithumb and crop, so we want to filter here. b1 = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), scaler, true); } else { b1 = source; } if (recycle && b1 != source) { source.recycle(); } int dx1 = Math.max(0, b1.getWidth() - targetWidth); int dy1 = Math.max(0, b1.getHeight() - targetHeight); Bitmap b2 = Bitmap.createBitmap(b1, dx1 / 2, dy1 / 2, targetWidth, targetHeight); if (b2 != b1) { if (recycle || b1 != source) { b1.recycle(); } } return b2; }
From source file:cn.jmessage.android.uikit.pickerimage.view.BaseZoomableImageView.java
/** * Setup the base matrix so that the image is centered and scaled properly. * ?BitmapRect?Matrix//from ww w . j a v a 2 s. c o m * @author Linleja * @date 2014-4-29 * @param bitmap * @param matrix * @param selection */ private void setBaseMatrix(Bitmap bitmap, Matrix matrix, Rect selection) { if (selection == null) { return; } float viewWidth = selection.right - selection.left; float viewHeight = selection.bottom - selection.top; matrix.reset(); float widthRatio = viewWidth / (float) bitmap.getWidth(); float heighRatio = viewHeight / (float) bitmap.getHeight(); float scale = 1.0f; if (widthRatio > heighRatio) { scale = widthRatio; } else { scale = heighRatio; } matrix.setScale(scale, scale); matrix.postTranslate(((getWidth() - (float) bitmap.getWidth() * scale)) / 2F, ((getHeight() - (float) bitmap.getHeight() * scale)) / 2F); }
From source file:com.kjsaw.alcosys.ibacapp.IBAC.java
private Bitmap processBitmap(Bitmap orginalBitmap, int degrees) { Matrix matrixRotate = new Matrix(); matrixRotate.setRotate(degrees, (float) orginalBitmap.getWidth() / 2, (float) orginalBitmap.getHeight() / 2); Bitmap rotatedBitmap = Bitmap.createBitmap(orginalBitmap, 0, 0, orginalBitmap.getWidth(), orginalBitmap.getHeight(), matrixRotate, true); int width = rotatedBitmap.getWidth(); int height = rotatedBitmap.getHeight(); int newWidth = 240; int newHeight = 320; float scaleWidth = ((float) newWidth) / width; float scaleHeight = ((float) newHeight) / height; Matrix matrixResize = new Matrix(); if (Session.isFacingCamera) { matrixResize.setScale(-1, 1);// reversal }/*from w w w. j a v a 2s. co m*/ matrixResize.postScale(scaleWidth, scaleHeight); return Bitmap.createBitmap(rotatedBitmap, 0, 0, width, height, matrixResize, true); }
From source file:android.support.v17.leanback.app.BackgroundManager.java
/** * Sets the given bitmap into the background. When using setBitmap to set the * background, the provided bitmap will be scaled and cropped to correctly * fit within the dimensions of the view. The timing for when this becomes * visible in the app is undefined and may take place after a small delay. *//*from ww w . ja v a 2 s . c om*/ public void setBitmap(Bitmap bitmap) { if (DEBUG) { Log.v(TAG, "setBitmap " + bitmap); } if (bitmap == null) { setDrawableInternal(null); return; } if (bitmap.getWidth() <= 0 || bitmap.getHeight() <= 0) { if (DEBUG) { Log.v(TAG, "invalid bitmap width or height"); } return; } Matrix matrix = null; if ((bitmap.getWidth() != mWidthPx || bitmap.getHeight() != mHeightPx)) { int dwidth = bitmap.getWidth(); int dheight = bitmap.getHeight(); float scale; // Scale proportionately to fit width and height. if (dwidth * mHeightPx > mWidthPx * dheight) { scale = (float) mHeightPx / (float) dheight; } else { scale = (float) mWidthPx / (float) dwidth; } int subX = Math.min((int) (mWidthPx / scale), dwidth); int dx = Math.max(0, (dwidth - subX) / 2); matrix = new Matrix(); matrix.setScale(scale, scale); matrix.preTranslate(-dx, 0); if (DEBUG) Log.v(TAG, "original image size " + bitmap.getWidth() + "x" + bitmap.getHeight() + " scale " + scale + " dx " + dx); } BitmapDrawable bitmapDrawable = new BitmapDrawable(mContext.getResources(), bitmap, matrix); setDrawableInternal(bitmapDrawable); }