List of usage examples for android.graphics Canvas Canvas
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) public Canvas(long nativeCanvas)
From source file:Main.java
public static Bitmap drawViewToBitmap(View view, int width, int height, float translateX, float translateY, int downSampling, String color) { float scale = 1f / downSampling; int bmpWidth = (int) (width * scale - translateX / downSampling); int bmpHeight = (int) (height * scale - translateY / downSampling); Bitmap dest = Bitmap.createBitmap(bmpWidth, bmpHeight, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(dest); canvas.translate(-translateX / downSampling, -translateY / downSampling); if (downSampling > 1) { canvas.scale(scale, scale);//from www. j av a2s . c o m } Paint paint = new Paint(); paint.setFlags(Paint.FILTER_BITMAP_FLAG | Paint.ANTI_ALIAS_FLAG); PorterDuffColorFilter filter = new PorterDuffColorFilter(Color.parseColor(color), PorterDuff.Mode.SRC_ATOP); paint.setColorFilter(filter); view.buildDrawingCache(); Bitmap cache = view.getDrawingCache(); canvas.drawBitmap(cache, 0, 0, paint); cache.recycle(); view.destroyDrawingCache(); return dest; }
From source file:Main.java
public static Bitmap drawableToBitmap(Drawable drawable) { int w = drawable.getIntrinsicWidth(); int h = drawable.getIntrinsicHeight(); Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;//from w w w . j ava 2 s . c om Bitmap bitmap = Bitmap.createBitmap(w, h, config); Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, w, h); drawable.draw(canvas); return bitmap; }
From source file:Main.java
/** * mask bitmap//from www. j a v a 2s . c o m * @param src todo * @param mask todo * @param dst todo * @param dstCanvas todo * @param paint todo * @param paintMode todo * @return todo */ public static Bitmap maskBitmap(Bitmap src, Bitmap mask, Bitmap dst, Canvas dstCanvas, Paint paint, PorterDuffXfermode paintMode) { if (dst == null) dst = Bitmap.createBitmap(mask.getWidth(), mask.getHeight(), Config.ARGB_8888); if (dstCanvas == null) dstCanvas = new Canvas(dst); if (paintMode == null) paintMode = new PorterDuffXfermode(Mode.DST_IN); if (paint == null) { paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setXfermode(paintMode); } dstCanvas.drawBitmap(src, 0, 0, null); dstCanvas.drawBitmap(mask, 0, 0, paint); paint.setXfermode(null); return dst; }
From source file:Main.java
/** * Retrieves a copy of the specified drawable resource, rotated by a specified angle. * * @param resources The current resources. * @param resourceId The resource ID of the drawable to rotate. * @param angle The angle of rotation./* ww w. j a v a2 s . c o m*/ * @return Rotated drawable. */ public static Drawable getRotatedDrawable(android.content.res.Resources resources, int resourceId, float angle) { // Get the original drawable and make a copy which will be rotated. Bitmap original = BitmapFactory.decodeResource(resources, resourceId); Bitmap rotated = Bitmap.createBitmap(original.getWidth(), original.getHeight(), Bitmap.Config.ARGB_8888); // Perform the rotation. Canvas tempCanvas = new Canvas(rotated); tempCanvas.rotate(angle, original.getWidth() / 2, original.getHeight() / 2); tempCanvas.drawBitmap(original, 0, 0, null); return new BitmapDrawable(resources, rotated); }
From source file:Main.java
public static Bitmap getRoundCornerBitmap(Bitmap bitmap, int radius) { // Canvas canvas = new Canvas(bitmap); // Rect r =new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); // RectF rect = new RectF(r); // Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); // paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OVER)); // canvas.drawRoundRect(rect, 3f, 3f, paint); // // canvas.drawBitmap(bitmap, 0, 0, paint); // return 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 = radius; paint.setAntiAlias(true);/*from w w w .j a v a 2 s. c om*/ // canvas.drawARGB(0, 0, 0, 0); // paint.setColor(color); canvas.drawRoundRect(rectF, roundPx, roundPx, new Paint(Paint.ANTI_ALIAS_FLAG)); paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect, paint); return output; }
From source file:Main.java
public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap) { if (bitmap == null) { return null; }/*from www . j a v a 2s.com*/ final int reflectionGap = 4; int width = bitmap.getWidth(); int height = bitmap.getHeight(); Matrix matrix = new Matrix(); matrix.preScale(1, -1); Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height * 2 / 3, width, height / 3, matrix, false); Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 3), Config.ARGB_8888); Canvas canvas = new Canvas(bitmapWithReflection); canvas.drawBitmap(bitmap, 0, 0, null); Paint defaultPaint = new Paint(); defaultPaint.setColor(Color.TRANSPARENT); canvas.drawRect(0, height, width, height + reflectionGap, defaultPaint); canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null); Paint paint = new Paint(); LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0, bitmapWithReflection.getHeight() + reflectionGap, 0x88ffffff, 0x00ffffff, TileMode.CLAMP); paint.setShader(shader); paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN)); canvas.drawRect(0, height, width, bitmapWithReflection.getHeight(), paint); return bitmapWithReflection; }
From source file:Main.java
@SuppressWarnings("unused") public static Bitmap drawableToBitmap(Drawable drawable) { Bitmap bitmap;/*from w w w . j a v a2 s. c om*/ if (drawable instanceof BitmapDrawable) { BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable; if (bitmapDrawable.getBitmap() != null) { return bitmapDrawable.getBitmap(); } } if (drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) { bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel } 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:Main.java
public static Bitmap drawTextToBitmap(Context gContext, int gResId, String gText) { Resources resources = gContext.getResources(); float scale = resources.getDisplayMetrics().density; Bitmap bitmap = BitmapFactory.decodeResource(resources, gResId); Bitmap.Config bitmapConfig = bitmap.getConfig(); // set default bitmap config if none if (bitmapConfig == null) { bitmapConfig = Bitmap.Config.ARGB_8888; }//from w w w . ja va 2s . co m // resource bitmaps are imutable, // so we need to convert it to mutable one bitmap = bitmap.copy(bitmapConfig, true); Canvas canvas = new Canvas(bitmap); // new antialised Paint Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); // text color - #808080 paint.setColor(Color.rgb(127, 127, 127)); // text size in pixels paint.setTextSize((int) (14 * scale * 5)); // text shadow paint.setShadowLayer(1f, 0f, 1f, Color.WHITE); // draw text to the Canvas center Rect bounds = new Rect(); paint.getTextBounds(gText, 0, gText.length(), bounds); // int x = (bitmap.getWidth() - bounds.width()) / 2; // int y = (bitmap.getHeight() + bounds.height()) / 2; //draw text to the bottom int x = (bitmap.getWidth() - bounds.width()) / 10 * 9; int y = (bitmap.getHeight() + bounds.height()) / 10 * 9; canvas.drawText(gText, x, y, paint); return bitmap; }
From source file:Main.java
public static Bitmap drawableToBitmap(Drawable drawable) { if (drawable instanceof BitmapDrawable) { return ((BitmapDrawable) drawable).getBitmap(); }/*ww w .java 2 s. c o m*/ int width = drawable.getIntrinsicWidth(); width = width > 0 ? width : 1; int height = drawable.getIntrinsicHeight(); height = height > 0 ? height : 1; 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:Main.java
/** * Returns a bitmap showing a screenshot of the view passed in. *//*w w w .jav a 2s .co m*/ @NonNull static Bitmap getBitmapFromView(@NonNull final View v) { Bitmap bitmap = Bitmap.createBitmap(v.getMeasuredWidth(), v.getMeasuredHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); v.draw(canvas); return bitmap; }