List of usage examples for android.graphics Paint setAntiAlias
public void setAntiAlias(boolean aa)
From source file:Main.java
public static Bitmap getMosaic(Bitmap bitmap) { int width = bitmap.getWidth(); int height = bitmap.getHeight(); int radius = 10; Bitmap mosaicBitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888); Canvas canvas = new Canvas(mosaicBitmap); int horCount = (int) Math.ceil(width / (float) radius); int verCount = (int) Math.ceil(height / (float) radius); Paint paint = new Paint(); paint.setAntiAlias(true); for (int horIndex = 0; horIndex < horCount; ++horIndex) { for (int verIndex = 0; verIndex < verCount; ++verIndex) { int l = radius * horIndex; int t = radius * verIndex; int r = l + radius; if (r > width) { r = width;// ww w. j a va 2 s. com } int b = t + radius; if (b > height) { b = height; } int color = bitmap.getPixel(l, t); Rect rect = new Rect(l, t, r, b); paint.setColor(color); canvas.drawRect(rect, paint); } } canvas.save(); return mosaicBitmap; }
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 2s . c o 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
/** * Method to scale {@code sourceBitmap}, maintaining the same original size of the bitmap, * but with a transparent frame and the scaled and centered {@code sourceBitmap} inside. * * @return/* w w w .j av a 2 s .co m*/ */ public static Bitmap scaleInsideWithFrame(Bitmap mutableBitmap, float factor, int color) { Bitmap clearBitmap = mutableBitmap.copy(Bitmap.Config.ARGB_8888, true); clearBitmap.eraseColor(color); Bitmap resizedInsideBitmap = scaleBitmapByFactor(mutableBitmap, factor); int frameWidth = clearBitmap.getWidth(); int frameHeight = clearBitmap.getHeight(); int imageWidth = resizedInsideBitmap.getWidth(); int imageHeight = resizedInsideBitmap.getHeight(); Canvas canvas = new Canvas(clearBitmap); Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG); paint.setAntiAlias(true); canvas.drawBitmap(resizedInsideBitmap, (frameWidth - imageWidth) / 2, (frameHeight - imageHeight) / 2, paint); return clearBitmap; }
From source file:Main.java
public static Bitmap getCroppedBitmap(Bitmap bmp, int radius, int border, int color) { Bitmap scaledBitmap;//from ww w. j ava2s .com if (bmp.getWidth() != radius || bmp.getHeight() != radius) { scaledBitmap = ThumbnailUtils.extractThumbnail(bmp, radius - 2, radius - 2); } else { scaledBitmap = bmp; } Bitmap output = Bitmap.createBitmap(scaledBitmap.getWidth(), scaledBitmap.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(output); final Paint paint = new Paint(); paint.setAntiAlias(true); paint.setFilterBitmap(true); paint.setDither(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); canvas.drawCircle(scaledBitmap.getWidth() / 2, scaledBitmap.getHeight() / 2, scaledBitmap.getWidth() / 2, paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); final Rect rect = new Rect(0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight()); canvas.drawBitmap(scaledBitmap, rect, rect, paint); if (border > 0) { paint.setStrokeWidth(border); paint.setStyle(Paint.Style.STROKE); canvas.drawCircle(scaledBitmap.getWidth() / 2, scaledBitmap.getHeight() / 2, scaledBitmap.getWidth() / 2, paint); } return output; }
From source file:Main.java
public static Bitmap getCircularBitmap(Bitmap srcBitmap) { // Calculate the circular bitmap width with border int squareBitmapWidth = Math.min(srcBitmap.getWidth(), srcBitmap.getHeight()); //int squareBitmapWidth = 300; // Initialize a new instance of Bitmap Bitmap dstBitmap = Bitmap.createBitmap(squareBitmapWidth, // Width squareBitmapWidth, // Height Bitmap.Config.ARGB_8888 // Config );/*from w w w . j av a 2s . c om*/ Canvas canvas = new Canvas(dstBitmap); // Initialize a new Paint instance Paint paint = new Paint(); paint.setAntiAlias(true); Rect rect = new Rect(0, 0, squareBitmapWidth, squareBitmapWidth); RectF rectF = new RectF(rect); canvas.drawOval(rectF, paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); // Calculate the left and top of copied bitmap float left = (squareBitmapWidth - srcBitmap.getWidth()) / 2; float top = (squareBitmapWidth - srcBitmap.getHeight()) / 2; canvas.drawBitmap(srcBitmap, left, top, paint); srcBitmap.recycle(); return dstBitmap; }
From source file:Main.java
public static Bitmap punchAHoleInABitmap(Context context, Bitmap foreground, float x1, float y1) { Bitmap bitmap = Bitmap.createBitmap(foreground.getWidth(), foreground.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); Paint paint = new Paint(); canvas.drawBitmap(foreground, 0, 0, paint); paint.setAntiAlias(true); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); float radius = (float) (getScreenSize(context).x * .06); canvas.drawCircle(x1, y1 - 450, radius, paint); return bitmap; }
From source file:Main.java
/** * The most useful answer about text drawing ever. http://stackoverflow.com/a/32081250 */// w ww .j a va 2 s.c o m @Nullable public static Bitmap createTypefaceBitmap(final Context context, @NonNull final String text, final int color, final float textSizePx) { final Typeface robotoMedium = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Medium.ttf"); final Paint paint = new Paint(); paint.setAntiAlias(true); paint.setSubpixelText(true); paint.setTypeface(robotoMedium); paint.setStyle(Paint.Style.FILL); paint.setColor(color); paint.setTextSize(textSizePx); final Rect bounds = new Rect(); paint.getTextBounds(text, 0, text.length(), bounds); Bitmap bitmap = null; if (!bounds.isEmpty()) { final int width = bounds.width(); final int height = bounds.height(); bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444); final float x = bounds.left; final float y = height - bounds.bottom; final Canvas canvas = new Canvas(bitmap); canvas.drawText(text, x, y, paint); } return bitmap; }
From source file:Main.java
public static Bitmap transferMode(Bitmap src, Bitmap mask, Xfermode xfermode) { final int width = mask.getWidth(); final int height = mask.getHeight(); final Bitmap dst = Bitmap.createBitmap(width, height, Config.ARGB_8888); final Canvas canvas = new Canvas(dst); final Paint paint = new Paint(); paint.setAntiAlias(true); final int srcWidth = src.getWidth(); final int srcHeight = src.getHeight(); canvas.save();//from w w w .ja v a 2 s . c o m // Scale down the image first if required. if ((width < srcWidth) || (height < srcHeight)) { float radioX = (float) width / srcWidth; float radioY = (float) height / srcHeight; float radio = 1f; float dx = 0f; float dy = 0f; if (radioX > radioY) { radio = radioX; dy = (height / radioX - srcHeight) / 2.0f; } else { radio = radioY; dx = (width / radioX - srcWidth) / 2.0f; } canvas.scale(radio, radio); canvas.translate(dx, dy); } canvas.drawBitmap(src, 0, 0, paint); canvas.restore(); if (xfermode != null) { paint.setXfermode(xfermode); canvas.drawBitmap(mask, 0, 0, paint); } return dst; }
From source file:Main.java
/** * Create round, coloured bitmap with text embedded. * @param circleColor The color to use.//from w w w. j a va 2 s .co m * @param diameterDP The diameter of the circle. * @param text The text to embed. * @return Bitmap showing a text. */ public static Bitmap generateCircleBitmap(int circleColor, float diameterDP, String text) { /** * * http://stackoverflow.com/questions/31168636/rounded-quickcontactbadge-with-text */ final int textColor = 0xffffffff; DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics(); float diameterPixels = diameterDP * (metrics.densityDpi / 160f); float radiusPixels = diameterPixels / 2; // Create the bitmap Bitmap output = Bitmap.createBitmap((int) diameterPixels, (int) diameterPixels, Bitmap.Config.ARGB_8888); // Create the canvas to draw on Canvas canvas = new Canvas(output); canvas.drawARGB(0, 0, 0, 0); // Draw the circle final Paint paintC = new Paint(); paintC.setAntiAlias(true); paintC.setColor(circleColor); canvas.drawCircle(radiusPixels, radiusPixels, radiusPixels, paintC); // Draw the text if (text != null && text.length() > 0) { final Paint paintT = new Paint(); paintT.setColor(textColor); paintT.setAntiAlias(true); paintT.setTextSize(radiusPixels * 2); paintT.setTypeface(Typeface.SANS_SERIF); final Rect textBounds = new Rect(); paintT.getTextBounds(text, 0, text.length(), textBounds); canvas.drawText(text, radiusPixels - textBounds.exactCenterX(), radiusPixels - textBounds.exactCenterY(), paintT); } return output; }
From source file:Main.java
public static Bitmap createReflectedBitmap(Bitmap srcBitmap, float reflectHeight) { if (null == srcBitmap) { return null; }//from www . j av a 2s .c o m int srcWidth = srcBitmap.getWidth(); int srcHeight = srcBitmap.getHeight(); int reflectionWidth = srcBitmap.getWidth(); int reflectionHeight = reflectHeight == 0 ? srcHeight / 3 : (int) (reflectHeight * srcHeight); if (0 == srcWidth || srcHeight == 0) { return null; } // The matrix Matrix matrix = new Matrix(); matrix.preScale(1, -1); try { // The reflection bitmap, width is same with original's Bitmap reflectionBitmap = Bitmap.createBitmap(srcBitmap, 0, srcHeight - reflectionHeight, reflectionWidth, reflectionHeight, matrix, false); if (null == reflectionBitmap) { return null; } Canvas canvas = new Canvas(reflectionBitmap); Paint paint = new Paint(); paint.setAntiAlias(true); LinearGradient shader = new LinearGradient(0, 0, 0, reflectionBitmap.getHeight(), 0x70FFFFFF, 0x00FFFFFF, Shader.TileMode.MIRROR); paint.setShader(shader); paint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.DST_IN)); // Draw the linear shader. canvas.drawRect(0, 0, reflectionBitmap.getWidth(), reflectionBitmap.getHeight(), paint); return reflectionBitmap; } catch (Exception e) { e.printStackTrace(); } return null; }