Example usage for android.graphics Canvas saveLayer

List of usage examples for android.graphics Canvas saveLayer

Introduction

In this page you can find the example usage for android.graphics Canvas saveLayer.

Prototype

public int saveLayer(@Nullable RectF bounds, @Nullable Paint paint, @Saveflags int saveFlags) 

Source Link

Document

This behaves the same as save(), but in addition it allocates and redirects drawing to an offscreen bitmap.

Usage

From source file:com.raghu.test.widgets.BezelImageView.java

@SuppressLint("WrongConstant")
@Override//w w w .j  ava 2 s  .c o m
protected void onDraw(Canvas canvas) {
    if (mBounds == null) {
        return;
    }

    int width = mBounds.width();
    int height = mBounds.height();

    if (width == 0 || height == 0) {
        return;
    }

    if (!mCacheValid || width != mCachedWidth || height != mCachedHeight || isSelected != isPressed) {
        // 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);
            if (isSelected) {
                if (mSelectorFilter != null) {
                    mMaskedPaint.setColorFilter(mSelectorFilter);
                } else {
                    mMaskedPaint.setColorFilter(mDesaturateColorFilter);

                }
            } else {
                mMaskedPaint.setColorFilter(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 (isSelected) {
            int sc = cacheCanvas.save();
            cacheCanvas.drawRect(0, 0, mCachedWidth, mCachedHeight, mBlackPaint);
            if (mSelectorFilter != null) {
                mMaskedPaint.setColorFilter(mSelectorFilter);
            } else {
                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);
        }
    }

    // Draw from cache
    canvas.drawBitmap(mCacheBitmap, mBounds.left, mBounds.top, null);

    //remember the previous press state
    isPressed = isPressed();
}