List of usage examples for android.graphics Canvas drawBitmap
public void drawBitmap(@NonNull Bitmap bitmap, @Nullable Rect src, @NonNull Rect dst, @Nullable Paint paint)
From source file:Main.java
public static Bitmap getRoundBitmap(Bitmap bitmap) { int width = bitmap.getWidth(); int height = bitmap.getHeight(); float roundPx; float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom; if (width <= height) { roundPx = width / 2;//from w w w . j av a 2 s . co m top = 0; bottom = width; left = 0; right = width; height = width; dst_left = 0; dst_top = 0; dst_right = width; dst_bottom = width; } else { roundPx = height / 2; float clip = (width - height) / 2; left = clip; right = width - clip; top = 0; bottom = height; width = height; dst_left = 0; dst_top = 0; dst_right = height; dst_bottom = height; } Bitmap output = Bitmap.createBitmap(width, height, Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = 0xff424242; final Paint paint = new Paint(); final Rect src = new Rect((int) left, (int) top, (int) right, (int) bottom); final Rect dst = new Rect((int) dst_left, (int) dst_top, (int) dst_right, (int) dst_bottom); final RectF rectF = new RectF(dst); paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); canvas.drawRoundRect(rectF, roundPx, roundPx, paint); paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); canvas.drawBitmap(bitmap, src, dst, paint); return output; }
From source file:com.entertailion.android.slideshow.utils.Utils.java
/** * Create a bitmap from a image URL.//w w w . j a va2s . c o m * * @param src * @return */ public static final Bitmap getBitmapFromURL(String src) { try { URL url = new URL(src); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoInput(true); connection.connect(); InputStream input = connection.getInputStream(); Bitmap myBitmap = BitmapFactory.decodeStream(input); int size = Math.max(myBitmap.getWidth(), myBitmap.getHeight()); Bitmap b = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); Canvas c = new Canvas(b); Paint paint = new Paint(); paint.setAntiAlias(true); c.drawBitmap(myBitmap, (size - myBitmap.getWidth()) / 2, (size - myBitmap.getHeight()) / 2, paint); return b; } catch (Exception e) { Log.e(LOG_TAG, "Faild to get the image from URL:" + src, e); return null; } }
From source file:Main.java
/** * Transform source Bitmap to targeted width and height. */// 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(); } 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, //dy1 / 2, targetWidth, targetHeight); if (b2 != b1) { if (recycle || b1 != source) { b1.recycle(); } } return b2; }
From source file:Main.java
/** * Create a rounded corner bitmap from current bitmap * * @param bitmap//from www . jav a2 s . c o m * @return */ public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) { try { Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888); Canvas canvas = new Canvas(output); final Paint paint = new Paint(); final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); final RectF rectF = new RectF(new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight())); final float roundPx = 50; paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(Color.BLACK); canvas.drawRoundRect(rectF, roundPx, roundPx, paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); final Rect src = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); canvas.drawBitmap(bitmap, src, rect, paint); return output; } catch (Exception e) { return bitmap; } }
From source file:Main.java
/** * Transform source Bitmap to targeted width and height. *//*from ww w . j a v a 2s .c om*/ 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, 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:Main.java
/** * Draw input bitmap in shadow color and offset. Assumes offset is positive. * * @param inBitmap//from w w w. ja v a2 s . c om * @param offsetX * @param offsetY * @param blurRadius Not used * @param shadowColor * @return */ public static Bitmap shadowBitmap(Bitmap inBitmap, int offsetX, int offsetY, float blurRadius, int shadowColor) { int imgWidth = inBitmap.getWidth(); int imgHeight = inBitmap.getHeight(); Bitmap bottomBm = Bitmap.createBitmap(imgWidth, imgHeight, Bitmap.Config.ARGB_8888); Canvas bottomCanvas = new Canvas(bottomBm); // bottomCanvas.drawColor(shadowColor); Paint bottomPaint = new Paint(Paint.ANTI_ALIAS_FLAG); bottomPaint.setColor(shadowColor); bottomCanvas.drawRect(offsetX, offsetY, imgWidth, imgHeight, bottomPaint); // MaskFilter filter = new BlurMaskFilter(Math.max(0.5f, blurRadius), BlurMaskFilter.Blur.NORMAL); bottomPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_ATOP)); // bottomPaint.setShadowLayer(blurRadius, offsetX, offsetX, shadowColor); // bottomPaint.setMaskFilter(filter); bottomCanvas.drawBitmap(inBitmap, offsetX, offsetY, bottomPaint); return bottomBm; }
From source file:ca.psiphon.ploggy.Robohash.java
public static Bitmap getRobohash(Context context, boolean cacheCandidate, byte[] data) throws Utils.ApplicationError { try {//from w w w .ja v a2s .c om MessageDigest sha1 = MessageDigest.getInstance("SHA-1"); byte[] digest = sha1.digest(data); String key = Utils.formatFingerprint(digest); Bitmap cachedBitmap = mCache.get(key); if (cachedBitmap != null) { return cachedBitmap; } ByteBuffer byteBuffer = ByteBuffer.wrap(digest); byteBuffer.order(ByteOrder.BIG_ENDIAN); // TODO: SecureRandom SHA1PRNG (but not LinuxSecureRandom) Random random = new Random(byteBuffer.getLong()); AssetManager assetManager = context.getAssets(); if (mConfig == null) { mConfig = new JSONObject(loadAssetToString(assetManager, CONFIG_FILENAME)); } int width = mConfig.getInt("width"); int height = mConfig.getInt("height"); JSONArray colors = mConfig.getJSONArray("colors"); JSONArray parts = colors.getJSONArray(random.nextInt(colors.length())); Bitmap robotBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas robotCanvas = new Canvas(robotBitmap); for (int i = 0; i < parts.length(); i++) { JSONArray partChoices = parts.getJSONArray(i); String selection = partChoices.getString(random.nextInt(partChoices.length())); Bitmap partBitmap = loadAssetToBitmap(assetManager, selection); Rect rect = new Rect(0, 0, width, height); Paint paint = new Paint(); paint.setAlpha(255); robotCanvas.drawBitmap(partBitmap, rect, rect, paint); partBitmap.recycle(); } if (cacheCandidate) { mCache.set(key, robotBitmap); } return robotBitmap; } catch (IOException e) { throw new Utils.ApplicationError(LOG_TAG, e); } catch (JSONException e) { throw new Utils.ApplicationError(LOG_TAG, e); } catch (NoSuchAlgorithmException e) { throw new Utils.ApplicationError(LOG_TAG, e); } }
From source file:Main.java
/** * Create circle image./* www .j a v a 2 s. co m*/ * * @param bitmap Bitmap to be cropped * @param resColor Resource color * @param strokeWidth Thickness of stroke * @return Returns the circle image with border */ public static Bitmap getCircleImage(Bitmap bitmap, int resColor, int strokeWidth) { // create Bitmap to draw Bitmap mBitmap = Bitmap.createBitmap(bitmap.getWidth() + 8, bitmap.getHeight() + 8, Bitmap.Config.ARGB_8888); // create Rect to hold image final Rect mRec = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); // create Canvas Canvas mCanvas = new Canvas(mBitmap); mCanvas.drawARGB(0, 0, 0, 0); // create Paint final Paint mPaint = new Paint(); mPaint.setStyle(Paint.Style.FILL); mPaint.setAntiAlias(true); // get the half size of the image int mHalfWidth = bitmap.getWidth() / 2; int mHalfHeight = bitmap.getHeight() / 2; // draw circle mCanvas.drawCircle((mHalfWidth + 4), (mHalfHeight + 4), Math.min(mHalfWidth, mHalfHeight), mPaint); // unknown mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); // draw the image mCanvas.drawBitmap(bitmap, mRec, mRec, mPaint); // set border mode mPaint.setXfermode(null); // set stroke mPaint.setStyle(Paint.Style.STROKE); // set stroke color mPaint.setColor(resColor); // set stroke width mPaint.setStrokeWidth(strokeWidth); // draw stroke mCanvas.drawCircle((mHalfWidth + 4), (mHalfHeight + 4), Math.min(mHalfWidth, mHalfHeight), mPaint); // return the circle image return mBitmap; }
From source file:Main.java
public static Bitmap duplicateBitmap(Bitmap bmpSrc, int width, int height) { if (null == bmpSrc) { return null; }//from ww w . j av a 2 s . com int bmpSrcWidth = bmpSrc.getWidth(); int bmpSrcHeight = bmpSrc.getHeight(); Bitmap bmpDest = Bitmap.createBitmap(width, height, Config.ARGB_8888); if (null != bmpDest) { Canvas canvas = new Canvas(bmpDest); Rect viewRect = new Rect(); final Rect rect = new Rect(0, 0, bmpSrcWidth, bmpSrcHeight); if (bmpSrcWidth <= width && bmpSrcHeight <= height) { viewRect.set(rect); } else if (bmpSrcHeight > height && bmpSrcWidth <= width) { viewRect.set(0, 0, bmpSrcWidth, height); } else if (bmpSrcHeight <= height && bmpSrcWidth > width) { viewRect.set(0, 0, width, bmpSrcWidth); } else if (bmpSrcHeight > height && bmpSrcWidth > width) { viewRect.set(0, 0, width, height); } canvas.drawBitmap(bmpSrc, rect, viewRect, null); } return bmpDest; }
From source file:Main.java
public static Bitmap getCircularBitmap(Bitmap bm) { int size = 192; Bitmap bitmap = ThumbnailUtils.extractThumbnail(bm, size, size); Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = 0xffff0000; final Paint paint = new Paint(); final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); final RectF rectF = new RectF(rect); paint.setAntiAlias(true);/*from w w w. j a va 2 s .c o m*/ paint.setDither(true); paint.setFilterBitmap(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); canvas.drawOval(rectF, paint); paint.setColor(Color.BLUE); paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth((float) 4); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect, paint); return output; }