Java Code Examples for android.graphics.Canvas#saveLayer()

The following examples show how to use android.graphics.Canvas#saveLayer() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TextShowView.java    From easyweather with MIT License 6 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    int saveLayerCount = canvas.saveLayer(0, 0, mCurW, mCurH, bPaint, Canvas.ALL_SAVE_FLAG);
    canvas.drawBitmap(backBitmap, 0, 0, bPaint);// 绘制目标图
    bPaint.setXfermode(mXfermode);    //设置混排模式
    canvas.drawRect(topRect, bPaint);   //绘制源图
    canvas.drawRect(belowRect,bPaint);
    bPaint.setXfermode(null);         //清除混排模式
    canvas.restoreToCount(saveLayerCount);    //恢复保存的图层

    // 改变Rect区域,假如
    mCurTop -= 1;
    mCurBelow += 1;
    topRect.top = mCurTop;
    belowRect.bottom = mCurBelow;
    if (mCurTop >= height / 2 - getPx(150)) {
        invalidate();
    }
}
 
Example 2
Source File: FinderView.java    From GestureViews with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("deprecation")
@Override
protected void onDraw(Canvas canvas) {
    float rx = rounding * 0.5f * rect.width();
    float ry = rounding * 0.5f * rect.height();

    // Punching hole in background color requires offscreen drawing
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        canvas.saveLayer(0, 0, canvas.getWidth(), canvas.getHeight(), null);
    } else {
        canvas.saveLayer(0, 0, canvas.getWidth(), canvas.getHeight(), null, 0);
    }
    canvas.drawColor(backColor);
    canvas.drawRoundRect(rect, rx, ry, paintClear);
    canvas.restore();

    canvas.drawRoundRect(strokeRect, rx, ry, paintStroke);
}
 
Example 3
Source File: AvatarView.java    From Android_UE with Apache License 2.0 6 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    canvas.drawOval(rectF, mPaint);

    int saveLayerCount = canvas.saveLayer(PADDING, PADDING, width, width, mPaint);
    canvas.drawOval(PADDING + SRTOK_WIDTH,
            PADDING + SRTOK_WIDTH,
            width - SRTOK_WIDTH,
            width - SRTOK_WIDTH,
            mPaint);
    mPaint.setXfermode(xfermode);
    canvas.drawBitmap(bitmap,
            PADDING,
            PADDING,
            mPaint);

    mPaint.setXfermode(null);
    canvas.restoreToCount(saveLayerCount);
}
 
Example 4
Source File: ScreensharingCapturer.java    From opentok-android-sdk-samples with MIT License 5 votes vote down vote up
@Override
public void run() {
    if (capturing) {
        int width = contentView.getWidth();
        int height = contentView.getHeight();

        if (frame == null ||
                ScreensharingCapturer.this.width != width ||
                ScreensharingCapturer.this.height != height) {

            ScreensharingCapturer.this.width = width;
            ScreensharingCapturer.this.height = height;

            if (bmp != null) {
                bmp.recycle();
                bmp = null;
            }

            bmp = Bitmap.createBitmap(width,
                    height, Bitmap.Config.ARGB_8888);

            canvas = new Canvas(bmp);
            frame = new int[width * height];
        }
        canvas.saveLayer(0, 0, width, height, null);
        canvas.translate(-contentView.getScrollX(), - contentView.getScrollY());
        contentView.draw(canvas);

        bmp.getPixels(frame, 0, width, 0, 0, width, height);

        provideIntArrayFrame(frame, ARGB, width, height, 0, false);

        canvas.restore();

        mHandler.postDelayed(newFrame, 1000 / fps);

    }
}
 
Example 5
Source File: RoundImageView.java    From ImageLoader with Apache License 2.0 5 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {
    canvas.saveLayer(srcRectf, null, Canvas.ALL_SAVE_FLAG);
    float sx = 1.0f * (width - 2 * borderWidth) / width;
    float sy = 1.0f * (height - 2 * borderWidth) / height;
    canvas.scale(sx, sy, width / 2.0f, height / 2.0f);
    super.onDraw(canvas);
    paint.reset();
    path.reset();
    if (isCircle) {
        path.addCircle(width / 2.0f, height / 2.0f, radius, Path.Direction.CCW);
    } else {
        path.addRoundRect(srcRectf, srcRadii, Path.Direction.CCW);
    }
    paint.setAntiAlias(true);
    paint.setStyle(Paint.Style.FILL);
    paint.setXfermode(xfermode);
    if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.O_MR1) {
        canvas.drawPath(path, paint);
    } else {
        srcPath.reset();
        srcPath.addRect(srcRectf, Path.Direction.CCW);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            srcPath.op(path, Path.Op.DIFFERENCE);
        }
        canvas.drawPath(srcPath, paint);
    }
    paint.setXfermode(null);
    canvas.restore();
    drawBorders(canvas);
}
 
