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 getCircleBitmap(Bitmap bitmap) { final Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888); final Canvas canvas = new Canvas(output); final int color = Color.RED; final Paint paint = new Paint(); final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); final RectF rectF = new RectF(rect); //final Paint paintBorder = new Paint(); // paintBorder.setColor(Color.GREEN); //paintBorder.setShadowLayer(4.0f, 0.0f, 2.0f, Color.BLACK); //BitmapShader shader = new BitmapShader(Bitmap.createScaledBitmap(output, canvas.getWidth(), canvas.getHeight(), false), Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); //paint.setShader(shader); paint.setAntiAlias(true);/*from w w w. j a v a 2 s. com*/ //paintBorder.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); canvas.drawOval(rectF, paint); //int circleCenter = bitmap.getWidth() / 2; //int borderWidth = 2; //canvas.drawCircle(circleCenter + borderWidth, circleCenter + borderWidth, circleCenter + borderWidth - 4.0f, paintBorder); //canvas.drawCircle(circleCenter + borderWidth, circleCenter + borderWidth, circleCenter - 4.0f, paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect, paint); //canvas.drawBitmap(bitmap, circleCenter + borderWidth, circleCenter + borderWidth, paint); bitmap.recycle(); return output; }
From source file:Main.java
/** * Given an input bitmap, scales it to the given width/height and makes it round. * * @param input {@link Bitmap} to scale and crop * @param targetWidth desired output width * @param targetHeight desired output height * @return output bitmap scaled to the target width/height and cropped to an oval. The * cropping algorithm will try to fit as much of the input into the output as possible, * while preserving the target width/height ratio. *///ww w . ja v a 2 s . com public static Bitmap getRoundedBitmap(Bitmap input, int targetWidth, int targetHeight) { if (input == null) { return null; } final Bitmap.Config inputConfig = input.getConfig(); final Bitmap result = Bitmap.createBitmap(targetWidth, targetHeight, inputConfig != null ? inputConfig : Bitmap.Config.ARGB_8888); final Canvas canvas = new Canvas(result); final Paint paint = new Paint(); canvas.drawARGB(0, 0, 0, 0); paint.setAntiAlias(true); canvas.drawOval(0, 0, targetWidth, targetHeight, paint); // Specifies that only pixels present in the destination (i.e. the drawn oval) should // be overwritten with pixels from the input bitmap. paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); final int inputWidth = input.getWidth(); final int inputHeight = input.getHeight(); // Choose the largest scale factor that will fit inside the dimensions of the // input bitmap. final float scaleBy = Math.min((float) inputWidth / targetWidth, (float) inputHeight / targetHeight); final int xCropAmountHalved = (int) (scaleBy * targetWidth / 2); final int yCropAmountHalved = (int) (scaleBy * targetHeight / 2); final Rect src = new Rect(inputWidth / 2 - xCropAmountHalved, inputHeight / 2 - yCropAmountHalved, inputWidth / 2 + xCropAmountHalved, inputHeight / 2 + yCropAmountHalved); final RectF dst = new RectF(0, 0, targetWidth, targetHeight); canvas.drawBitmap(input, src, dst, paint); return result; }
From source file:com.almalence.googsharing.Thumbnail.java
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int size, int pixels) { final int side = Math.min(bitmap.getWidth(), bitmap.getHeight()); final Bitmap bitmapCropped = Bitmap.createBitmap(bitmap, (bitmap.getWidth() - side) / 2, (bitmap.getHeight() - side) / 2, side, side); final Bitmap output = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = 0xffffffff; final Paint paint = new Paint(); final Rect rectSrc = new Rect(0, 0, bitmapCropped.getWidth(), bitmapCropped.getHeight()); final Rect rect = new Rect(6, 6, output.getWidth() - 6, output.getHeight() - 6); final RectF rectF = new RectF(rect); final RectF rectFBorder = new RectF(0, 0, output.getWidth(), output.getHeight()); final float roundPx = pixels; paint.setAntiAlias(true);// w ww. ja v a 2 s . c o m canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); canvas.drawRoundRect(rectF, roundPx, roundPx, paint); paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); canvas.drawBitmap(bitmapCropped, rectSrc, rect, paint); paint.setXfermode(new PorterDuffXfermode(Mode.DST_ATOP)); canvas.drawRoundRect(rectFBorder, roundPx, roundPx, paint); return output; }
From source file:com.dunrite.xpaper.utility.Utils.java
/** * Creates drawable for the wallpaper/* w ww . j av a 2s. com*/ * @param context current app context * @param foreground foreground drawable * @param bgColor color of background * @param fgColor color of foreground * @param isPreview if this is for the preview or not * @return the final, constructed wallpaper */ public static Drawable constructWallpaper(Context context, Drawable foreground, int bgColor, int fgColor, boolean isPreview) { final int WIDTH = 2560; final int HEIGHT = 1440; Canvas comboImage; Bitmap cs, fg; Paint fgPaint = new Paint(); //create bitmap from foreground drawable fg = ((BitmapDrawable) foreground).getBitmap(); if (isPreview) cs = Bitmap.createBitmap(WIDTH / 2, HEIGHT / 2, Bitmap.Config.ARGB_8888); else cs = Bitmap.createBitmap(WIDTH, HEIGHT, Bitmap.Config.ARGB_8888); comboImage = new Canvas(cs); fgPaint.setFilterBitmap(false); fgPaint.setColorFilter(new PorterDuffColorFilter(fgColor, PorterDuff.Mode.SRC_ATOP)); comboImage.drawRGB(Color.red(bgColor), Color.green(bgColor), Color.blue(bgColor)); if (isPreview) comboImage.drawBitmap(Bitmap.createScaledBitmap(fg, WIDTH / 2, HEIGHT / 2, true), 0, 0, fgPaint); else comboImage.drawBitmap(fg, 0, 0, fgPaint); return new BitmapDrawable(context.getResources(), cs); }
From source file:Main.java
private static synchronized Bitmap createScaledBitmap(Bitmap bitmap, final int width, final int height, float cornerRadius) { if (bitmap == null) { return null; }/*www. j a v a 2 s .c om*/ int adjustedWidth = width; int adjustedHeight = height; final int bitmapWidth = bitmap.getWidth(); final int bitmapHeight = bitmap.getHeight(); //int inBytes = bitmap.getByteCount(); if (width >= bitmapWidth && height >= bitmapHeight) return bitmap; if (width > 0 && height > 0) { //if (width < bitmapWidth || height < bitmapHeight) { final float ratio = (float) bitmapWidth / bitmapHeight; if (bitmapWidth > bitmapHeight) { adjustedHeight = (int) (width / ratio); } else if (bitmapHeight > bitmapWidth) { adjustedWidth = (int) (height * ratio); } final Bitmap.Config c = Bitmap.Config.ARGB_8888; final Bitmap thumb = Bitmap.createBitmap(width, height, c); final Canvas canvas = sScaleCanvas; final Paint paint = sPaint; canvas.setBitmap(thumb); paint.setDither(false); paint.setFilterBitmap(true); Rect sBounds = new Rect(); Rect sOldBounds = new Rect(); sBounds.set((width - adjustedWidth) >> 1, (height - adjustedHeight) >> 1, adjustedWidth, adjustedHeight); sOldBounds.set(0, 0, bitmapWidth, bitmapHeight); if (cornerRadius != 0) { //Path p = new Path(); RectF rect = new RectF(sBounds); canvas.drawARGB(0, 0, 0, 0); paint.setColor(Color.WHITE); canvas.drawRoundRect(rect, cornerRadius, cornerRadius, paint); paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); //p.addRoundRect(rect, cornerRadius, cornerRadius, Direction.CCW); //canvas.clipPath(p, Op.REPLACE); } else { paint.setXfermode(null); //canvas.clipRect(0, 0, thumb.getWidth(), thumb.getHeight()); } canvas.drawBitmap(bitmap, sOldBounds, sBounds, paint); canvas.setBitmap(Bitmap.createBitmap(1, 1, Config.ALPHA_8)); return thumb; } return bitmap; }
From source file:com.ibuildapp.romanblack.FanWallPlugin.data.Statics.java
/** * Sets the downloaded avatar.// w w w . j av a 2 s .co m * * @param filePath file path to avatar image */ public static Bitmap publishAvatar(String filePath) { Bitmap bitmap = null; if (!TextUtils.isEmpty(filePath)) { try { BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inSampleSize = 1; bitmap = BitmapFactory.decodeFile(filePath, opts); int size = 0; if (bitmap.getHeight() > bitmap.getWidth()) { size = bitmap.getWidth(); } else { size = bitmap.getHeight(); } Bitmap output = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = 0xff424242; final Paint paint = new Paint(); final Rect rect = new Rect(0, 0, size, size); final RectF rectF = new RectF(rect); final float roundPx = 12; paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); canvas.drawRoundRect(rectF, roundPx, roundPx, paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect, paint); bitmap.recycle(); return output; } catch (Exception e) { } } return null; }
From source file:com.ecuamobi.deckwallet.util.Renderer.java
static void printWallet(final Activity context, final String label, final String addressUri, final String privateKey) { new AsyncTask<Void, Void, Bitmap>() { @Override//from www . j a va 2 s . c o m protected Bitmap doInBackground(Void... params) { TextPaint textPaint = new TextPaint(); textPaint.setAntiAlias(true); textPaint.setColor(0xFF000000); final int bitmapMargin = 100;//big margin is to prevent possible clipping final int textHeight = 28; final int spaceBetweenQrCodes = 60; textPaint.setTextSize(textHeight); textPaint.setTextAlign(Paint.Align.CENTER); final int qrCodePadding = (int) (textPaint.descent() * 2); Rect bounds = new Rect(); textPaint.getTextBounds(privateKey, 0, privateKey.length(), bounds); int textWidth = getTextWidth(privateKey, textPaint); ArrayList<String> labelLinesRelaxed = wrap(label, textWidth, false, textPaint); for (String titleLine : labelLinesRelaxed) { textWidth = Math.max(textWidth, getTextWidth(titleLine, textPaint)); } textWidth = Math.max(textWidth, getTextWidth(addressUri, textPaint)); QRCode privateKeyQrCode = QRCode.getMinimumQRCode(privateKey, ErrorCorrectLevel.M); Bitmap privateKeyQrCodeBitmap = privateKeyQrCode.createImage(textWidth); QRCode addressQrCode = QRCode.getMinimumQRCode(addressUri, ErrorCorrectLevel.M); Bitmap addressQrCodeBitmap = addressQrCode.createImage(textWidth); ArrayList<String> labelLines = wrap(label, textWidth, true, textPaint); Bitmap bmp = Bitmap.createBitmap( textWidth * 2 + bitmapMargin * 2 + spaceBetweenQrCodes, privateKeyQrCodeBitmap.getHeight() + textHeight * (labelLines.size() + 1) + qrCodePadding * 2 + bitmapMargin * 2, Bitmap.Config.RGB_565); Canvas canvas = new Canvas(bmp); Paint paint = new Paint(); paint.setStyle(Paint.Style.FILL); paint.setARGB(0xFF, 0xFF, 0xFF, 0xFF); paint.setAntiAlias(false); canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), paint); int centerXForAddress = bitmapMargin + textWidth / 2; int centerXForPrivateKey = bitmapMargin + textWidth + spaceBetweenQrCodes + textWidth / 2; int y = (int) (bitmapMargin - textPaint.ascent()); for (int i = 0; i < labelLines.size(); i++) { canvas.drawText(labelLines.get(i), centerXForPrivateKey, y + i * textHeight, textPaint); } y = bitmapMargin + labelLines.size() * textHeight + qrCodePadding; Paint qrCodePaint = new Paint(); qrCodePaint.setAntiAlias(false); qrCodePaint.setDither(false); canvas.drawBitmap(addressQrCodeBitmap, centerXForAddress - addressQrCodeBitmap.getWidth() / 2, y, qrCodePaint); canvas.drawBitmap(privateKeyQrCodeBitmap, centerXForPrivateKey - privateKeyQrCodeBitmap.getWidth() / 2, y, qrCodePaint); y += qrCodePadding - textPaint.ascent(); canvas.drawText(addressUri, centerXForAddress, y + addressQrCodeBitmap.getHeight(), textPaint); canvas.drawText(privateKey, centerXForPrivateKey, y + privateKeyQrCodeBitmap.getHeight(), textPaint); return bmp; } @Override protected void onPostExecute(final Bitmap bitmap) { if (bitmap != null) { //DEBUG // android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(context); // android.widget.ImageView view = new android.widget.ImageView(context); // view.setImageBitmap(bitmap); // builder.setView(view); // builder.setPositiveButton(android.R.string.ok, null); // builder.show(); PrintHelper printHelper = new PrintHelper(context); printHelper.setScaleMode(PrintHelper.SCALE_MODE_FIT); printHelper.printBitmap(label, bitmap); } } }.execute(); }
From source file:com.breadwallet.tools.adapter.ParallaxViewPager.java
@Override protected void onDraw(Canvas canvas) { if (bitmap != null) canvas.drawBitmap(bitmap, source, destination, null); }
From source file:com.facebook.react.views.slider.ReactSlider.java
private NinePatchDrawable createNineDrawable(Bitmap src) { if (src.getNinePatchChunk() != null) { return NinePatchDrawableFactory.convertBitmap(getResources(), src, null); }// w ww . jav a2s . c o m Bitmap desc = Bitmap.createBitmap(src.getWidth() + 4, src.getHeight() + 4, Bitmap.Config.ARGB_4444); PointF center = new PointF(desc.getWidth() / 2, desc.getHeight() / 2); Canvas canvas = new Canvas(desc); canvas.drawBitmap(src, 2, 2, null); Paint p = new Paint(); p.setColor(Color.BLACK); canvas.drawLine(center.x, 0, center.x + 1, 0, p); canvas.drawLine(0, center.y, 0, center.y + 1, p); NinePatchDrawable drawable = NinePatchDrawableFactory.convertBitmap(getResources(), desc, null); desc.recycle(); return drawable; }
From source file:com.c4mprod.utils.ImageManager.java
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) { Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = 0xff424242; final Paint paint = new Paint(); final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); final RectF rectF = new RectF(rect); final float roundPx = 20; paint.setAntiAlias(true);/*from www. ja va 2s . c o m*/ 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, rect, rect, paint); if (output != bitmap) { bitmap.recycle(); } return output; }