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 cropWithCanvas(Bitmap bitmap, Rect cropRect) { Rect destRect = new Rect(0, 0, cropRect.width(), cropRect.height()); Bitmap cropped = Bitmap.createBitmap(cropRect.width(), cropRect.height(), Bitmap.Config.RGB_565); Canvas canvas = new Canvas(cropped); canvas.drawBitmap(bitmap, cropRect, destRect, null); return cropped; }
From source file:Main.java
public static Bitmap cropJpgFile(String inFl, String outFl) throws IOException { Bitmap bmpIn = BitmapFactory.decodeFile(inFl); Bitmap bmOverlay = Bitmap.createBitmap(bmpIn.getWidth(), bmpIn.getHeight(), Bitmap.Config.ARGB_8888); Paint p = new Paint(); p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); Canvas c = new Canvas(bmOverlay); c.drawBitmap(bmpIn, 0, 0, null);/* w w w . j a va 2 s . com*/ //c.drawRect(30, 30, 100, 100, p); File fileOut = new File(outFl); FileOutputStream out = new FileOutputStream(fileOut); bmOverlay = drawTextToBitmap(bmOverlay, "Image Viewer"); bmOverlay.compress(Bitmap.CompressFormat.JPEG, 100, out); return bmOverlay; }
From source file:Main.java
public static Bitmap toGrayscale(Bitmap bmpOriginal) { int width, height; height = bmpOriginal.getHeight();/* w w w . ja va2 s. co m*/ width = bmpOriginal.getWidth(); Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565); Canvas c = new Canvas(bmpGrayscale); Paint paint = new Paint(); ColorMatrix cm = new ColorMatrix(); cm.setSaturation(0); ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm); paint.setColorFilter(f); c.drawBitmap(bmpOriginal, 0, 0, paint); return bmpGrayscale; }
From source file:Main.java
public static Bitmap drawTextToBitmap(Context gContext, String gText, int frontColor, int backColor) { Resources resources = gContext.getResources(); float scale = resources.getDisplayMetrics().density; int w = 1536, h = 1280; Bitmap.Config conf = Bitmap.Config.ARGB_8888; // see other conf types Bitmap bmp = Bitmap.createBitmap(w, h, conf); // this creates a MUTABLE bitmap android.graphics.Bitmap.Config bitmapConfig = bmp.getConfig(); Canvas canvas = new Canvas(bmp); // new antialised Paint Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); // text color - #3D3D3D paint.setColor(frontColor);/*www . j a v a 2s. c o m*/ // text size in pixels paint.setTextSize((int) (400 * scale)); // text shadow //paint.setShadowLayer(1f, 0f, 1f, Color.WHITE); // draw text to the Canvas center if (backColor != -1) { canvas.drawColor(backColor); } Rect bounds = new Rect(); paint.getTextBounds(gText, 0, gText.length(), bounds); int x = (bmp.getWidth() - bounds.width()) / 2; int y = (bmp.getHeight() + bounds.height()) / 2; canvas.drawText(gText, x, y, paint); return bmp; }
From source file:Main.java
public static Bitmap drawableToBitmap(Drawable drawable) { if (drawable instanceof BitmapDrawable) { BitmapDrawable bitmapDrawable = ((BitmapDrawable) drawable); return bitmapDrawable.getBitmap(); } else {/* w ww .j a v a2s . c o m*/ Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565); Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); drawable.draw(canvas); return bitmap; } }
From source file:Main.java
public static Bitmap drawViewToBitmap(View view, int width, int height, float translateX, float translateY, int scaleRatio) { float scale = 1f / scaleRatio; int bmpWidth = (int) (width * scale - translateX / scaleRatio); int bmpHeight = (int) (height * scale - translateY / scaleRatio); Bitmap dest = Bitmap.createBitmap(bmpWidth, bmpHeight, Bitmap.Config.ARGB_8888); Canvas c = new Canvas(dest); c.translate(-translateX / scaleRatio, -translateY / scaleRatio); if (scaleRatio > 1) { c.scale(scale, scale);//from w ww . j av a2s . c o m } view.draw(c); return dest; }
From source file:Main.java
public static Bitmap drawTextCenterToBitmap(Bitmap bitmap, String text, int textSize, int textColor) { android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig(); // set default bitmap config if none if (bitmapConfig == null) { bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888; }/*w w w. j av a 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 - #3D3D3D paint.setColor(textColor); // text size in pixels paint.setTextSize(textSize); // text shadow paint.setShadowLayer(1f, 0f, 1f, Color.WHITE); // draw text to the Canvas center Rect bounds = new Rect(); paint.getTextBounds(text, 0, text.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 * 5; int y = (bitmap.getHeight() + bounds.height()) / 10 * 5; canvas.drawText(text, x, y, paint); return bitmap; }
From source file:Main.java
/** * Convert bitmap to the grayscale//w w w . j a v a 2 s. c om * http://androidsnippets.com/convert-bitmap-to-grayscale * * @param bmpOriginal Original bitmap * @return Grayscale bitmap */ public static Bitmap toGrayscale(Bitmap bmpOriginal) { final int height = bmpOriginal.getHeight(); final int width = bmpOriginal.getWidth(); final Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Config.ARGB_8888); final Canvas c = new Canvas(bmpGrayscale); final Paint paint = new Paint(); final ColorMatrix cm = new ColorMatrix(); cm.setSaturation(0); final ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm); paint.setColorFilter(f); c.drawBitmap(bmpOriginal, 0, 0, paint); return bmpGrayscale; }
From source file:Main.java
/** * Returns a rounded bitmap using specified bitmap image. * /*from w w w. j ava 2s.c o m*/ * @param scaleBitmapImage bitmap to make round image. * @return rounded bitmap */ public static Bitmap getRoundedShape(Bitmap scaleBitmapImage) { if (scaleBitmapImage == null) return null; int targetWidth = (int) DP; int targetHeight = (int) DP; Bitmap targetBitmap = Bitmap.createBitmap(targetWidth, targetHeight, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(targetBitmap); Path path = new Path(); path.addCircle(((float) targetWidth - 1) / 2, ((float) targetHeight - 1) / 2, (Math.min(((float) targetWidth), ((float) targetHeight)) / 2), Path.Direction.CCW); Paint p = new Paint(); p.setAntiAlias(true); canvas.clipPath(path); canvas.drawBitmap(scaleBitmapImage, new Rect(0, 0, scaleBitmapImage.getWidth(), scaleBitmapImage.getHeight()), new Rect(0, 0, targetWidth, targetHeight), p); p.setARGB(255, 16, 18, 16); scaleBitmapImage.recycle(); return targetBitmap; }
From source file:Main.java
public static Bitmap getRoundedCornerBitmap(Context context, Bitmap input, int pixels, int w, int h, boolean squareTL, boolean squareTR, boolean squareBL, boolean squareBR) { bitmapResult = Bitmap.createBitmap(w, h, Config.ARGB_8888); Canvas canvas = new Canvas(bitmapResult); final float densityMultiplier = context.getResources().getDisplayMetrics().density; final int color = 0xff424242; final Paint paint = new Paint(); final Rect rect = new Rect(0 + 5, 0 + 5, w - 5, h - 5); final RectF rectF = new RectF(rect); // make sure that our rounded corner is scaled appropriately final float roundPx = pixels * densityMultiplier; paint.setAntiAlias(true);/*from w ww . j a v a2 s . c om*/ canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); // canvas.drawColor(Color.BLACK); canvas.drawRoundRect(rectF, roundPx, roundPx, paint); // draw rectangles over the corners we want to be square if (squareTL) { canvas.drawRect(0, 0, w / 2, h / 2, paint); } if (squareTR) { canvas.drawRect(w / 2, 0, w, h / 2, paint); } if (squareBL) { canvas.drawRect(0, h / 2, w / 2, h, paint); } if (squareBR) { canvas.drawRect(w / 2, h / 2, w, h, paint); } paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); // paint.setXfermode(new AvoidXfermode(Color.WHITE, 255, // AvoidXfermode.Mode.TARGET)); canvas.drawBitmap(input, 0, 0, paint); return bitmapResult; }