Example 6
Source File: ReflectItemView.java    From AndroidTVWidget with Apache License 2.0 5 votes vote down vote up
private void drawRefleCanvas4_3_18(Canvas canvas) {
    if (mIsReflection) {
        // 创建一个画布.
        String cacheID = getViewCacheID() + "";
        //
        Bitmap reflectBitmap = mBitmapMemoryCache.getBitmapFromMemCache(cacheID);
        if (reflectBitmap == null) {
            reflectBitmap = Bitmap.createBitmap(getWidth(), mRefHeight, Bitmap.Config.ARGB_8888);
            mBitmapMemoryCache.addBitmapToMemoryCache(cacheID, reflectBitmap);
        }
        Canvas reflectCanvas = new Canvas(reflectBitmap);
        reflectCanvas.drawPaint(mClearPaint); // 清空画布.
        /**
         * 如果设置了圆角,倒影也需要圆角.
         */
        int width = reflectCanvas.getWidth();
        int height = reflectCanvas.getHeight();
        RectF outerRect = new RectF(0, 0, width, height);
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        if (mIsDrawShape) {
            reflectCanvas.drawPath(getShapePath(width, height + 50, mRadius), paint);
            paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        }
        reflectCanvas.saveLayer(outerRect, paint, Canvas.ALL_SAVE_FLAG);
        drawReflection4_3_18(reflectCanvas);
        reflectCanvas.restore();
        canvas.save();
        int dy = getHeight();
        int dx = 0;
        canvas.translate(dx, dy + mRefleSpacing);
        canvas.drawBitmap(reflectBitmap, 0, 0, null);
        canvas.restore();
    }
}
 
Example 7
Source File: SwitchThemeAnimView.java    From DanDanPlayForAndroid with MIT License 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onDraw(Canvas canvas) {
    //在新的图层上面绘制
    int layer = canvas.saveLayer(0, 0, getWidth(), getHeight(), null);
    canvas.drawBitmap(mBackground, 0, 0, null);
    canvas.drawCircle(mStartX, mStartY, mCurrentRadius, mPaint);
    canvas.restoreToCount(layer);
}
 
Example 8
Source File: SwitchButton.java    From BigApp_Discuz_Android with Apache License 2.0 5 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {
	// TODO Auto-generated method stub
	super.onDraw(canvas);
	if (mDeltX > 0 || mDeltX == 0 && mSwitchOn) {
		if (mSrc != null) {
			mSrc.set(mMoveLength - mDeltX, 0, mSwitchBottom.getWidth()
					- mDeltX, mSwitchFrame.getHeight());
		}
	} else if (mDeltX < 0 || mDeltX == 0 && !mSwitchOn) {
		if (mSrc != null) {
			mSrc.set(-mDeltX, 0, mSwitchFrame.getWidth() - mDeltX,
					mSwitchFrame.getHeight());
		}
	}

	// 这儿是离屏缓冲,自己感觉类似双缓冲机制吧
	int count = canvas.saveLayer(rf, null, Canvas.MATRIX_SAVE_FLAG
			| Canvas.CLIP_SAVE_FLAG | Canvas.HAS_ALPHA_LAYER_SAVE_FLAG
			| Canvas.FULL_COLOR_LAYER_SAVE_FLAG
			| Canvas.CLIP_TO_LAYER_SAVE_FLAG);

	canvas.drawBitmap(mSwitchBottom, mSrc, mDest, null);
	canvas.drawBitmap(mSwitchThumb, mSrc, mDest, null);
	canvas.drawBitmap(mSwitchFrame, 0, 0, null);
	canvas.drawBitmap(mSwitchMask, 0, 0, mPaint);
	canvas.restoreToCount(count);
}
 
