List of usage examples for android.graphics Canvas Canvas
public Canvas()
From source file:Main.java
public static Bitmap bitmapFromDrawable(Drawable drawable) { Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565); Canvas canvas = new Canvas(); canvas.setBitmap(bitmap);/*from w ww . j a v a2 s .c o m*/ drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); drawable.draw(canvas); return bitmap; }
From source file:Main.java
static private Bitmap smallCoverPostProc(Bitmap smallBitmap) { try {/*w w w . j a va 2 s . c o m*/ Bitmap smallBitmapPostProc = Bitmap.createBitmap(smallBitmap.getWidth(), smallBitmap.getHeight(), Bitmap.Config.RGB_565); Canvas canvas = new Canvas(); canvas.setBitmap(smallBitmapPostProc); Paint paint = new Paint(); paint.setAntiAlias(true); Bitmap bitmapToShade = Bitmap.createBitmap(smallBitmap, 2, 2, smallBitmap.getWidth() - 4, smallBitmap.getHeight() - 4); BitmapShader bmShader = new BitmapShader(bitmapToShade, TileMode.CLAMP, TileMode.CLAMP); paint.setShader(bmShader); canvas.drawRoundRect(new RectF(2, 2, smallBitmap.getWidth() - 2, smallBitmap.getHeight() - 2), 4, 4, paint); return smallBitmapPostProc; } catch (Exception e) { e.printStackTrace(); return null; } }
From source file:Main.java
public static byte[] makeFontBitmap(String font, String code, int size, float[] arrayOfPos) { Canvas c = new Canvas(); Paint p = new Paint(); float density = context.getResources().getDisplayMetrics().density; // Log.v(TAG, String.format("makeFontBitmap called(Java): font=%s code=%s density=%f", font, code, density)); p.setTextSize((float) size * density); p.setAntiAlias(true);//from w w w. j a v a 2 s. c o m Rect textBounds = new Rect(); p.getTextBounds(code, 0, code.length(), textBounds); // Log.v(TAG, String.format("makeFontBitmap textBounds: %d,%d,%d,%d", textBounds.left, textBounds.top, textBounds.right, textBounds.bottom)); Rect textBoundsAxA = new Rect(); String axa = String.format("A%sA", code); p.getTextBounds(axa, 0, axa.length(), textBoundsAxA); Rect textBoundsAA = new Rect(); String aa = "AA"; p.getTextBounds(aa, 0, aa.length(), textBoundsAA); // cache.distDelta = Vec2(0, 0); arrayOfPos[0] = textBounds.left; arrayOfPos[1] = textBounds.top; // cache.srcWidth = Vec2(16, 16); arrayOfPos[2] = textBounds.width(); arrayOfPos[3] = textBounds.height(); // cache.step = 16; // arrayOfPos[4] = textBounds.width() + 1; arrayOfPos[4] = textBoundsAxA.width() - textBoundsAA.width(); if (textBounds.width() == 0 || textBounds.height() == 0) { Log.v(TAG, "makeFontBitmap: empty"); return null; } Bitmap b = Bitmap.createBitmap(textBounds.width(), textBounds.height(), Bitmap.Config.ARGB_8888); c.setBitmap(b); Rect r = new Rect(0, 0, textBounds.width(), textBounds.height()); // p.setColor(Color.RED); p.setARGB(0, 0, 0, 0); c.drawRect(r, p); p.setARGB(255, 255, 255, 255); // Log.v(TAG, "makeFontBitmap: drawText"); c.drawText(code, -textBounds.left, -textBounds.top, p); // Log.v(TAG, String.format("makeFontBitmap: w=%.2f h=%.2f", arrayOfPos[2], arrayOfPos[3])); ByteBuffer buf = ByteBuffer.allocate(textBounds.width() * textBounds.height() * 4); // Log.v(TAG, String.format("makeFontBitmap: b.getRowBytes() %d", b.getRowBytes())); buf.position(0); b.copyPixelsToBuffer(buf); // Log.v(TAG, String.format("makeFontBitmap results: capacity=%d", buf.capacity())); return buf.array(); }
From source file:Main.java
public static byte[] makeFontBitmap(String font, String code, int size, float[] arrayOfPos) { Log.v(TAG, String.format("makeFontBitmap called(Java): font=%s code=%s", font, code)); Canvas c = new Canvas(); Paint p = new Paint(); // Log.v(TAG, "get density"); float density = context.getResources().getDisplayMetrics().density; Log.v(TAG, String.format("makeFontBitmap density: %f", density)); p.setTextSize((float) size * density); p.setAntiAlias(true);/*w w w. ja va 2 s . com*/ Rect textBounds = new Rect(); p.getTextBounds(code, 0, code.length(), textBounds); Log.v(TAG, String.format("makeFontBitmap textBounds: %d,%d,%d,%d", textBounds.left, textBounds.top, textBounds.right, textBounds.bottom)); Rect textBoundsAxA = new Rect(); String axa = String.format("A%sA", code); p.getTextBounds(axa, 0, axa.length(), textBoundsAxA); Rect textBoundsAA = new Rect(); String aa = "AA"; p.getTextBounds(aa, 0, aa.length(), textBoundsAA); // cache.distDelta = Vec2(0, 0); arrayOfPos[0] = textBounds.left; arrayOfPos[1] = textBounds.top; // cache.srcWidth = Vec2(16, 16); arrayOfPos[2] = textBounds.width(); arrayOfPos[3] = textBounds.height(); // cache.step = 16; // arrayOfPos[4] = textBounds.width() + 1; arrayOfPos[4] = textBoundsAxA.width() - textBoundsAA.width(); if (textBounds.width() == 0 || textBounds.height() == 0) { Log.v(TAG, "makeFontBitmap: empty"); return null; } Bitmap b = Bitmap.createBitmap(textBounds.width(), textBounds.height(), Bitmap.Config.ARGB_8888); c.setBitmap(b); Rect r = new Rect(0, 0, textBounds.width(), textBounds.height()); // p.setColor(Color.RED); p.setARGB(0, 0, 0, 0); c.drawRect(r, p); p.setARGB(255, 255, 255, 255); // Log.v(TAG, "makeFontBitmap: drawText"); c.drawText(code, -textBounds.left, -textBounds.top, p); Log.v(TAG, String.format("makeFontBitmap: w=%.2f h=%.2f", arrayOfPos[2], arrayOfPos[3])); ByteBuffer buf = ByteBuffer.allocate(textBounds.width() * textBounds.height() * 4); // ByteBuffer buf = ByteBuffer.allocate(b.getRowBytes()); Log.v(TAG, String.format("makeFontBitmap: b.getRowBytes() %d", b.getRowBytes())); buf.position(0); b.copyPixelsToBuffer(buf); Log.v(TAG, String.format("makeFontBitmap results: capacity=%d", buf.capacity())); // byte bytes[] = buf.array(); // for (int i = 0; i < size * size * 2; i++) // bytes[i] = (byte)(Math.random() * 255); return buf.array(); }
From source file:Main.java
/** * Creates a mutable bitmap from subset of source bitmap, transformed by the optional matrix. *//*from w ww .j a v a 2 s.co 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:Main.java
private static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, float offset, boolean clipShadow, Paint paint) { int scaledWidth = width; int scaledHeight = height; final Canvas canvas = new Canvas(); canvas.translate(offset / 2.0f, offset / 2.0f); Bitmap bitmap;// w ww .j a va 2s . c o m final Rect from = new Rect(x, y, x + width, y + height); final RectF to = new RectF(0, 0, width, height); if (m == null || m.isIdentity()) { bitmap = Bitmap.createBitmap(scaledWidth + (int) offset, scaledHeight + (int) (clipShadow ? (offset / 2.0f) : offset), Bitmap.Config.ARGB_8888); paint = null; } else { RectF mapped = new RectF(); m.mapRect(mapped, to); scaledWidth = Math.round(mapped.width()); scaledHeight = Math.round(mapped.height()); bitmap = Bitmap.createBitmap(scaledWidth + (int) offset, scaledHeight + (int) (clipShadow ? (offset / 2.0f) : offset), Bitmap.Config.ARGB_8888); canvas.translate(-mapped.left, -mapped.top); canvas.concat(m); } canvas.setBitmap(bitmap); canvas.drawRect(0.0f, 0.0f, width, height, paint); canvas.drawBitmap(source, from, to, SCALE_PAINT); return bitmap; }
From source file:Main.java
/** * This method calculates maximum size of both width and height of bitmap. * It is twice the device screen diagonal for default implementation (extra quality to zoom image). * Size cannot exceed max texture size./* w ww.j a v a2 s . co m*/ * * @return - max bitmap size in pixels. */ @SuppressWarnings({ "SuspiciousNameCombination", "deprecation" }) public static int calculateMaxBitmapSize(@NonNull Context context) { WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); Display display = wm.getDefaultDisplay(); Point size = new Point(); int width, height; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) { display.getSize(size); width = size.x; height = size.y; } else { width = display.getWidth(); height = display.getHeight(); } int screenDiagonal = (int) Math.sqrt(Math.pow(width, 2) + Math.pow(height, 2)); Canvas canvas = new Canvas(); return Math.min(screenDiagonal * 2, Math.min(canvas.getMaximumBitmapWidth(), canvas.getMaximumBitmapHeight())); }
From source file:Main.java
/** * Returns a Bitmap representing the thumbnail of the specified Bitmap. The * size of the thumbnail is defined by the dimension * android.R.dimen.launcher_application_icon_size. * <p>//w ww. j a v a2 s.com * This method is not thread-safe and should be invoked on the UI thread * only. * * @param bitmap The bitmap to get a thumbnail of. * @param context The application's context. * @return A thumbnail for the specified bitmap or the bitmap itself if the * thumbnail could not be created. */ public static Bitmap createThumbnailBitmap(Bitmap bitmap, Context context) { int sIconWidth = -1; int sIconHeight = -1; final Resources resources = context.getResources(); sIconWidth = sIconHeight = (int) resources.getDimension(android.R.dimen.app_icon_size); final Paint sPaint = new Paint(); final Rect sBounds = new Rect(); final Rect sOldBounds = new Rect(); Canvas sCanvas = new Canvas(); int width = sIconWidth; int height = sIconHeight; sCanvas.setDrawFilter(new PaintFlagsDrawFilter(Paint.DITHER_FLAG, Paint.FILTER_BITMAP_FLAG)); final int bitmapWidth = bitmap.getWidth(); final int bitmapHeight = bitmap.getHeight(); if (width > 0 && height > 0) { if (width < bitmapWidth || height < bitmapHeight) { final float ratio = (float) bitmapWidth / bitmapHeight; if (bitmapWidth > bitmapHeight) { height = (int) (width / ratio); } else if (bitmapHeight > bitmapWidth) { width = (int) (height * ratio); } final Config c = (width == sIconWidth && height == sIconHeight) ? bitmap.getConfig() : Config.ARGB_8888; final Bitmap thumb = Bitmap.createBitmap(sIconWidth, sIconHeight, c); final Canvas canvas = sCanvas; final Paint paint = sPaint; canvas.setBitmap(thumb); paint.setDither(false); paint.setFilterBitmap(true); sBounds.set((sIconWidth - width) / 2, (sIconHeight - height) / 2, width, height); sOldBounds.set(0, 0, bitmapWidth, bitmapHeight); canvas.drawBitmap(bitmap, sOldBounds, sBounds, paint); return thumb; } else if (bitmapWidth < width || bitmapHeight < height) { final Config c = Config.ARGB_8888; final Bitmap thumb = Bitmap.createBitmap(sIconWidth, sIconHeight, c); final Canvas canvas = sCanvas; final Paint paint = sPaint; canvas.setBitmap(thumb); paint.setDither(false); paint.setFilterBitmap(true); canvas.drawBitmap(bitmap, (sIconWidth - bitmapWidth) / 2, (sIconHeight - bitmapHeight) / 2, paint); return thumb; } } return bitmap; }