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 drawableToBitmap(Drawable drawable, float ratio) { int width = (int) Math.ceil(drawable.getIntrinsicWidth() * ratio); int height = (int) Math.ceil(drawable.getIntrinsicHeight() * ratio); Bitmap bitmap = Bitmap.createBitmap(width, height, drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565); Canvas canvas = new Canvas(bitmap); canvas.scale(ratio, ratio);// w ww . j a va 2 s . c om drawable.setBounds(0, 0, width, height); drawable.draw(canvas); return bitmap; }
From source file:Main.java
public static Bitmap toGrayScale(Bitmap bmpOriginal) { int width, height; height = bmpOriginal.getHeight();/* ww w . j a v a 2 s. c om*/ 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 resizeBitmapByScale(final Bitmap bitmap, final float scale, final boolean recycle) { final int width = Math.round(bitmap.getWidth() * scale); final int height = Math.round(bitmap.getHeight() * scale); if (width == bitmap.getWidth() && height == bitmap.getHeight()) return bitmap; final Bitmap target = Bitmap.createBitmap(width, height, getConfig(bitmap)); final Canvas canvas = new Canvas(target); canvas.scale(scale, scale);//from ww w . ja v a2s.co m final Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG); canvas.drawBitmap(bitmap, 0, 0, paint); if (recycle) { bitmap.recycle(); } return target; }
From source file:Main.java
/** * Copy the red channel to a new Bitmap's alpha channel. The old * one will be recycled./*w w w.jav a2s . c om*/ */ static Bitmap redToAlpha(Bitmap src, boolean recycle) { Bitmap dest = Bitmap.createBitmap(src.getWidth(), src.getHeight(), Bitmap.Config.ARGB_8888); float[] matrix = new float[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 }; Paint paint = new Paint(); paint.setColorFilter(new ColorMatrixColorFilter(new ColorMatrix(matrix))); Canvas canvas = new Canvas(dest); canvas.setDensity(Bitmap.DENSITY_NONE); canvas.drawBitmap(src, 0, 0, paint); if (recycle) src.recycle(); return dest; }
From source file:Main.java
public static Bitmap duplicateBitmap(Bitmap bmpSrc) { if (null == bmpSrc) { return null; }/*from w w w . j av a 2 s . c o m*/ int bmpSrcWidth = bmpSrc.getWidth(); int bmpSrcHeight = bmpSrc.getHeight(); Bitmap bmpDest = Bitmap.createBitmap(bmpSrcWidth, bmpSrcHeight, Config.ARGB_8888); if (null != bmpDest) { Canvas canvas = new Canvas(bmpDest); final Rect rect = new Rect(0, 0, bmpSrcWidth, bmpSrcHeight); canvas.drawBitmap(bmpSrc, rect, rect, null); } return bmpDest; }
From source file:Main.java
public static Bitmap createRoundedBitmap(Context context, Bitmap bitmap, int leftTop, int rightTop, int leftBottm, int rightBottom) { Bitmap bmp;// w w w. ja va2s . co m 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 toRoundBitmap(Bitmap bitmap, int round) { if (bitmap == null) return null; int width = bitmap.getWidth(); int height = bitmap.getHeight(); float roundPx; float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom; if (width <= height) { if (round != 0) roundPx = round;/* w ww .j a v a2 s . c om*/ else roundPx = width / 2; top = 0; bottom = width; left = 0; right = width; height = width; dst_left = 0; dst_top = 0; dst_right = width; dst_bottom = width; } else { if (round != 0) roundPx = round; else roundPx = height / 2; float clip = (width - height) / 2; left = clip; right = width - clip; top = 0; bottom = height; width = height; dst_left = 0; dst_top = 0; dst_right = height; dst_bottom = height; } Bitmap output = Bitmap.createBitmap(width, height, Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = 0xff424242; final Paint paint = new Paint(); final Rect src = new Rect((int) left, (int) top, (int) right, (int) bottom); final Rect dst = new Rect((int) dst_left, (int) dst_top, (int) dst_right, (int) dst_bottom); final RectF rectF = new RectF(dst); 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, src, dst, paint); return output; }
From source file:Main.java
/** * Create a bitmap which is wrapped to circle * (similar to what you can see in G+ profile pic.) * * @param bitmap Original Bitmap/* w w w. j a v a 2s . co m*/ * @return Circled bitmap */ public static Bitmap createCircleBitmap(Bitmap bitmap) { final int width = bitmap.getWidth(); final int height = bitmap.getHeight(); BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); Paint bitmapPaint = new Paint(); bitmapPaint.setAntiAlias(true); bitmapPaint.setShader(bitmapShader); Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(output); canvas.drawCircle(width / 2, height / 2, Math.min(width, height) / 2, bitmapPaint); return output; }
From source file:Main.java
public static Bitmap rotateAndFrame(Bitmap bitmap) { final boolean positive = sRandom.nextFloat() >= 0.5f; final float angle = (ROTATION_ANGLE_MIN + sRandom.nextFloat() * ROTATION_ANGLE_EXTRA) * (positive ? 1.0f : -1.0f); final double radAngle = Math.toRadians(angle); final int bitmapWidth = bitmap.getWidth(); final int bitmapHeight = bitmap.getHeight(); final double cosAngle = Math.abs(Math.cos(radAngle)); final double sinAngle = Math.abs(Math.sin(radAngle)); final int strokedWidth = (int) (bitmapWidth + 2 * PHOTO_BORDER_WIDTH); final int strokedHeight = (int) (bitmapHeight + 2 * PHOTO_BORDER_WIDTH); final int width = (int) (strokedHeight * sinAngle + strokedWidth * cosAngle); final int height = (int) (strokedWidth * sinAngle + strokedHeight * cosAngle); final float x = (width - bitmapWidth) / 2.0f; final float y = (height - bitmapHeight) / 2.0f; final Bitmap decored = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); final Canvas canvas = new Canvas(decored); canvas.rotate(angle, width / 2.0f, height / 2.0f); canvas.drawBitmap(bitmap, x, y, sPaint); canvas.drawRect(x, y, x + bitmapWidth, y + bitmapHeight, sStrokePaint); return decored; }
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 ww w . ja v a 2 s. 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); return new BitmapDrawable(res, buffer); }