Example 9
Source File: ClipView.java    From YiZhi with Apache License 2.0 5 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    int LAYER_FLAGS = Canvas.MATRIX_SAVE_FLAG | Canvas.CLIP_SAVE_FLAG
            | Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.FULL_COLOR_LAYER_SAVE_FLAG
            | Canvas.CLIP_TO_LAYER_SAVE_FLAG;
    //通过Xfermode的DST_OUT来产生中间的透明裁剪区域,一定要另起一个Layer(层)
    canvas.saveLayer(0, 0, this.getWidth(), this.getHeight(), null, LAYER_FLAGS);
    //设置背景
    canvas.drawColor(Color.parseColor("#a8000000"));
    paint.setXfermode(xfermode);
    //绘制圆形裁剪框
    if (clipType == ClipType.CIRCLE) {
        //中间的透明的圆
        canvas.drawCircle(this.getWidth() / 2, this.getHeight() / 2, clipRadiusWidth, paint);
        //白色的圆边框
        canvas.drawCircle(this.getWidth() / 2, this.getHeight() / 2, clipRadiusWidth, borderPaint);
    } else if (clipType == ClipType.RECTANGLE) { //绘制矩形裁剪框
        //绘制中间的矩形
        canvas.drawRect(mHorizontalPadding, this.getHeight() / 2 - clipWidth / 2,
                this.getWidth() - mHorizontalPadding, this.getHeight() / 2 + clipWidth / 2, paint);
        //绘制白色的矩形边框
        canvas.drawRect(mHorizontalPadding, this.getHeight() / 2 - clipWidth / 2,
                this.getWidth() - mHorizontalPadding, this.getHeight() / 2 + clipWidth / 2, borderPaint);
    }
    //出栈,恢复到之前的图层,意味着新建的图层会被删除,新建图层上的内容会被绘制到canvas (or the previous layer)
    canvas.restore();
}
 
Example 10
Source File: EaseImageView.java    From Study_Android_Demo with Apache License 2.0 5 votes vote down vote up
/**
 * draw Rounded Rectangle
 *
 * @param canvas
 * @param bitmap
 */
private void drawDrawable(Canvas canvas, Bitmap bitmap) {
    Paint paint = new Paint();
    paint.setColor(0xffffffff);
    paint.setAntiAlias(true); //smooths out the edges of what is being drawn
    PorterDuffXfermode xfermode = new PorterDuffXfermode(PorterDuff.Mode.SRC_IN);
    // set flags
    int saveFlags = Canvas.MATRIX_SAVE_FLAG
            | Canvas.CLIP_SAVE_FLAG
            | Canvas.HAS_ALPHA_LAYER_SAVE_FLAG
            | Canvas.FULL_COLOR_LAYER_SAVE_FLAG
            | Canvas.CLIP_TO_LAYER_SAVE_FLAG;
    canvas.saveLayer(0, 0, width, height, null, saveFlags);

    if (shapeType == 1) {
        canvas.drawCircle(width / 2, height / 2, width / 2 - 1, paint);
    } else if (shapeType == 2) {
        RectF rectf = new RectF(1, 1, getWidth() - 1, getHeight() - 1);
        canvas.drawRoundRect(rectf, radius + 1, radius + 1, paint);
    }

    paint.setXfermode(xfermode);

    float scaleWidth = ((float) getWidth()) / bitmap.getWidth();
    float scaleHeight = ((float) getHeight()) / bitmap.getHeight();

    Matrix matrix = new Matrix();
    matrix.postScale(scaleWidth, scaleHeight);

    //bitmap scale
    bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);

    canvas.drawBitmap(bitmap, 0, 0, paint);
    canvas.restore();
}
 
Example 11
Source File: CompositeDrawable.java    From materialize with GNU General Public License v3.0 5 votes vote down vote up
private void drawInternal(Canvas canvas, boolean antiAliasing,
                          Rect bounds, RectF foregroundBounds, Rect backgroundBounds, Rect scoreBounds) {
    paint.setFlags(antiAliasing ? FLAG_SCALES : 0);

    canvas.drawBitmap(back, null, bounds, paint);

    canvas.saveLayer(
            bounds.left, bounds.top, bounds.right, bounds.bottom,
            null, Canvas.ALL_SAVE_FLAG);

    if (background != null) {
        background.setBounds(backgroundBounds);
        background.draw(canvas);
    }

    // always anti-aliasing on source bitmap because we scaled it
    if (source != null) {
        paint.setFlags(FLAG_SCALES);
        canvas.drawBitmap(source, null, foregroundBounds, paint);
    }

    paint.setFlags(antiAliasing ? FLAG_SCALES : 0);

    if (shape.score) {
        paint.setColor(SCORE_COLOR);
        canvas.drawRect(scoreBounds, paint);
        paint.setColor(Color.WHITE);
    }

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
    canvas.drawBitmap(mask, null, bounds, paint);
    paint.setXfermode(null);

    canvas.drawBitmap(fore, null, bounds, paint);

    canvas.restore();
}
 
