List of usage examples for android.graphics Paint Paint
public Paint()
From source file:Main.java
public static Bitmap scaleCenterCrop(Bitmap source, int newHeight, int newWidth) { int sourceWidth = source.getWidth(); int sourceHeight = source.getHeight(); // Compute the scaling factors to fit the new height and width, respectively. // To cover the final image, the final scaling will be the bigger // of these two. float xScale = (float) newWidth / sourceWidth; float yScale = (float) newHeight / sourceHeight; float scale = Math.max(xScale, yScale); // Now get the size of the source bitmap when scaled float scaledWidth = scale * sourceWidth; float scaledHeight = scale * sourceHeight; // Let's find out the upper left coordinates if the scaled bitmap // should be centered in the new size give by the parameters float left = (newWidth - scaledWidth) / 2; float top = (newHeight - scaledHeight) / 2; // The target rectangle for the new, scaled version of the source bitmap will now // be//from w w w .ja v a 2 s .co m RectF targetRect = new RectF(left, top, left + scaledWidth, top + scaledHeight); // Finally, we create a new bitmap of the specified size and draw our new, // scaled bitmap onto it. Bitmap dest = Bitmap.createBitmap(newWidth, newHeight, CONFIG); Canvas canvas = new Canvas(dest); Paint paint = new Paint(); paint.setAntiAlias(true); paint.setFilterBitmap(true); paint.setDither(true); canvas.drawBitmap(source, null, targetRect, paint); return dest; }
From source file:Main.java
/** * --------------------------------------------------------------------------- * USE THIS METHOD AND NOT THE OLDER VERSION CALLED: "overlayColorOnGrayScale" * --------------------------------------------------------------------------- * Method to overlay color on a gray scale Bitmap. * This method creates automatically a gray scale bitmap from {@code source}. * * @param source The original colored Bitmap. * @param color Color to overlay./*from www . j ava2s . c om*/ * @return A colored gray scale Bitmap. */ public static Bitmap overlayColorOnGrayScale(Bitmap source, int color) { Bitmap newBitmap = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888); Bitmap mutableBitmap = newBitmap.copy(Bitmap.Config.ARGB_8888, true); Canvas canvas = new Canvas(mutableBitmap); canvas.drawBitmap(source, 0, 0, getGrayScalePaint()); Paint paint = new Paint(); paint.setAntiAlias(true); ColorFilter filter = new LightingColorFilter(color, 1); paint.setColorFilter(filter); canvas.drawBitmap(mutableBitmap, 0, 0, paint); return mutableBitmap; }
From source file:Main.java
public static Bitmap toReflectionBitmap(Bitmap bitmap) { if (bitmap == null) { return null; }// w w w .ja va 2s. c o m try { int reflectionGap = 1; int width = bitmap.getWidth(); int height = bitmap.getHeight(); // This will not scale but will flip on the Y axis Matrix matrix = new Matrix(); matrix.preScale(1, -1); // Create a Bitmap with the flip matrix applied to it. // We only want the bottom half of the image Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height / 2, width, height / 2, matrix, false); // Create a new bitmap with same width but taller to fit // reflection Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2), Bitmap.Config.ARGB_8888); // Create a new Canvas with the bitmap that's big enough for // the image plus gap plus reflection Canvas canvas = new Canvas(bitmapWithReflection); // Draw in the original image canvas.drawBitmap(bitmap, 0, 0, null); // Draw in the gap Paint deafaultPaint = new Paint(); canvas.drawRect(0, height, width, height + reflectionGap, deafaultPaint); // Draw in the reflection canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null); // Create a shader that is a linear gradient that covers the // reflection Paint paint = new Paint(); LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0, bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, Shader.TileMode.CLAMP); // Set the paint to use this shader (linear gradient) paint.setShader(shader); // Set the Transfer mode to be porter duff and destination in paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN)); // Draw a rectangle using the paint with our linear gradient canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint); bitmap = bitmapWithReflection; } catch (Exception e) { e.printStackTrace(); } return bitmap; }
From source file:Main.java
/** * Save jpeg image with background color * @param strFileName Save file path/*from w w w . j a va2 s .c om*/ * @param bitmap Input bitmap * @param nQuality Jpeg quality for saving * @param nBackgroundColor background color * @return whether success or not */ public static boolean saveBitmapJPEGWithBackgroundColor(String strFileName, Bitmap bitmap, int nQuality, int nBackgroundColor) { boolean bSuccess1 = false; boolean bSuccess2 = false; boolean bSuccess3; File saveFile = new File(strFileName); if (saveFile.exists()) { if (!saveFile.delete()) return false; } int nA = (nBackgroundColor >> 24) & 0xff; // If Background color alpha is 0, Background color substitutes as white if (nA == 0) nBackgroundColor = 0xFFFFFFFF; Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); Bitmap newBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888); Canvas canvas = new Canvas(newBitmap); canvas.drawColor(nBackgroundColor); canvas.drawBitmap(bitmap, rect, rect, new Paint()); // Quality limitation min/max if (nQuality < 10) nQuality = 10; else if (nQuality > 100) nQuality = 100; OutputStream out = null; try { bSuccess1 = saveFile.createNewFile(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try { out = new FileOutputStream(saveFile); bSuccess2 = newBitmap.compress(CompressFormat.JPEG, nQuality, out); } catch (Exception e) { e.printStackTrace(); } try { if (out != null) { out.flush(); out.close(); bSuccess3 = true; } else bSuccess3 = false; } catch (IOException e) { e.printStackTrace(); bSuccess3 = false; } finally { if (out != null) { try { out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } return (bSuccess1 && bSuccess2 && bSuccess3); }
From source file:Main.java
public static Bitmap createRoundCornerBitmap(Bitmap bitmap, float roundPx, boolean lt, boolean rt, boolean lb, boolean rb) { Bitmap roundCornerBitmap = createRoundCornerBitmap(bitmap, roundPx); Canvas canvas = new Canvas(roundCornerBitmap); canvas.drawARGB(0, 0, 0, 0);//from ww w.ja v a 2 s. c o m int bw = bitmap.getWidth(); int bh = bitmap.getHeight(); int centerW = bw / 2; int centerH = bh / 2; Paint paint = new Paint(); Paint defaultPaint = new Paint(); int color = 0xff424242; paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); paint.setAntiAlias(true); paint.setColor(color); Bitmap cutBitmap = null; Canvas cutCanvas = null; if (!lt) { cutBitmap = Bitmap.createBitmap(bw, bh, Bitmap.Config.ARGB_8888); cutCanvas = new Canvas(cutBitmap); cutCanvas.drawRect(0, 0, centerW, centerH, defaultPaint); cutCanvas.drawBitmap(bitmap, 0, 0, paint); canvas.drawBitmap(cutBitmap, 0, 0, defaultPaint); cutBitmap.recycle(); } if (!rt) { cutBitmap = Bitmap.createBitmap(bw, bh, Bitmap.Config.ARGB_8888); cutCanvas = new Canvas(cutBitmap); cutCanvas.drawRect(centerW, 0, bw, centerH, defaultPaint); cutCanvas.drawBitmap(bitmap, 0, 0, paint); canvas.drawBitmap(cutBitmap, 0, 0, defaultPaint); cutBitmap.recycle(); } if (!lb) { cutBitmap = Bitmap.createBitmap(bw, bh, Bitmap.Config.ARGB_8888); cutCanvas = new Canvas(cutBitmap); cutCanvas.drawRect(0, centerH, centerW, bh, defaultPaint); cutCanvas.drawBitmap(bitmap, 0, 0, paint); canvas.drawBitmap(cutBitmap, 0, 0, defaultPaint); cutBitmap.recycle(); } if (!rb) { cutBitmap = Bitmap.createBitmap(bw, bh, Bitmap.Config.ARGB_8888); cutCanvas = new Canvas(cutBitmap); cutCanvas.drawRect(centerW, centerH, bw, bh, defaultPaint); cutCanvas.drawBitmap(bitmap, 0, 0, paint); canvas.drawBitmap(cutBitmap, 0, 0, defaultPaint); cutBitmap.recycle(); } return roundCornerBitmap; }
From source file:Main.java
/** * Save jpeg image with background color * @param strFileName Save file path//from w ww . j a v a2s .c om * @param bitmap Input bitmap * @param nQuality Jpeg quality for saving * @param nBackgroundColor background color * @return whether success or not */ public static boolean saveBitmapJPEGWithBackgroundColor(String strFileName, Bitmap bitmap, int nQuality, int nBackgroundColor) { boolean bSuccess1 = false; boolean bSuccess2 = false; boolean bSuccess3; File saveFile = new File(strFileName); if (saveFile.exists()) { if (!saveFile.delete()) return false; } int nA = (nBackgroundColor >> 24) & 0xff; // If Background color alpha is 0, Background color substitutes as white if (nA == 0) nBackgroundColor = 0xFFFFFFFF; Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); Bitmap newBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888); Canvas canvas = new Canvas(newBitmap); canvas.drawColor(nBackgroundColor); canvas.drawBitmap(newBitmap, rect, rect, new Paint()); canvas.drawBitmap(bitmap, rect, rect, new Paint()); // Quality limitation min/max if (nQuality < 10) nQuality = 10; else if (nQuality > 100) nQuality = 100; OutputStream out = null; try { bSuccess1 = saveFile.createNewFile(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try { out = new FileOutputStream(saveFile); bSuccess2 = newBitmap.compress(CompressFormat.JPEG, nQuality, out); } catch (Exception e) { e.printStackTrace(); } try { if (out != null) { out.flush(); out.close(); bSuccess3 = true; } else bSuccess3 = false; } catch (IOException e) { e.printStackTrace(); bSuccess3 = false; } finally { if (out != null) { try { out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } return (bSuccess1 && bSuccess2 && bSuccess3); }
From source file:Main.java
public static Bitmap toGrayScale(Bitmap bmpOriginal) { int width, height; height = bmpOriginal.getHeight();/* w ww.j a v a 2s. c o 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
/** * This method takes a square bitmap and clips it into a circle * * @param bitmap : the image to clip/*from ww w . j av a2 s . c o m*/ * @return the clipped bitmap */ public static Bitmap getCroppedBitmap(Bitmap bitmap) { Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.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()); paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); // canvas.drawRoundRect(rectF, roundPx, roundPx, paint); canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2, bitmap.getWidth() / 2, paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect, paint); //Bitmap _bmp = Bitmap.createScaledBitmap(output, 60, 60, false); //return _bmp; return output; }
From source file:Main.java
public static Bitmap createRoundedBitmap(Context context, Bitmap bitmap, int leftTop, int rightTop, int leftBottm, int rightBottom) { Bitmap bmp;//ww w .ja va2 s. c om bmp = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888); BitmapShader shader = new BitmapShader(bitmap, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP); float radius = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, leftTop, context.getResources().getDisplayMetrics()); Canvas canvas = new Canvas(bmp); Paint paint = new Paint(); paint.setAntiAlias(true); paint.setShader(shader); RectF rect = new RectF(0, 0, bitmap.getWidth(), bitmap.getHeight()); canvas.drawRoundRect(rect, radius, radius, paint); return bmp; }
From source file:Main.java
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) { if (bitmap == null) return null; Bitmap output;/*from w ww. ja va 2s. c om*/ try { output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.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(PorterDuff.Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect, paint); } catch (Exception ex) { //adding as a precaution because finding a few crashes in bitmap modification return bitmap; } return output; }