List of usage examples for android.graphics Canvas saveLayer
public int saveLayer(@Nullable RectF bounds, @Nullable Paint paint, @Saveflags int saveFlags)
From source file:Main.java
public static Bitmap createFramedImage(Drawable imageDrawable, int borderThickness) { int size = Math.min(imageDrawable.getMinimumWidth(), imageDrawable.getMinimumHeight()); Bitmap output = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(output); RectF outerRect = new RectF(0, 0, size, size); Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setColor(Color.RED);/*from w ww . j a v a 2 s.c o m*/ canvas.drawRect(outerRect, paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); imageDrawable.setBounds(0, 0, size, size); // Save the layer to apply the paint canvas.saveLayer(outerRect, paint, Canvas.ALL_SAVE_FLAG); imageDrawable.draw(canvas); canvas.restore(); // FRAMING THE PHOTO float border = size / 15f; // 1. Create offscreen bitmap link: http://www.youtube.com/watch?feature=player_detailpage&v=jF6Ad4GYjRU#t=1035s Bitmap framedOutput = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); Canvas framedCanvas = new Canvas(framedOutput); // End of Step 1 // Start - TODO IMPORTANT - this section shouldn't be included in the final code // It's needed here to differentiate step 2 (red) with the background color of the activity // It's should be commented out after the codes includes step 3 onwards // Paint squaredPaint = new Paint(Paint.ANTI_ALIAS_FLAG); // squaredPaint.setColor(Color.BLUE); // framedCanvas.drawRoundRect(outerRect, 0f, 0f, squaredPaint); // End // 2. Draw an opaque rounded rectangle link: RectF innerRect = new RectF(border, border, size - border, size - border); Paint innerPaint = new Paint(Paint.ANTI_ALIAS_FLAG); innerPaint.setColor(Color.RED); // framedCanvas.drawRoundRect(innerRect, cornerRadius, cornerRadius, outerPaint); framedCanvas.drawRect(innerRect, innerPaint); // 3. Set the Power Duff mode link: Paint outerPaint = new Paint(Paint.ANTI_ALIAS_FLAG); outerPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT)); // 4. Draw a translucent rounded rectangle link: outerPaint.setColor(Color.argb(255, 255, 255, 255)); // framedCanvas.drawRoundRect(outerRect, cornerRadius, cornerRadius, outerPaint); framedCanvas.drawRect(outerRect, outerPaint); // 5. Draw the frame on top of original bitmap canvas.drawBitmap(framedOutput, 0f, 0f, null); return output; }
From source file:Main.java
public static Bitmap createRoundedFramedImage(Drawable imageDrawable, int borderThickness) { int size = Math.min(imageDrawable.getMinimumWidth(), imageDrawable.getMinimumHeight()); // Drawable imageDrawable = (image != null) ? new BitmapDrawable(image) : placeHolder; Bitmap output = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(output); RectF outerRect = new RectF(0, 0, size, size); float cornerRadius = size / 18f; Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setColor(Color.RED);//from w ww . j a v a2s. c o m canvas.drawRoundRect(outerRect, cornerRadius, cornerRadius, paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); imageDrawable.setBounds(0, 0, size, size); // Save the layer to apply the paint canvas.saveLayer(outerRect, paint, Canvas.ALL_SAVE_FLAG); imageDrawable.draw(canvas); canvas.restore(); // FRAMING THE PHOTO float border = size / 15f; // 1. Create offscreen bitmap link: http://www.youtube.com/watch?feature=player_detailpage&v=jF6Ad4GYjRU#t=1035s Bitmap framedOutput = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); Canvas framedCanvas = new Canvas(framedOutput); // End of Step 1 // Start - TODO IMPORTANT - this section shouldn't be included in the final code // It's needed here to differentiate step 2 (red) with the background color of the activity // It's should be commented out after the codes includes step 3 onwards // Paint squaredPaint = new Paint(Paint.ANTI_ALIAS_FLAG); // squaredPaint.setColor(Color.BLUE); // framedCanvas.drawRoundRect(outerRect, 0f, 0f, squaredPaint); // End // 2. Draw an opaque rounded rectangle link: // http://www.youtube.com/watch?feature=player_detailpage&v=jF6Ad4GYjRU#t=1044s RectF innerRect = new RectF(border, border, size - border, size - border); Paint innerPaint = new Paint(Paint.ANTI_ALIAS_FLAG); innerPaint.setColor(Color.RED); framedCanvas.drawRoundRect(innerRect, cornerRadius, cornerRadius, innerPaint); // 3. Set the Power Duff mode link: // http://www.youtube.com/watch?feature=player_detailpage&v=jF6Ad4GYjRU#t=1056s Paint outerPaint = new Paint(Paint.ANTI_ALIAS_FLAG); outerPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT)); // 4. Draw a translucent rounded rectangle link: // http://www.youtube.com/watch?feature=player_detailpage&v=jF6Ad4GYjRU outerPaint.setColor(Color.argb(100, 0, 0, 0)); framedCanvas.drawRoundRect(outerRect, cornerRadius, cornerRadius, outerPaint); // 5. Draw the frame on top of original bitmap canvas.drawBitmap(framedOutput, 0f, 0f, null); return output; }
From source file:Main.java
public static Bitmap createRoundedFramedImage(Drawable imageDrawable, int borderThickness, int color) { int size = Math.min(imageDrawable.getMinimumWidth(), imageDrawable.getMinimumHeight()); // Drawable imageDrawable = (image != null) ? new BitmapDrawable(image) : placeHolder; Bitmap output = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(output); RectF outerRect = new RectF(0, 0, size, size); float cornerRadius = size / 18f; Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setColor(Color.RED);//from ww w . ja v a2 s . c o m canvas.drawRoundRect(outerRect, cornerRadius, cornerRadius, paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); imageDrawable.setBounds(0, 0, size, size); // Save the layer to apply the paint canvas.saveLayer(outerRect, paint, Canvas.ALL_SAVE_FLAG); imageDrawable.draw(canvas); canvas.restore(); // FRAMING THE PHOTO float border = size / 15f; // 1. Create offscreen bitmap link: http://www.youtube.com/watch?feature=player_detailpage&v=jF6Ad4GYjRU#t=1035s Bitmap framedOutput = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); Canvas framedCanvas = new Canvas(framedOutput); // End of Step 1 // Start - TODO IMPORTANT - this section shouldn't be included in the final code // It's needed here to differentiate step 2 (red) with the background color of the activity // It's should be commented out after the codes includes step 3 onwards // Paint squaredPaint = new Paint(Paint.ANTI_ALIAS_FLAG); // squaredPaint.setColor(Color.BLUE); // framedCanvas.drawRoundRect(outerRect, 0f, 0f, squaredPaint); // End // 2. Draw an opaque rounded rectangle link: // http://www.youtube.com/watch?feature=player_detailpage&v=jF6Ad4GYjRU#t=1044s RectF innerRect = new RectF(border, border, size - border, size - border); Paint innerPaint = new Paint(Paint.ANTI_ALIAS_FLAG); innerPaint.setColor(Color.RED); framedCanvas.drawRoundRect(innerRect, cornerRadius, cornerRadius, innerPaint); // 3. Set the Power Duff mode link: // http://www.youtube.com/watch?feature=player_detailpage&v=jF6Ad4GYjRU#t=1056s Paint outerPaint = new Paint(Paint.ANTI_ALIAS_FLAG); outerPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT)); // 4. Draw a translucent rounded rectangle link: // http://www.youtube.com/watch?feature=player_detailpage&v=jF6Ad4GYjRU outerPaint.setColor(color); framedCanvas.drawRoundRect(outerRect, cornerRadius, cornerRadius, outerPaint); // 5. Draw the frame on top of original bitmap canvas.drawBitmap(framedOutput, 0f, 0f, null); return output; }
From source file:com.example.zhangyangjing.roundcornerimageview.BezelImageView.java
@Override protected void onDraw(Canvas canvas) { if (mBounds == null) { return;// w ww.ja va 2 s . c o m } int width = mBounds.width(); int height = mBounds.height(); if (width == 0 || height == 0) { return; } if (!mCacheValid || width != mCachedWidth || height != mCachedHeight) { // Need to redraw the cache if (width == mCachedWidth && height == mCachedHeight) { // Have a correct-sized bitmap cache already allocated. Just erase it. mCacheBitmap.eraseColor(0); } else { // Allocate a new bitmap with the correct dimensions. mCacheBitmap.recycle(); //noinspection AndroidLintDrawAllocation mCacheBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); mCachedWidth = width; mCachedHeight = height; generateMaskPath(mCachedWidth, mCachedHeight); } Canvas cacheCanvas = new Canvas(mCacheBitmap); int sc = cacheCanvas.save(); mDesaturatePaint.setColorFilter((mDesaturateOnPress && isPressed()) ? mDesaturateColorFilter : null); cacheCanvas.saveLayer(mBoundsF, mDesaturatePaint, Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.FULL_COLOR_LAYER_SAVE_FLAG); super.onDraw(cacheCanvas); cacheCanvas.drawPath(this.mMaskedPath, this.mMaskedPaint); cacheCanvas.restoreToCount(sc); } // Draw from cache canvas.drawBitmap(mCacheBitmap, mBounds.left, mBounds.top, null); }
From source file:cl.monsoon.s1next.widget.BezelImageView.java
@Override protected void onDraw(@NonNull Canvas canvas) { if (mBounds == null) { return;/*from ww w . j ava 2s . c o m*/ } int width = mBounds.width(); int height = mBounds.height(); if (width == 0 || height == 0) { return; } if (!mCacheValid || mCachedWidth != width || mCachedHeight != height) { // need to redraw the cache if (mCachedWidth == width && mCachedHeight == height) { // have a correct-sized bitmap cache already allocated // just erase it mCacheBitmap.eraseColor(Color.TRANSPARENT); } else { // allocate a new bitmap with the correct dimensions mCacheBitmap.recycle(); //noinspection AndroidLintDrawAllocation mCacheBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); mCachedWidth = width; mCachedHeight = height; } Canvas cacheCanvas = new Canvas(mCacheBitmap); if (mMaskDrawable != null) { int saveCount = cacheCanvas.save(); mMaskDrawable.draw(cacheCanvas); cacheCanvas.saveLayer(mBoundsF, mMaskedPaint, Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.FULL_COLOR_LAYER_SAVE_FLAG); super.onDraw(cacheCanvas); cacheCanvas.restoreToCount(saveCount); } else { super.onDraw(cacheCanvas); } if (mBorderDrawable != null) { mBorderDrawable.draw(cacheCanvas); } } // draw from cache canvas.drawBitmap(mCacheBitmap, mBounds.left, mBounds.top, null); }
From source file:com.udacity.adcs.app.goodintents.ui.view.BezelImageView.java
@Override protected void onDraw(Canvas canvas) { if (mBounds == null) { return;//from w w w . ja va2 s.c om } int width = mBounds.width(); int height = mBounds.height(); if (width == 0 || height == 0) { return; } if (!mCacheValid || width != mCachedWidth || height != mCachedHeight) { // Need to redraw the cache if (width == mCachedWidth && height == mCachedHeight) { // Have a correct-sized bitmap cache already allocated. Just erase it. mCacheBitmap.eraseColor(0); } else { // Allocate a new bitmap with the correct dimensions. mCacheBitmap.recycle(); // noinspection AndroidLintDrawAllocation mCacheBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); mCachedWidth = width; mCachedHeight = height; } Canvas cacheCanvas = new Canvas(mCacheBitmap); if (mMaskDrawable != null) { int sc = cacheCanvas.save(); mMaskDrawable.draw(cacheCanvas); mMaskedPaint.setColorFilter((mDesaturateOnPress && isPressed()) ? mDesaturateColorFilter : null); cacheCanvas.saveLayer(mBoundsF, mMaskedPaint, Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.FULL_COLOR_LAYER_SAVE_FLAG); super.onDraw(cacheCanvas); cacheCanvas.restoreToCount(sc); } else if (mDesaturateOnPress && isPressed()) { int sc = cacheCanvas.save(); cacheCanvas.drawRect(0, 0, mCachedWidth, mCachedHeight, mBlackPaint); mMaskedPaint.setColorFilter(mDesaturateColorFilter); cacheCanvas.saveLayer(mBoundsF, mMaskedPaint, Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.FULL_COLOR_LAYER_SAVE_FLAG); super.onDraw(cacheCanvas); cacheCanvas.restoreToCount(sc); } else { super.onDraw(cacheCanvas); } if (mBorderDrawable != null) { mBorderDrawable.draw(cacheCanvas); } } // Draw from cache canvas.drawBitmap(mCacheBitmap, mBounds.left, mBounds.top, null); }
From source file:technology.mainthread.apps.moment.ui.view.BezelImageView.java
@Override protected void onDraw(@NonNull Canvas canvas) { if (mBounds == null) { return;/*w ww . java2 s. c o m*/ } int width = mBounds.width(); int height = mBounds.height(); if (width == 0 || height == 0) { return; } if (!mCacheValid || width != mCachedWidth || height != mCachedHeight) { // Need to redraw the cache if (width == mCachedWidth && height == mCachedHeight) { // Have a correct-sized bitmap cache already allocated. Just erase it. mCacheBitmap.eraseColor(0); } else { // Allocate a new bitmap with the correct dimensions. mCacheBitmap.recycle(); //noinspection AndroidLintDrawAllocation mCacheBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); mCachedWidth = width; mCachedHeight = height; } Canvas cacheCanvas = new Canvas(mCacheBitmap); if (mMaskDrawable != null) { int sc = cacheCanvas.save(); mMaskDrawable.draw(cacheCanvas); mMaskedPaint.setColorFilter(mDesaturateOnPress && isPressed() ? mDesaturateColorFilter : null); cacheCanvas.saveLayer(mBoundsF, mMaskedPaint, Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.FULL_COLOR_LAYER_SAVE_FLAG); super.onDraw(cacheCanvas); cacheCanvas.restoreToCount(sc); } else if (mDesaturateOnPress && isPressed()) { int sc = cacheCanvas.save(); cacheCanvas.drawRect(0, 0, mCachedWidth, mCachedHeight, mBlackPaint); mMaskedPaint.setColorFilter(mDesaturateColorFilter); cacheCanvas.saveLayer(mBoundsF, mMaskedPaint, Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.FULL_COLOR_LAYER_SAVE_FLAG); super.onDraw(cacheCanvas); cacheCanvas.restoreToCount(sc); } else { super.onDraw(cacheCanvas); } if (mBorderDrawable != null) { mBorderDrawable.draw(cacheCanvas); } } // Draw from cache canvas.drawBitmap(mCacheBitmap, mBounds.left, mBounds.top, null); }
From source file:app.geochat.ui.widgets.BezelImageView.java
@Override protected void onDraw(Canvas canvas) { if (mBounds == null) { return;// w w w . j av a 2 s . c om } int width = mBounds.width(); int height = mBounds.height(); if (width == 0 || height == 0) { return; } if (!mCacheValid || width != mCachedWidth || height != mCachedHeight) { // Need to redraw the cache if (width == mCachedWidth && height == mCachedHeight) { // Have a correct-sized bitmap cache already allocated. Just erase it. mCacheBitmap.eraseColor(0); } else { // Allocate a new bitmap with the correct dimensions. mCacheBitmap.recycle(); //noinspection AndroidLintDrawAllocation mCacheBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); mCachedWidth = width; mCachedHeight = height; } Canvas cacheCanvas = new Canvas(mCacheBitmap); if (mMaskDrawable != null) { int sc = cacheCanvas.save(); mMaskDrawable.draw(cacheCanvas); mMaskedPaint.setColorFilter((mDesaturateOnPress && isPressed()) ? mDesaturateColorFilter : null); cacheCanvas.saveLayer(mBoundsF, mMaskedPaint, Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.FULL_COLOR_LAYER_SAVE_FLAG); super.onDraw(cacheCanvas); cacheCanvas.restoreToCount(sc); } else if (mDesaturateOnPress && isPressed()) { int sc = cacheCanvas.save(); cacheCanvas.drawRect(0, 0, mCachedWidth, mCachedHeight, mBlackPaint); mMaskedPaint.setColorFilter(mDesaturateColorFilter); cacheCanvas.saveLayer(mBoundsF, mMaskedPaint, Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.FULL_COLOR_LAYER_SAVE_FLAG); super.onDraw(cacheCanvas); cacheCanvas.restoreToCount(sc); } else { super.onDraw(cacheCanvas); } if (mBorderDrawable != null) { mBorderDrawable.draw(cacheCanvas); } } // Draw from cache canvas.drawBitmap(mCacheBitmap, mBounds.left, mBounds.top, null); }
From source file:com.aprz.easy_iosched.ui.widget.BezelImageView.java
@Override protected void onDraw(Canvas canvas) { if (mBounds == null) { return;//from w w w . ja v a 2 s. com } int width = mBounds.width(); int height = mBounds.height(); if (width == 0 || height == 0) { return; } if (!mCacheValid || width != mCachedWidth || height != mCachedHeight) { // Need to redraw the cache. if (width == mCachedWidth && height == mCachedHeight) { // Have a correct-sized bitmap cache already allocated. Just erase it. mCacheBitmap.eraseColor(0); } else { // Allocate a new bitmap with the correct dimensions. mCacheBitmap.recycle(); //noinspection AndroidLintDrawAllocation mCacheBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); mCachedWidth = width; mCachedHeight = height; } Canvas cacheCanvas = new Canvas(mCacheBitmap); if (mMaskDrawable != null) { int sc = cacheCanvas.save(); mMaskDrawable.draw(cacheCanvas); mMaskedPaint.setColorFilter((mDesaturateOnPress && isPressed()) ? mDesaturateColorFilter : null); cacheCanvas.saveLayer(mBoundsF, mMaskedPaint, Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.FULL_COLOR_LAYER_SAVE_FLAG); super.onDraw(cacheCanvas); cacheCanvas.restoreToCount(sc); } else if (mDesaturateOnPress && isPressed()) { int sc = cacheCanvas.save(); cacheCanvas.drawRect(0, 0, mCachedWidth, mCachedHeight, mBlackPaint); mMaskedPaint.setColorFilter(mDesaturateColorFilter); cacheCanvas.saveLayer(mBoundsF, mMaskedPaint, Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.FULL_COLOR_LAYER_SAVE_FLAG); super.onDraw(cacheCanvas); cacheCanvas.restoreToCount(sc); } else { super.onDraw(cacheCanvas); } if (mBorderDrawable != null) { mBorderDrawable.draw(cacheCanvas); } } // Draw from cache. canvas.drawBitmap(mCacheBitmap, mBounds.left, mBounds.top, null); }
From source file:ng.kingsley.android.widget.BezelImageView.java
@Override protected void onDraw(@NonNull Canvas canvas) { if (mBounds == null) { return;/*from w w w . j a v a2 s.c o m*/ } int width = mBounds.width(); int height = mBounds.height(); if (width == 0 || height == 0) { return; } if (!mCacheValid || width != mCachedWidth || height != mCachedHeight) { // Need to redraw the cache if (width == mCachedWidth && height == mCachedHeight) { // Have a correct-sized bitmap cache already allocated. Just erase it. mCacheBitmap.eraseColor(0); } else { // Allocate a new bitmap with the correct dimensions. mCacheBitmap.recycle(); //noinspection AndroidLintDrawAllocation mCacheBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); mCachedWidth = width; mCachedHeight = height; } Canvas cacheCanvas = new Canvas(mCacheBitmap); if (mMaskDrawable != null) { int sc = cacheCanvas.save(); mMaskDrawable.draw(cacheCanvas); mMaskedPaint.setColorFilter((mDesaturateOnPress && isPressed()) ? mDesaturateColorFilter : null); cacheCanvas.saveLayer(mBoundsF, mMaskedPaint, Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.FULL_COLOR_LAYER_SAVE_FLAG); super.onDraw(cacheCanvas); cacheCanvas.restoreToCount(sc); } else if (mDesaturateOnPress && isPressed()) { int sc = cacheCanvas.save(); cacheCanvas.drawRect(0, 0, mCachedWidth, mCachedHeight, mBlackPaint); mMaskedPaint.setColorFilter(mDesaturateColorFilter); cacheCanvas.saveLayer(mBoundsF, mMaskedPaint, Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.FULL_COLOR_LAYER_SAVE_FLAG); super.onDraw(cacheCanvas); cacheCanvas.restoreToCount(sc); } else { super.onDraw(cacheCanvas); } if (mBorderDrawable != null) { mBorderDrawable.draw(cacheCanvas); } } // Draw from cache canvas.drawBitmap(mCacheBitmap, mBounds.left, mBounds.top, null); }