Example 12
Source File: BitmapUtils.java    From zone-sdk with MIT License 5 votes vote down vote up
/**
 * 转换图片成圆形
 *
 * @param bitmap 传入Bitmap对象
 * @return
 */
public static Bitmap toRoundBitmap(Bitmap bitmap, boolean recycledSrc) {
    if (bitmap == null)
        return null;
    Bitmap circleBt = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());

    Canvas canvas = new Canvas(circleBt);

    Paint paint = new Paint();
    paint.setDither(true);
    paint.setAntiAlias(true);
    paint.setAntiAlias(true);
    paint.setFilterBitmap(true);
    canvas.saveLayer(0, 0, circleBt.getWidth(), circleBt.getHeight(), paint, Canvas.ALL_SAVE_FLAG);

    float radius = 0F;
    if (circleBt.getWidth() < circleBt.getHeight())
        radius = circleBt.getWidth() / 2;
    else
        radius = circleBt.getHeight() / 2;

    paint.setColor(Color.RED);
    canvas.drawCircle(circleBt.getWidth() / 2, circleBt.getHeight() / 2, radius, paint);
    paint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, 0, 0, paint);
    canvas.restoreToCount(1);
    if (recycledSrc)
        recycledBitmap(bitmap);
    return circleBt;
}
 
Example 13
Source File: BaseImageView.java    From ZzBeeLayout with Apache License 2.0 4 votes vote down vote up
@SuppressLint("DrawAllocation")
    @Override
    protected void onDraw(Canvas canvas) {
        if (!isInEditMode()) {
            int i = canvas.saveLayer(0.0f, 0.0f, getWidth(), getHeight(),
                    null, Canvas.ALL_SAVE_FLAG);
            try {
                Bitmap bitmap = mWeakBitmap != null ? mWeakBitmap.get() : null;
                // Bitmap not loaded.
                if (bitmap == null || bitmap.isRecycled()) {
                    Drawable drawable = getDrawable();
                    if (drawable != null) {
                        // Allocation onDraw but it's ok because it will not always be called.
                        bitmap = Bitmap.createBitmap(getWidth(),
                                getHeight(), Bitmap.Config.ARGB_8888);
                        Canvas bitmapCanvas = new Canvas(bitmap);
                        drawable.setBounds(0, 0, getWidth(), getHeight());
                        drawable.draw(bitmapCanvas);

                        // If mask is already set, skip and use cached mask.
						if (mMaskBitmap == null || mMaskBitmap.isRecycled()) {
                            mMaskBitmap = getBitmap();
						}

                        // Draw Bitmap.
                        mPaint.reset();
                        mPaint.setFilterBitmap(false);
                        mPaint.setXfermode(sXfermode);
//                        mBitmapShader = new BitmapShader(mMaskBitmap,
//                                Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
//                        mPaint.setShader(mBitmapShader);
                        bitmapCanvas.drawBitmap(mMaskBitmap, 0.0f, 0.0f, mPaint);

                        mWeakBitmap = new WeakReference<>(bitmap);
                    }
                }

                // Bitmap already loaded.
                if (bitmap != null) {
                    mPaint.setXfermode(null);
//                    mPaint.setShader(null);
                    canvas.drawBitmap(bitmap, 0.0f, 0.0f, mPaint);
                    return;
                }
            } catch (Exception e) {
                System.gc();

                Log.e(TAG, String.format("Failed to draw, Id :: %s. Error occurred :: %s", getId(), e.toString()));
            } finally {
                canvas.restoreToCount(i);
            }
        } else {
            super.onDraw(canvas);
        }
    }
 
