List of usage examples for android.graphics Canvas scale
public void scale(float sx, float sy)
From source file:Main.java
public static Bitmap drawViewToBitmap(View view, int width, int height, float translateX, float translateY, int downSampling, String color) { float scale = 1f / downSampling; int bmpWidth = (int) (width * scale - translateX / downSampling); int bmpHeight = (int) (height * scale - translateY / downSampling); Bitmap dest = Bitmap.createBitmap(bmpWidth, bmpHeight, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(dest); canvas.translate(-translateX / downSampling, -translateY / downSampling); if (downSampling > 1) { canvas.scale(scale, scale); }/* w w w . j av a2 s.co m*/ Paint paint = new Paint(); paint.setFlags(Paint.FILTER_BITMAP_FLAG | Paint.ANTI_ALIAS_FLAG); PorterDuffColorFilter filter = new PorterDuffColorFilter(Color.parseColor(color), PorterDuff.Mode.SRC_ATOP); paint.setColorFilter(filter); view.buildDrawingCache(); Bitmap cache = view.getDrawingCache(); canvas.drawBitmap(cache, 0, 0, paint); cache.recycle(); view.destroyDrawingCache(); return dest; }
From source file:Main.java
public static Bitmap resizeAndCropCenterExt(Bitmap bitmap, int size, boolean recycle) { int w = bitmap.getWidth(); int h = bitmap.getHeight(); if (w == size && h == size) return bitmap; // scale the image so that the shorter side equals to the target; // the longer side will be center-cropped. float scale = (float) size / Math.min(w, h); int width = Math.round(scale * bitmap.getWidth()); int height = Math.round(scale * bitmap.getHeight()); if (width > height) { int largeSize = (int) (width <= size * 1.5 ? width : size * 1.5); Bitmap target = Bitmap.createBitmap(largeSize, size, getConfig(bitmap)); Canvas canvas = new Canvas(target); canvas.translate((largeSize - width) / 2f, (size - height) / 2f); canvas.scale(scale, scale); Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG); canvas.drawBitmap(bitmap, 0, 0, paint); if (recycle) bitmap.recycle();/*from w w w . j ava2 s . c om*/ return target; } else { int largeSize = (int) (height <= size * 1.5 ? height : size * 1.5); Bitmap target = Bitmap.createBitmap(size, largeSize, getConfig(bitmap)); Canvas canvas = new Canvas(target); canvas.translate((size - width) / 2f, (largeSize - height) / 2f); canvas.scale(scale, scale); Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG); canvas.drawBitmap(bitmap, 0, 0, paint); if (recycle) bitmap.recycle(); return target; } }
From source file:Main.java
/** * This is only used when the launcher shortcut is created. * /*from ww w . j a v a2s. com*/ * @param bitmap The artist, album, genre, or playlist image that's going to * be cropped. * @param size The new size. * @return A {@link Bitmap} that has been resized and cropped for a launcher * shortcut. */ public static final Bitmap resizeAndCropCenter(final Bitmap bitmap, final int size) { Bitmap blurbitmap = null; final int w = bitmap.getWidth(); final int h = bitmap.getHeight(); if (w == size && h == size) { return bitmap; } ByteArrayOutputStream bos = new ByteArrayOutputStream(); bitmap.compress(CompressFormat.PNG, 0 /*ignored for PNG*/, bos); byte[] bitmapdata = bos.toByteArray(); ByteArrayInputStream bs = new ByteArrayInputStream(bitmapdata); try { final BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 8; options.inJustDecodeBounds = true; blurbitmap = BitmapFactory.decodeStream(bs, null, options); options.inJustDecodeBounds = false; } catch (Exception e) { e.printStackTrace(); } finally { if (bs != null) { try { bs.close(); } catch (IOException e) { e.printStackTrace(); } } } final float mScale = (float) size / Math.min(w, h); final Bitmap mTarget = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); final int mWidth = Math.round(mScale * blurbitmap.getWidth()); final int mHeight = Math.round(mScale * blurbitmap.getHeight()); final Canvas mCanvas = new Canvas(mTarget); mCanvas.translate((size - mWidth) / 2f, (size - mHeight) / 2f); mCanvas.scale(mScale, mScale); final Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG); mCanvas.drawBitmap(bitmap, 0, 0, paint); return mTarget; }
From source file:Main.java
/** * This is only used when the launcher shortcut is created. * * @param bitmap The artist, album, genre, or playlist image that's going to * be cropped.//from w ww . j av a2 s. com * @param size The new size. * @return A {@link Bitmap} that has been resized and cropped for a launcher * shortcut. */ public static final Bitmap resizeAndCropCenter(final Bitmap bitmap, final int size) { final int w = bitmap.getWidth(); final int h = bitmap.getHeight(); if (w == size && h == size) { return bitmap; } final float mScale = (float) size / Math.min(w, h); final Bitmap mTarget = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); final int mWidth = Math.round(mScale * bitmap.getWidth()); final int mHeight = Math.round(mScale * bitmap.getHeight()); final Canvas mCanvas = new Canvas(mTarget); mCanvas.translate((size - mWidth) / 2f, (size - mHeight) / 2f); mCanvas.scale(mScale, mScale); final Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG); mCanvas.drawBitmap(bitmap, 0, 0, paint); return mTarget; }
From source file:Main.java
public static Bitmap addLabelToBitmap(Bitmap src, String label) { float densityFactor = Resources.getSystem().getDisplayMetrics().density; final float textPadding = src.getWidth() * 0.05f; Bitmap result = Bitmap.createBitmap(src.getWidth(), src.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(result); Paint textPaint = new Paint(); textPaint.setAntiAlias(true);/*from ww w . j a v a 2s. c o m*/ textPaint.setTextSize(12 * densityFactor); textPaint.setColor(Color.WHITE); textPaint.setStrokeWidth(2 * densityFactor); textPaint.setShadowLayer(1 * densityFactor, 0, 0, Color.BLACK); float textWidth = textPaint.measureText(label); float scaleFactor = (src.getWidth() - textPadding * 2) / textWidth; canvas.drawBitmap(src, 0, 0, textPaint); canvas.save(); canvas.scale(scaleFactor, scaleFactor); float textPosX = (src.getWidth() / scaleFactor - textWidth) / 2; float textPosY = (src.getHeight() - textPadding) / scaleFactor; canvas.drawText(label, textPosX, textPosY, textPaint); canvas.restore(); return result; }
From source file:com.gh4a.utils.HttpImageGetter.java
private static Bitmap renderSvgToBitmap(Resources res, InputStream is, int maxWidth, int maxHeight) { //noinspection TryWithIdenticalCatches try {/*from w ww .jav a 2 s . c om*/ SVG svg = SVG.getFromInputStream(is); if (svg != null) { svg.setRenderDPI(DisplayMetrics.DENSITY_DEFAULT); Float density = res.getDisplayMetrics().density; int docWidth = (int) (svg.getDocumentWidth() * density); int docHeight = (int) (svg.getDocumentHeight() * density); if (docWidth < 0 || docHeight < 0) { float aspectRatio = svg.getDocumentAspectRatio(); if (aspectRatio > 0) { float heightForAspect = (float) maxWidth / aspectRatio; float widthForAspect = (float) maxHeight * aspectRatio; if (widthForAspect < heightForAspect) { docWidth = Math.round(widthForAspect); docHeight = maxHeight; } else { docWidth = maxWidth; docHeight = Math.round(heightForAspect); } } else { docWidth = maxWidth; docHeight = maxHeight; } // we didn't take density into account anymore when calculating docWidth // and docHeight, so don't scale with it and just let the renderer // figure out the scaling density = null; } while (docWidth >= maxWidth || docHeight >= maxHeight) { docWidth /= 2; docHeight /= 2; if (density != null) { density /= 2; } } Bitmap bitmap = Bitmap.createBitmap(docWidth, docHeight, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); if (density != null) { canvas.scale(density, density); } svg.renderToCanvas(canvas); return bitmap; } } catch (SVGParseException e) { // fall through } catch (NullPointerException e) { // https://github.com/BigBadaboom/androidsvg/issues/81 // remove me when there's a 1.2.3 release } return null; }
From source file:Main.java
public static Bitmap transferMode(Bitmap src, Bitmap mask, Xfermode xfermode) { final int width = mask.getWidth(); final int height = mask.getHeight(); final Bitmap dst = Bitmap.createBitmap(width, height, Config.ARGB_8888); final Canvas canvas = new Canvas(dst); final Paint paint = new Paint(); paint.setAntiAlias(true);/* w w w . j av a2 s . c o m*/ final int srcWidth = src.getWidth(); final int srcHeight = src.getHeight(); canvas.save(); // Scale down the image first if required. if ((width < srcWidth) || (height < srcHeight)) { float radioX = (float) width / srcWidth; float radioY = (float) height / srcHeight; float radio = 1f; float dx = 0f; float dy = 0f; if (radioX > radioY) { radio = radioX; dy = (height / radioX - srcHeight) / 2.0f; } else { radio = radioY; dx = (width / radioX - srcWidth) / 2.0f; } canvas.scale(radio, radio); canvas.translate(dx, dy); } canvas.drawBitmap(src, 0, 0, paint); canvas.restore(); if (xfermode != null) { paint.setXfermode(xfermode); canvas.drawBitmap(mask, 0, 0, paint); } return dst; }
From source file:Main.java
/** * @brief Converting picture to bitmap array. * // w w w.j av a2 s. c o m * @author daiping.zhao * @param bm [IN] bitmap object * * @return Return converted bytes array */ public static Bitmap pictureToBitmap(Picture picture, int width, int height) { Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565); Canvas canvas = new Canvas(b); // May need to tweak these values to determine what is the // best scale factor int picWidth = picture.getWidth(); int picHeight = picture.getHeight(); float scaleFactorX = 1.0f; float scaleFactorY = 1.0f; if (picWidth > 0) { scaleFactorX = (float) width / (float) picWidth; } else { return null; } if (picWidth > picHeight) { // If the device is in landscape and the page is shorter // than the height of the view, stretch the thumbnail to fill the // space. scaleFactorY = (float) height / (float) picHeight; } else { // In the portrait case, this looks nice. scaleFactorY = scaleFactorX; } canvas.scale(scaleFactorX, scaleFactorY); picture.draw(canvas); return b; }
From source file:com.fastaccess.tfl.ui.widget.drag.DragView.java
@Override protected void onDraw(Canvas canvas) { float scale = mAnimationScale; if (scale < 0.999f) { float width = mBitmap.getWidth(); float offset = (width - (width * scale)) / 2; canvas.translate(offset, offset); canvas.scale(scale, scale); }/*from ww w . j av a2 s. c om*/ canvas.drawBitmap(mBitmap, 0.0f, 0.0f, mPaint); }
From source file:net.yanzm.actionbarprogress.MaterialIndeterminateProgressDrawable.java
@Override public void draw(Canvas canvas) { final int level = getLevel(); final float input = level / 10000f; canvas.drawColor(trackColor);// w ww . j ava2 s . co m canvas.save(); canvas.translate(canvas.getWidth() / 2f, 0); canvas.scale(canvas.getWidth() / 360f, 1); paint.setColor(accentColor); { final int saveCount = canvas.save(); float translateX2 = translateInterpolator2.getInterpolation(input); canvas.translate(-197.60001f + translateX2 * 620.20002f, 0); float x = scaleInterpolator2.getInterpolation(input); float scaleX = 1.6199005127f * -Math.abs(x - 0.5f) + 0.909950256348f; rect2.set(-144f * scaleX, 0, 144f * scaleX, canvas.getHeight()); canvas.drawRect(rect2, paint); canvas.restoreToCount(saveCount); } { final int saveCount = canvas.save(); float translateX1 = translateInterpolator1.getInterpolation(input); canvas.translate(-522.59998f + translateX1 * 722.19999f, 0); float x = scaleInterpolator1.getInterpolation(input); float scaleX = 1.45369842529f * -Math.abs(x - 0.5f) + 0.826849212646f; rect1.set(-144f * scaleX, 0, 144f * scaleX, canvas.getHeight()); canvas.drawRect(rect1, paint); canvas.restoreToCount(saveCount); } canvas.restore(); }