List of usage examples for android.graphics Canvas getHeight
public int getHeight()
From source file:Main.java
public static Bitmap setupFrame(Bitmap bitmap, int width, int color) { if (bitmap.getWidth() <= width * 2 || bitmap.getHeight() <= width * 2) { return bitmap; }/* w w w . ja v a2s . c om*/ Bitmap bp = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888); Canvas canvas = new Canvas(bp); canvas.drawBitmap(bitmap, 0, 0, new Paint()); Paint paint = new Paint(); paint.setColor(color); paint.setStrokeWidth(width); paint.setStyle(Style.STROKE); canvas.drawRect(0, 0, canvas.getWidth() - width, canvas.getHeight() - width, paint); bitmap.recycle(); return bp; }
From source file:org.xbmc.kore.utils.Utils.java
/** * Converts a drawable to a bitmap/*from w w w . j a v a 2 s. c o m*/ * @param drawable Drawable to convert * @return Bitmap */ public static Bitmap drawableToBitmap(Drawable drawable, int width, int height) { if (drawable instanceof BitmapDrawable) { return ((BitmapDrawable) drawable).getBitmap(); } Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); drawable.draw(canvas); return bitmap; }
From source file:com.tr4android.support.extension.widget.CircleImageView.java
/** * Helper for creating a bitmap from a drawable * * @param drawable The drawable which should be converted to a bitmap * @return the bitmap containing the drawable *///w w w . ja va 2 s . c o m public static Bitmap getBitmapFromDrawable(Drawable drawable) { if (drawable == null) return null; if (drawable instanceof BitmapDrawable) { Log.w(LOG_TAG, "For better performance consider using setImageBitmap() instead!"); return ((BitmapDrawable) drawable).getBitmap(); } else { Bitmap bitmap = Bitmap.createBitmap(Math.max(2, drawable.getIntrinsicWidth()), Math.max(2, drawable.getIntrinsicHeight()), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); drawable.draw(canvas); return bitmap; } }
From source file:Main.java
public static void clearCanvas(Canvas canvas) { if (USE_DRAWCOLOR_TO_CLEAR_CANVAS) { if (USE_DRAWCOLOR_MODE_CLEAR) { canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR); } else {//from ww w. j a va 2 s . c om canvas.drawColor(Color.TRANSPARENT); } } else { RECT.set(0, 0, canvas.getWidth(), canvas.getHeight()); clearCanvas(canvas, RECT); } }
From source file:com.xxxifan.devbox.core.util.ViewUtils.java
public static Bitmap toBitmap(Drawable drawable) { if (drawable instanceof BitmapDrawable) { return ((BitmapDrawable) drawable).getBitmap(); }//from w ww. ja v a 2 s . c o m Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); drawable.draw(canvas); return bitmap; }
From source file:me.zhang.bingo.Utility.java
public static Bitmap drawableToBitmap(Drawable drawable) { Bitmap bitmap;/*from ww w. ja v a2 s . com*/ if (drawable instanceof BitmapDrawable) { BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable; if (bitmapDrawable.getBitmap() != null) { return bitmapDrawable.getBitmap(); } } if (drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) { // Single color bitmap will be created of 1x1 pixel. bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); } else { bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); } Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); drawable.draw(canvas); return bitmap; }
From source file:com.tafayor.selfcamerashot.taflib.helpers.GraphicsHelper.java
public static Bitmap drawableToBitmap(Drawable drawable, int width, int height) { Bitmap bitmap;/* ww w .j a v a2 s . c om*/ bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); /* The drawable may be composite of bitmap and drawable layers */ if (drawable instanceof BitmapDrawable) { Bitmap bmp = ((BitmapDrawable) drawable).getBitmap(); canvas.drawBitmap(bmp, (canvas.getWidth() - bmp.getWidth()) / 2, (canvas.getHeight() - bmp.getHeight()) / 2, null); } drawable.draw(canvas); return bitmap; }
From source file:org.bottiger.podcast.utils.UIUtils.java
public static Bitmap getBitmapFromVectorDrawable(Context context, int drawableId) { Drawable drawable = ContextCompat.getDrawable(context, drawableId); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { drawable = (DrawableCompat.wrap(drawable)).mutate(); }/*from w ww .j ava 2s. c om*/ Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); drawable.draw(canvas); return bitmap; }
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 w ww . ja v a2 s . com*/ 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: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/*ww w. ja va2 s .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; 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(); }