Example 14
Source File: CoordinatorLayout.java    From Carbon with Apache License 2.0 4 votes vote down vote up
@Override
public void drawShadow(@NotNull Canvas canvas) {
    float alpha = getAlpha() * Carbon.getBackgroundTintAlpha(this) / 255.0f;
    if (alpha == 0 || !hasShadow())
        return;

    float z = getElevation() + getTranslationZ();

    int saveCount;
    boolean maskShadow = getBackground() != null && alpha != 1;
    boolean r = revealAnimator != null && revealAnimator.isRunning();

    if (alpha != 255) {
        paint.setAlpha((int) (127 * alpha));
        saveCount = canvas.saveLayer(0, 0, canvas.getWidth(), canvas.getHeight(), paint, Canvas.ALL_SAVE_FLAG);
    } else {
        saveCount = canvas.save();
    }
    Matrix matrix = getMatrix();
    canvas.setMatrix(matrix);

    if (r) {
        canvas.clipRect(
                getLeft() + revealAnimator.x - revealAnimator.radius, getTop() + revealAnimator.y - revealAnimator.radius,
                getLeft() + revealAnimator.x + revealAnimator.radius, getTop() + revealAnimator.y + revealAnimator.radius);
    }

    shadowDrawable.setFillColor(spotShadowColor);
    shadowDrawable.setShadowColor(spotShadowColor != null ? spotShadowColor.getColorForState(getDrawableState(), spotShadowColor.getDefaultColor()) : 0xff000000);
    shadowDrawable.setShadowCompatibilityMode(MaterialShapeDrawable.SHADOW_COMPAT_MODE_ALWAYS);
    shadowDrawable.setAlpha(0x44);
    shadowDrawable.setElevation(z);
    shadowDrawable.setShadowVerticalOffset(0);
    shadowDrawable.setBounds(getLeft(), (int) (getTop() + z / 4), getRight(), (int) (getBottom() + z / 4));
    shadowDrawable.draw(canvas);

    canvas.translate(this.getLeft(), this.getTop());
    canvas.concat(matrix);
    paint.setXfermode(Carbon.CLEAR_MODE);
    if (maskShadow) {
        cornersMask.setFillType(Path.FillType.WINDING);
        canvas.drawPath(cornersMask, paint);
    }
    if (r)
        canvas.drawPath(revealAnimator.mask, paint);

    canvas.restoreToCount(saveCount);
    paint.setXfermode(null);
    paint.setAlpha(255);
}
 
Example 15
Source File: BaseImageView.java    From CustomShapeImageView with Apache License 2.0 4 votes vote down vote up
@SuppressLint("DrawAllocation")
    @Override
    protected void onDraw(Canvas canvas) {
        if (!isInEditMode()) {
            int i = canvas.saveLayer(0.0f, 0.0f, getWidth(), getHeight(),
                    null, Canvas.ALL_SAVE_FLAG);
            try {
                Bitmap bitmap = mWeakBitmap != null ? mWeakBitmap.get() : null;
                // Bitmap not loaded.
                if (bitmap == null || bitmap.isRecycled()) {
                    Drawable drawable = getDrawable();
                    if (drawable != null) {
                        // Allocation onDraw but it's ok because it will not always be called.
                        bitmap = Bitmap.createBitmap(getWidth(),
                                getHeight(), Bitmap.Config.ARGB_8888);
                        Canvas bitmapCanvas = new Canvas(bitmap);
                        drawable.setBounds(0, 0, getWidth(), getHeight());
                        drawable.draw(bitmapCanvas);

                        // If mask is already set, skip and use cached mask.
						if (mMaskBitmap == null || mMaskBitmap.isRecycled()) {
                            mMaskBitmap = getBitmap();
						}

                        // Draw Bitmap.
                        mPaint.reset();
                        mPaint.setFilterBitmap(false);
                        mPaint.setXfermode(sXfermode);
//                        mBitmapShader = new BitmapShader(mMaskBitmap,
//                                Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
//                        mPaint.setShader(mBitmapShader);
                        bitmapCanvas.drawBitmap(mMaskBitmap, 0.0f, 0.0f, mPaint);

                        mWeakBitmap = new WeakReference<>(bitmap);
                    }
                }

                // Bitmap already loaded.
                if (bitmap != null) {
                    mPaint.setXfermode(null);
//                    mPaint.setShader(null);
                    canvas.drawBitmap(bitmap, 0.0f, 0.0f, mPaint);
                    return;
                }
            } catch (Exception e) {
                System.gc();

                Log.e(TAG, String.format("Failed to draw, Id :: %s. Error occurred :: %s", getId(), e.toString()));
            } finally {
                canvas.restoreToCount(i);
            }
        } else {
            super.onDraw(canvas);
        }
    }
 
