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:com.ecuamobi.deckwallet.util.Renderer.java
public static void printQR(final Activity context, final String addressUri) { new AsyncTask<Void, Void, Bitmap>() { @Override//from ww w . j ava2s . c om 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; textPaint.setTextSize(textHeight); textPaint.setTextAlign(Paint.Align.CENTER); final int qrCodePadding = (int) (textPaint.descent() * 2); int textWidth = getTextWidth(addressUri, textPaint); QRCode addressQrCode = QRCode.getMinimumQRCode(addressUri, ErrorCorrectLevel.M); Bitmap addressQrCodeBitmap = addressQrCode.createImage(textWidth); Bitmap bmp = Bitmap.createBitmap(textWidth + bitmapMargin * 2, addressQrCodeBitmap.getHeight() + 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 y = bitmapMargin + qrCodePadding; Paint qrCodePaint = new Paint(); qrCodePaint.setAntiAlias(false); qrCodePaint.setDither(false); canvas.drawBitmap(addressQrCodeBitmap, centerXForAddress - addressQrCodeBitmap.getWidth() / 2, y, qrCodePaint); y += qrCodePadding - textPaint.ascent(); canvas.drawText(addressUri, centerXForAddress, y + addressQrCodeBitmap.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(addressUri, bitmap); } } }.execute(); }
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);//from w ww .j a v a2s . 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
/** * Return rounded bitmap with top left corner as rounded using specified bitmap and bitmap size in pixels. * //from w w w . j a v a 2 s . c o m * @param bitmap to make rounded * @param pixels size of bitmap * @return slightly rounded bitmap. */ public static Bitmap getRoundedTopLeftCornerBitmap(Bitmap bitmap, int pixels) { 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 = pixels; final Rect topRightRect = new Rect(bitmap.getWidth() / 2, 0, bitmap.getWidth(), bitmap.getHeight() / 2); final Rect bottomRect = new Rect(0, bitmap.getHeight() / 2, bitmap.getWidth(), bitmap.getHeight()); paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); canvas.drawRoundRect(rectF, roundPx, roundPx, paint); // Fill in upper right corner canvas.drawRect(topRightRect, paint); // Fill in bottom corners canvas.drawRect(bottomRect, paint); paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect, paint); return output; }
From source file:com.dunrite.xpaper.utility.Utils.java
public static Drawable combineImages2(Drawable background, Drawable foreground, Drawable deviceMisc, int backgroundCol, int foregroundCol, String type, Context context) { Bitmap cs;//w w w. j a va2 s . c o m Bitmap device = null; int width; int height; //TEXTURE TESTING String textureLocation = ""; Bitmap foregroundTexture = null; //TODO: will need some type of way to know which location to put the texture (foreground/background/both) // String textureLocation = "foreground"; // type = ""; //TODO: will need some type of way to know which foreground texture drawable to pull from // Bitmap foregroundTexture = ((BitmapDrawable) ContextCompat.getDrawable(context, R.drawable.texture_bamboo)).getBitmap(); // foregroundTexture = foregroundTexture.copy(Bitmap.Config.ARGB_8888, true); //convert from drawable to bitmap Bitmap back = ((BitmapDrawable) background).getBitmap(); back = back.copy(Bitmap.Config.ARGB_8888, true); Bitmap fore = ((BitmapDrawable) foreground).getBitmap(); fore = fore.copy(Bitmap.Config.ARGB_8888, true); if (type.equals("device")) { device = ((BitmapDrawable) deviceMisc).getBitmap(); device = device.copy(Bitmap.Config.ARGB_8888, true); } //initialize Canvas if (type.equals("preview") || type.equals("device")) { width = back.getWidth() / 2; height = back.getHeight() / 2; } else { width = back.getWidth(); height = back.getHeight(); } cs = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas comboImage = new Canvas(cs); Paint paint1 = new Paint(); paint1.setFilterBitmap(false); //Filter for Background if (textureLocation.equals("background") || textureLocation.equals("both")) { paint1.setColorFilter(new PorterDuffColorFilter(backgroundCol, PorterDuff.Mode.DST_ATOP)); } else { paint1.setColorFilter(new PorterDuffColorFilter(backgroundCol, PorterDuff.Mode.SRC_ATOP)); } //Filter for Foreground Paint paint2 = new Paint(); paint2.setFilterBitmap(false); if (textureLocation.equals("foreground") || textureLocation.equals("both")) { //DIFFICULT CASE //create new canvas to combine Canvas foreCanvas = new Canvas(fore); //set up paint for texture Paint paintTexture = new Paint(); paintTexture.setFilterBitmap(false); paintTexture.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); //draw our combination foreCanvas.drawBitmap(foregroundTexture, 0, 0, paintTexture); //set up theme for outer image paint2.setColorFilter(new PorterDuffColorFilter(foregroundCol, PorterDuff.Mode.DST_IN)); } else { paint2.setColorFilter(new PorterDuffColorFilter(foregroundCol, PorterDuff.Mode.SRC_ATOP)); } //Draw both images if (type.equals("preview") || type.equals("device")) { if (type.equals("device") && device != null) { comboImage.drawBitmap( Bitmap.createScaledBitmap(device, device.getWidth() / 2, device.getHeight() / 2, true), 0, 0, null); device.recycle(); } comboImage.drawBitmap(Bitmap.createScaledBitmap(back, back.getWidth() / 2, back.getHeight() / 2, true), 0, 0, paint1); comboImage.drawBitmap(Bitmap.createScaledBitmap(fore, fore.getWidth() / 2, fore.getHeight() / 2, true), 0, 0, paint2); } else { comboImage.drawBitmap(back, 0, 0, paint1); comboImage.drawBitmap(fore, 0, 0, paint2); } back.recycle(); fore.recycle(); return new BitmapDrawable(context.getResources(), cs); }
From source file:com.android.gallery3d.filtershow.imageshow.ImageShow.java
private static Bitmap convertToAlphaMask(Bitmap b) { Bitmap a = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ALPHA_8); Canvas c = new Canvas(a); c.drawBitmap(b, 0.0f, 0.0f, null); return a;/* www .j a v a 2 s . co m*/ }
From source file:Main.java
/** * Creates a mutable bitmap from subset of source bitmap, transformed by the optional matrix. *//*from w ww . ja va 2 s . c o m*/ private static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m) { // Re-implement Bitmap createBitmap() to always return a mutable bitmap. Canvas canvas = new Canvas(); Bitmap bitmap; Paint paint; if ((m == null) || m.isIdentity()) { bitmap = Bitmap.createBitmap(width, height, source.getConfig()); paint = null; } else { RectF rect = new RectF(0, 0, width, height); m.mapRect(rect); bitmap = Bitmap.createBitmap(Math.round(rect.width()), Math.round(rect.height()), source.getConfig()); canvas.translate(-rect.left, -rect.top); canvas.concat(m); paint = new Paint(Paint.FILTER_BITMAP_FLAG); if (!m.rectStaysRect()) { paint.setAntiAlias(true); } } bitmap.setDensity(source.getDensity()); canvas.setBitmap(bitmap); Rect srcBounds = new Rect(x, y, x + width, y + height); RectF dstBounds = new RectF(0, 0, width, height); canvas.drawBitmap(source, srcBounds, dstBounds, paint); return bitmap; }
From source file:com.arisprung.tailgate.utilities.FacebookImageLoader.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()); paint.setAntiAlias(true);/*w ww . j av a 2 s .co m*/ canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); // canvas.drawRoundRect(rectF, roundPx, roundPx, paint); canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2, bitmap.getWidth() / 2, paint); paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect, paint); int dens = mContext.getResources().getDisplayMetrics().densityDpi; Bitmap _bmp = Bitmap.createScaledBitmap(output, dens / 2, dens / 2, false); // return _bmp; Bitmap bit = TailGateUtility.drawWhiteFrame(_bmp); return bit; }
From source file:Main.java
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int backgroundColor, int borderColor) { Bitmap output = Bitmap.createBitmap(bitmap.getWidth() + 12, bitmap.getHeight() + 12, Bitmap.Config.ARGB_8888);/* ww w . j a va2s .c om*/ Canvas canvas = new Canvas(output); //canvas.drawARGB(Color.alpha(backgroundColor), Color.red(backgroundColor), Color.green(backgroundColor), Color.blue(backgroundColor)); Paint borderPaint = new Paint(); borderPaint.setAntiAlias(true); borderPaint.setColor(borderColor); borderPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER)); borderPaint.setShadowLayer(2.0f, 0.0f, 2.0f, Color.BLACK); int centerWidth = output.getWidth() / 2; int centerHeight = output.getHeight() / 2; canvas.drawCircle(centerWidth, centerHeight, ((centerWidth + centerHeight) / 2) - 4, borderPaint); Paint paint = new Paint(); paint.setAntiAlias(true); Rect rectS = new Rect(0, 0, output.getWidth() - 12, output.getHeight() - 12); Rect rectD = new Rect(0, 0, output.getWidth(), output.getHeight()); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP)); canvas.drawBitmap(bitmap, rectS, rectD, paint); return output; }
From source file:Main.java
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int color, int cornerDips, int borderDips, Context context, boolean recycleOrig) { Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(output); final int borderSizePx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, (float) borderDips, context.getResources().getDisplayMetrics()); final int cornerSizePx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, (float) cornerDips, context.getResources().getDisplayMetrics()); final Paint paint = new Paint(); final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); final RectF rectF = new RectF(rect); // prepare canvas for transfer paint.setAntiAlias(true);//w w w. j av a 2 s . c o m paint.setColor(0xFFFFFFFF); paint.setStyle(Paint.Style.FILL); canvas.drawARGB(0, 0, 0, 0); canvas.drawRoundRect(rectF, cornerSizePx, cornerSizePx, paint); // draw bitmap paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect, paint); // draw border paint.setColor(color); paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth((float) borderSizePx); canvas.drawRoundRect(rectF, cornerSizePx, cornerSizePx, paint); if (recycleOrig && bitmap != null && !bitmap.isRecycled()) bitmap.recycle(); return output; }
From source file:Main.java
/** * Returns semi-rounded bitmap. This is used for displaying atn promotion images. * /* www .j ava 2 s. c o m*/ * @param context * @param input * @return */ public static Bitmap getSemiRoundedBitmap(Context context, Bitmap input) { Bitmap output = Bitmap.createBitmap(input.getWidth(), input.getHeight(), Config.ARGB_8888); Canvas canvas = new Canvas(output); final float densityMultiplier = context.getResources().getDisplayMetrics().density; final int color = 0xff424242; final Paint paint = new Paint(); final Rect rect = new Rect(0, 0, input.getWidth(), input.getHeight()); final RectF rectF = new RectF(rect); //make sure that our rounded corner is scaled appropriately final float roundPx = densityMultiplier * 10; paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); canvas.drawRoundRect(rectF, roundPx, roundPx, paint); //draw rectangles over the corners we want to be square canvas.drawRect(input.getWidth() / 2, 0, input.getWidth(), input.getHeight() / 2, paint); canvas.drawRect(input.getWidth() / 2, input.getHeight() / 2, input.getWidth(), input.getHeight(), paint); paint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.SRC_IN)); canvas.drawBitmap(input, 0, 0, paint); input.recycle(); return output; }