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
private static Drawable getMoreSuggestionsHint(final Resources res, final float textSize, final int color) { final Paint paint = new Paint(); paint.setAntiAlias(true);//from w w w . j a va2s.c o m paint.setTextAlign(Align.CENTER); paint.setTextSize(textSize); paint.setColor(color); final Rect bounds = new Rect(); paint.getTextBounds(MORE_SUGGESTIONS_HINT, 0, MORE_SUGGESTIONS_HINT.length(), bounds); final int width = Math.round(bounds.width() + 0.5f); final int height = Math.round(bounds.height() + 0.5f); final Bitmap buffer = Bitmap.createBitmap(width, (height * 3 / 2), Bitmap.Config.ARGB_8888); final Canvas canvas = new Canvas(buffer); canvas.drawText(MORE_SUGGESTIONS_HINT, width / 2, height, paint); BitmapDrawable bitmapDrawable = new BitmapDrawable(res, buffer); bitmapDrawable.setTargetDensity(canvas); return bitmapDrawable; }
From source file:Main.java
public static Bitmap drawTextToBitmap(Bitmap bitmap, String gText) { //Resources resources = gContext.getResources(); //float scale = resources.getDisplayMetrics().density; android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig(); // set default bitmap config if none if (bitmapConfig == null) { bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888; }/*from w ww.j av a 2 s. 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 - #3D3D3D paint.setColor(Color.rgb(61, 61, 61)); // text size in pixels paint.setTextSize((int) (21)); //* scale)); // text shadow paint.setShadowLayer(2f, 1f, 1f, Color.WHITE); // draw text to the Canvas center //Rect bounds = new Rect(); //paint.getTextBounds(gText, 0, gText.length(), bounds); int x = bitmap.getWidth() - 150;//bounds.width()) - 150; int y = bitmap.getHeight() - 27;//bounds.height()) - 30; // fill canvas.drawRect(x, y, x + 150, y + 27, paint); canvas.drawText(gText, x, y + 20, paint); return bitmap; }
From source file:Main.java
/** * Gets the rounded corner bitmap.//w ww. j av a2 s.c om * * @param bitmap * the bitmap * @return the rounded corner bitmap */ public static Bitmap getRoundedCornerBitmap(Context context, Bitmap bitmap, Boolean create_circle) { DisplayMetrics mMetrics = context.getResources().getDisplayMetrics(); float mScaleFactor = mMetrics.density; Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = Color.BLACK; final Paint paint = new Paint(); int width = bitmap.getWidth(); int height = (bitmap.getHeight() > width) ? width : bitmap.getHeight(); final Rect rect = new Rect(0, 0, width, height); final RectF rectF = new RectF(rect); final float roundPx = (create_circle) ? (bitmap.getWidth() > 360) ? bitmap.getWidth() : 360 : 2; paint.setAntiAlias(true); paint.setColor(color); paint.setStyle(Paint.Style.FILL); canvas.drawARGB(0, 0, 0, 0); canvas.drawRoundRect(rectF, roundPx, roundPx, paint); paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect, paint); // draw border paint.setColor(Color.parseColor("#cccccc")); paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(0.5F * mScaleFactor); canvas.drawRoundRect(rectF, roundPx, roundPx, paint); return output; }
From source file:Main.java
public static int colorDrawableGetColor(ColorDrawable d) { try {//from w ww . j av a2s. c o m Method getColor = d.getClass().getMethod("getColor"); return (Integer) getColor.invoke(d); } catch (NoSuchMethodException ignore) { } catch (InvocationTargetException ignore) { } catch (IllegalAccessException ignore) { } catch (ClassCastException ignore) { } // For some reason the following doesn't work on Android 15, but the above does, and the below // works for Android <= 10, so the function as a whole works but is a dirty hack. Bitmap b = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); d.draw(new Canvas(b)); return b.getPixel(0, 0); }
From source file:Main.java
public static Bitmap drawableToBitmap(Drawable drawable) { if (drawable == null) { return null; }/*ww w. j ava 2s. c om*/ int w = drawable.getIntrinsicWidth(); int h = drawable.getIntrinsicHeight(); Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565; 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
/** * Frames the input bitmap in a circle.//w w w.j av a 2 s. com */ public static Bitmap frameBitmapInCircle(Bitmap input) { if (input == null) { return null; } // Crop the image if not squared. int inputWidth = input.getWidth(); int inputHeight = input.getHeight(); int targetX, targetY, targetSize; if (inputWidth >= inputHeight) { targetX = inputWidth / 2 - inputHeight / 2; targetY = 0; targetSize = inputHeight; } else { targetX = 0; targetY = inputHeight / 2 - inputWidth / 2; targetSize = inputWidth; } // Create an output bitmap and a canvas to draw on it. Bitmap output = Bitmap.createBitmap(targetSize, targetSize, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(output); // Create a black paint to draw the mask. Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setColor(Color.BLACK); // Draw a circle. canvas.drawCircle(targetSize / 2, targetSize / 2, targetSize / 2, paint); // Replace the black parts of the mask with the input image. paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); canvas.drawBitmap(input, targetX /* left */, targetY /* top */, paint); return output; }
From source file:Main.java
/** * Create a Bitmap that will be used as a mask to create the preview window in the background. * The Bitmap will be the given width and height. The Bitmap will be transparent except for a * gradient on the left and bottom sides. * * @param width Width of the mask.//from w w w . jav a2s . com * @param height Height of the mask. * @param gradientSize Size of the gradient. * @return Bitmap mask that will be used to create a preview window. */ private static Bitmap createPreviewWindowMask(int width, int height, int gradientSize) { // Do nothing if the size is invalid if (width <= 0 || height <= 0) { return null; } // Initialize the mask Bitmap mask = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(mask); canvas.drawColor(Color.TRANSPARENT); // If the gradient size is zero, don't draw the gradients if (gradientSize <= 0) { return mask; } // Calculate gradient rects Rect leftGradientRect = new Rect(0, 0, gradientSize, height - gradientSize); Rect bottomGradientRect = new Rect(leftGradientRect.right, height - gradientSize, width, height); Rect cornerGradientRect = new Rect(leftGradientRect.left, leftGradientRect.bottom, bottomGradientRect.left, bottomGradientRect.bottom); // Create left gradient Paint leftGradientPaint = new Paint(); leftGradientPaint.setDither(true); leftGradientPaint.setShader(new LinearGradient(leftGradientRect.left, 0, leftGradientRect.right, 0, Color.BLACK, Color.TRANSPARENT, Shader.TileMode.CLAMP)); // Create right gradient Paint bottomGradientPaint = new Paint(); bottomGradientPaint.setDither(true); bottomGradientPaint.setShader( new LinearGradient(leftGradientRect.right, bottomGradientRect.bottom, leftGradientRect.right, bottomGradientRect.top, Color.BLACK, Color.TRANSPARENT, Shader.TileMode.CLAMP)); // Create corner gradient Paint cornerGradientPaint = new Paint(); cornerGradientPaint.setDither(true); cornerGradientPaint.setShader(new RadialGradient(cornerGradientRect.right, cornerGradientRect.top, gradientSize, Color.TRANSPARENT, Color.BLACK, Shader.TileMode.CLAMP)); // Draw the gradients canvas.drawRect(leftGradientRect, leftGradientPaint); canvas.drawRect(bottomGradientRect, bottomGradientPaint); canvas.drawRect(cornerGradientRect, cornerGradientPaint); return mask; }
From source file:Main.java
public static int filter(final ColorFilter filter, final int color) { final Paint paint = new Paint(); paint.setColor(color);//from w ww.j av a 2 s . co m paint.setColorFilter(filter); final Bitmap bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); final Canvas canvas = new Canvas(bitmap); canvas.drawPaint(paint); return bitmap.getPixel(0, 0); }
From source file:Main.java
/** * This method is used to create bitmap with rounded corners. * /* w w w .j a va 2 s .c o m*/ * @param bitmap * which is to be rouded. * @param pixels * dimension of the resultant image * @return new bitmap with the specified dimension */ public static Bitmap getRoundedCornerBitmap(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; paint.setAntiAlias(true); 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); 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. *//*from w w w. j a va 2 s . c om*/ 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); final RectF dst = new RectF(0, 0, targetWidth, targetHeight); canvas.drawOval(dst, 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); canvas.drawBitmap(input, src, dst, paint); return result; }