Example 16
Source File: CompositionAvatarView.java    From CompositionAvatar with MIT License 4 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    final List<DrawableInfo> drawables = mDrawables;
    final int N = drawables.size();

    if (!isInEditMode() && (mContentSize <= 0 || N <= 0)) {
        return;
    }

    canvas.translate(getPaddingLeft(), getPaddingTop());
    int width = getWidth() - getPaddingLeft() - getPaddingRight();
    int height = getHeight() - getPaddingTop() - getPaddingBottom();
    if (width > height) {
        canvas.translate((width - height) * .5f, 0);
    } else {
        canvas.translate(0, (height - width) * .5f);
    }

    if (isInEditMode()) {
        float cr = Math.min(width, height) * .5f;
        canvas.drawCircle(cr, cr, cr, mPaint);
        return;
    }

    canvas.translate(0, mOffsetY);

    final Paint paint = mPaint;
    final float gapRadius = mSteinerCircleRadius * (mGap + 1f);
    for (int i = 0; i < drawables.size(); i++) {
        DrawableInfo drawable = drawables.get(i);
        final int savedLayer = canvas.saveLayer(0, 0, mContentSize, mContentSize,
                null, Canvas.ALL_SAVE_FLAG);

        drawable.mDrawable.draw(canvas);

        canvas.drawPath(drawable.mMaskPath, paint);
        if (drawable.mHasGap && mGap > 0f) {
            canvas.drawCircle(drawable.mGapCenterX, drawable.mGapCenterY, gapRadius, paint);
        }

        canvas.restoreToCount(savedLayer);
    }
}
 
Example 17
Source File: RxPorterImageView.java    From RxTools-master with Apache License 2.0 4 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {
    if (!isInEditMode()) {
        int saveCount = canvas.saveLayer(0.0f, 0.0f, getWidth(), getHeight(), null, Canvas.ALL_SAVE_FLAG);
        try {
            if (invalidated) {
                Drawable drawable = getDrawable();
                if (drawable != null) {
                    invalidated = false;
                    Matrix imageMatrix = getImageMatrix();
                    if (imageMatrix == null){// && mPaddingTop == 0 && mPaddingLeft == 0) {
                        drawable.draw(drawableCanvas);
                    } else {
                        int drawableSaveCount = drawableCanvas.getSaveCount();
                        drawableCanvas.save();
                        drawableCanvas.concat(imageMatrix);
                        drawable.draw(drawableCanvas);
                        drawableCanvas.restoreToCount(drawableSaveCount);
                    }

                    drawablePaint.reset();
                    drawablePaint.setFilterBitmap(false);
                    drawablePaint.setXfermode(PORTER_DUFF_XFERMODE);
                    drawableCanvas.drawBitmap(maskBitmap, 0.0f, 0.0f, drawablePaint);
                }
            }

            if (!invalidated) {
                drawablePaint.setXfermode(null);
                canvas.drawBitmap(drawableBitmap, 0.0f, 0.0f, drawablePaint);
            }
        } catch (Exception e) {
            String log = "Exception occured while drawing " + getId();
            Log.e(TAG, log, e);
        } finally {
            canvas.restoreToCount(saveCount);
        }
    } else {
        super.onDraw(canvas);
    }
}
 
