List of usage examples for android.graphics Canvas drawBitmap
public void drawBitmap(@NonNull Bitmap bitmap, @Nullable Rect src, @NonNull Rect dst, @Nullable Paint paint)
From source file:Main.java
/** * Get a circular (rounded) bitmap shape with the diameter is the smaller between target width and target height. * @param bitmap/*w ww .j a va 2s .c o m*/ * @param width target width * @param height target height * @return Rounded (circular) bitmap width diameter is the smaller between target width and target height. */ public static Bitmap getRoundedBitmap(Bitmap bitmap, int width, int height) { int diameter = width < height ? width : height; Bitmap targetBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(targetBitmap); Path path = new Path(); path.addCircle(width / 2f, height / 2f, diameter / 2f, Path.Direction.CCW); canvas.clipPath(path); Bitmap sourceBitmap = bitmap; Rect destinationRect = new Rect(0, 0, width, height); Rect sourceRect = new Rect(0, 0, sourceBitmap.getWidth(), sourceBitmap.getHeight()); canvas.drawBitmap(sourceBitmap, sourceRect, destinationRect, null); return targetBitmap; }
From source file:Main.java
public static Bitmap generateGrayImage(Bitmap bm) { Bitmap bitmap = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); Paint paint = new Paint(); float[] matrixs = new float[] { 0.33f, 0.59f, 0.11f, 0, 0, 0.33f, 0.59f, 0.11f, 0, 0, 0.33f, 0.59f, 0.11f, 0, 0, 0, 0, 0, 1, 0 };/*from w ww. j av a2s . c o m*/ ColorMatrix colorMatrix = new ColorMatrix(); colorMatrix.set(matrixs); paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix)); canvas.drawBitmap(bm, 0, 0, paint); return bitmap; }
From source file:Main.java
public static Bitmap memoriesImage(Bitmap bm) { Bitmap bitmap = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); Paint paint = new Paint(); float[] matrixs = new float[] { 0.393f, 0.769f, 0.189f, 0, 0, 0.349f, 0.686f, 0.168f, 0, 0, 0.272f, 0.534f, 0.134f, 0, 0, 0, 0, 0, 1, 0 }; ColorMatrix colorMatrix = new ColorMatrix(); colorMatrix.set(matrixs);/*from www .jav a 2 s. co m*/ paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix)); canvas.drawBitmap(bm, 0, 0, paint); return bitmap; }
From source file:Main.java
public static Bitmap bitmap2Round(Bitmap bitmap, float roundPx) { Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888); Canvas canvas = new Canvas(output); final Paint paint = new Paint(); final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); final RectF rectF = new RectF(rect); paint.setAntiAlias(true);// ww w .j a v a 2 s. c o m 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); return output; }
From source file:com.lightbox.android.bitmap.BitmapUtils.java
public static Bitmap createScaledBitmap(Bitmap src, int scaledWidth, int scaledHeight) { Bitmap scaledBitmap = Bitmap.createBitmap(scaledWidth, scaledHeight, src.getConfig()); Canvas canvas = new Canvas(scaledBitmap); canvas.drawBitmap(src, new Rect(0, 0, src.getWidth(), src.getHeight()), new Rect(0, 0, scaledWidth, scaledHeight), sScalePaint); return scaledBitmap; }
From source file:Main.java
public static Bitmap getRoundImage(Bitmap oriImg, boolean recycleOld) { Bitmap targetBitmap = null;// w ww.j av a 2 s.co m if (oriImg != null && !oriImg.isRecycled()) { int size = Math.min(oriImg.getWidth(), oriImg.getHeight()); int targetWidth = size; int targetHeight = size; targetBitmap = Bitmap.createBitmap(targetWidth, targetHeight, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(targetBitmap); Paint paint = new Paint(); paint.setAntiAlias(true); paint.setFilterBitmap(true); canvas.drawARGB(0, 0, 0, 0); canvas.drawCircle(targetWidth / 2f, targetHeight / 2f, targetWidth / 2f, paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); canvas.drawBitmap(oriImg, new Rect(0, 0, size, size), new Rect(0, 0, targetWidth, targetHeight), paint); if (recycleOld) { oriImg.recycle(); } } return targetBitmap; }
From source file:Main.java
public static Bitmap toRoundCorner(Bitmap bitmap) { int height = bitmap.getHeight(); int width = bitmap.getHeight(); Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(output); final Paint paint = new Paint(); final Rect rect = new Rect(0, 0, width, height); paint.setAntiAlias(true);//from ww w.j a va2 s .c o m canvas.drawARGB(0, 0, 0, 0); paint.setColor(Color.TRANSPARENT); canvas.drawCircle(width / 2, height / 2, width / 2, paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect, paint); return output; }
From source file:Main.java
public static Bitmap roundCorners(Bitmap bitmap, float radius) { Paint paint = new Paint(); paint.setAntiAlias(true);/*from w w w . j a v a 2s. com*/ Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig()); Canvas canvas = new Canvas(output); final int color = 0xff424242; final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); final RectF rectF = new RectF(rect); canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); canvas.drawRoundRect(rectF, radius, radius, paint); paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect, paint); return output; }
From source file:com.google.android.libraries.cast.companionlibrary.utils.Utils.java
/** * Scale and center-crop a bitmap to fit the given dimensions. *//*from ww w . j av a 2 s. co m*/ public static Bitmap scaleAndCenterCropBitmap(Bitmap source, int newHeight, int newWidth) { if (source == null) { return null; } int sourceWidth = source.getWidth(); int sourceHeight = source.getHeight(); float xScale = (float) newWidth / sourceWidth; float yScale = (float) newHeight / sourceHeight; float scale = Math.max(xScale, yScale); float scaledWidth = scale * sourceWidth; float scaledHeight = scale * sourceHeight; float left = (newWidth - scaledWidth) / 2; float top = (newHeight - scaledHeight) / 2; RectF targetRect = new RectF(left, top, left + scaledWidth, top + scaledHeight); Bitmap destination = Bitmap.createBitmap(newWidth, newHeight, source.getConfig()); Canvas canvas = new Canvas(destination); canvas.drawBitmap(source, null, targetRect, null); return destination; }
From source file:com.dm.wallpaper.board.helpers.DrawableHelper.java
@Nullable public static Drawable getDefaultImage(@NonNull Context context, @DrawableRes int res, @ColorInt int color, int padding) { try {//from w w w . j av a 2s .c om Drawable drawable = AppCompatDrawableManager.get().getDrawable(context, res); drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { drawable = (DrawableCompat.wrap(drawable)).mutate(); } Bitmap 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); Bitmap tintedBitmap = Bitmap.createBitmap(bitmap.getWidth() + padding, bitmap.getHeight() + padding, Bitmap.Config.ARGB_8888); Canvas tintedCanvas = new Canvas(tintedBitmap); int background = ColorHelper.getAttributeColor(context, R.attr.card_background); Paint paint = new Paint(); paint.setFilterBitmap(true); paint.setAntiAlias(true); tintedCanvas.drawColor(background, PorterDuff.Mode.ADD); tintedCanvas.drawBitmap(bitmap, (tintedCanvas.getWidth() - bitmap.getWidth()) / 2, (tintedCanvas.getHeight() - bitmap.getHeight()) / 2, paint); return new BitmapDrawable(context.getResources(), tintedBitmap); } catch (Exception | OutOfMemoryError e) { return null; } }