Example 18
Source File: ConstraintLayout.java    From Carbon with Apache License 2.0 4 votes vote down vote up
@SuppressLint("MissingSuperCall")
@Override
public void draw(@NonNull Canvas canvas) {
    drawCalled = true;
    boolean r = revealAnimator != null;
    boolean c = !Carbon.isShapeRect(shapeModel, boundsRect);

    if (Carbon.IS_PIE_OR_HIGHER) {
        if (spotShadowColor != null)
            super.setOutlineSpotShadowColor(spotShadowColor.getColorForState(getDrawableState(), spotShadowColor.getDefaultColor()));
        if (ambientShadowColor != null)
            super.setOutlineAmbientShadowColor(ambientShadowColor.getColorForState(getDrawableState(), ambientShadowColor.getDefaultColor()));
    }

    if (isInEditMode() && (r || c) && getWidth() > 0 && getHeight() > 0) {
        Bitmap layer = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
        Canvas layerCanvas = new Canvas(layer);
        drawInternal(layerCanvas);

        Bitmap mask = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
        Canvas maskCanvas = new Canvas(mask);
        Paint maskPaint = new Paint(0xffffffff);
        maskCanvas.drawPath(cornersMask, maskPaint);

        for (int x = 0; x < getWidth(); x++) {
            for (int y = 0; y < getHeight(); y++) {
                int maskPixel = mask.getPixel(x, y);
                layer.setPixel(x, y, Color.alpha(maskPixel) > 0 ? layer.getPixel(x, y) : 0);
            }
        }
        canvas.drawBitmap(layer, 0, 0, paint);
    } else if (getWidth() > 0 && getHeight() > 0 && (((r || c) && !Carbon.IS_LOLLIPOP_OR_HIGHER) || !shapeModel.isRoundRect(boundsRect))) {
        int saveCount = canvas.saveLayer(0, 0, getWidth(), getHeight(), null, Canvas.ALL_SAVE_FLAG);

        if (r) {
            int saveCount2 = canvas.save();
            canvas.clipRect(revealAnimator.x - revealAnimator.radius, revealAnimator.y - revealAnimator.radius, revealAnimator.x + revealAnimator.radius, revealAnimator.y + revealAnimator.radius);
            drawInternal(canvas);
            canvas.restoreToCount(saveCount2);
        } else {
            drawInternal(canvas);
        }

        paint.setXfermode(Carbon.CLEAR_MODE);
        if (c) {
            cornersMask.setFillType(Path.FillType.INVERSE_WINDING);
            canvas.drawPath(cornersMask, paint);
        }
        if (r)
            canvas.drawPath(revealAnimator.mask, paint);
        paint.setXfermode(null);

        canvas.restoreToCount(saveCount);
        paint.setXfermode(null);
    } else {
        drawInternal(canvas);
    }
}
 
Example 19
Source File: ColorFilterRelativeLayout.java    From SoloPi with Apache License 2.0 4 votes vote down vote up
@Override
protected void dispatchDraw(Canvas canvas) {
    canvas.saveLayer(null, m_paint, Canvas.ALL_SAVE_FLAG);
    super.dispatchDraw(canvas);
    canvas.restore();
}
 
Example 20
Source File: RecyclerView.java    From Carbon with Apache License 2.0 4 votes vote down vote up
@Override
public void drawShadow(@NotNull Canvas canvas) {
    float alpha = getAlpha() * Carbon.getBackgroundTintAlpha(this) / 255.0f;
    if (alpha == 0 || !hasShadow())
        return;

    float z = getElevation() + getTranslationZ();

    int saveCount;
    boolean maskShadow = getBackground() != null && alpha != 1;
    boolean r = revealAnimator != null && revealAnimator.isRunning();

    if (alpha != 255) {
        paint.setAlpha((int) (127 * alpha));
        saveCount = canvas.saveLayer(0, 0, canvas.getWidth(), canvas.getHeight(), paint, Canvas.ALL_SAVE_FLAG);
    } else {
        saveCount = canvas.save();
    }
    Matrix matrix = getMatrix();
    canvas.setMatrix(matrix);

    if (r) {
        canvas.clipRect(
                getLeft() + revealAnimator.x - revealAnimator.radius, getTop() + revealAnimator.y - revealAnimator.radius,
                getLeft() + revealAnimator.x + revealAnimator.radius, getTop() + revealAnimator.y + revealAnimator.radius);
    }

    shadowDrawable.setFillColor(spotShadowColor);
    shadowDrawable.setShadowColor(spotShadowColor != null ? spotShadowColor.getColorForState(getDrawableState(), spotShadowColor.getDefaultColor()) : 0xff000000);
    shadowDrawable.setShadowCompatibilityMode(MaterialShapeDrawable.SHADOW_COMPAT_MODE_ALWAYS);
    shadowDrawable.setAlpha(0x44);
    shadowDrawable.setElevation(z);
    shadowDrawable.setShadowVerticalOffset(0);
    shadowDrawable.setBounds(getLeft(), (int) (getTop() + z / 4), getRight(), (int) (getBottom() + z / 4));
    shadowDrawable.draw(canvas);

    canvas.translate(this.getLeft(), this.getTop());
    canvas.concat(matrix);
    paint.setXfermode(Carbon.CLEAR_MODE);
    if (maskShadow) {
        cornersMask.setFillType(Path.FillType.WINDING);
        canvas.drawPath(cornersMask, paint);
    }
    if (r)
        canvas.drawPath(revealAnimator.mask, paint);

    canvas.restoreToCount(saveCount);
    paint.setXfermode(null);
    paint.setAlpha(255);
}