Example usage for android.graphics Canvas save

List of usage examples for android.graphics Canvas save

Introduction

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

Prototype

public int save() 

Source Link

Document

Saves the current matrix and clip onto a private stack.

Usage

From source file:com.comcast.freeflow.core.FreeFlowContainer.java

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    boolean needsInvalidate = false;

    final int height = getMeasuredHeight() - getPaddingTop() - getPaddingBottom();
    final int width = getMeasuredWidth();

    if (!mLeftEdge.isFinished()) {
        final int restoreCount = canvas.save();

        canvas.rotate(270);/*w  w w .  j  a va  2 s  .co m*/
        canvas.translate(-height + getPaddingTop(), 0);// width);
        mLeftEdge.setSize(height, width);

        needsInvalidate = mLeftEdge.draw(canvas);
        canvas.restoreToCount(restoreCount);
    }

    if (!mTopEdge.isFinished()) {
        final int restoreCount = canvas.save();

        mTopEdge.setSize(width, height);

        needsInvalidate = mTopEdge.draw(canvas);
        canvas.restoreToCount(restoreCount);
    }

    if (!mRightEdge.isFinished()) {
        final int restoreCount = canvas.save();

        canvas.rotate(90);
        canvas.translate(0, -width);// width);
        mRightEdge.setSize(height, width);

        needsInvalidate = mRightEdge.draw(canvas);
        canvas.restoreToCount(restoreCount);
    }

    if (!mBottomEdge.isFinished()) {
        final int restoreCount = canvas.save();

        canvas.rotate(180);
        canvas.translate(-width + getPaddingTop(), -height);

        mBottomEdge.setSize(width, height);

        needsInvalidate = mBottomEdge.draw(canvas);
        canvas.restoreToCount(restoreCount);
    }

    if (needsInvalidate)
        postInvalidateOnAnimation();

}

From source file:com.mark.quick.ui.view.swipebacklayout.SwipeBackLayout.java

@Override
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
    final LayoutParams lp = (LayoutParams) child.getLayoutParams();
    boolean result;
    final int save = canvas.save();

    if (mCanSlide && !lp.slideable && mSlideableView != null) {
        // Clip against the slider; no sense drawing what will immediately be covered.
        canvas.getClipBounds(mTmpRect);/* w  w w.  java2s  .  c om*/
        if (isLayoutRtlSupport()) {
            mTmpRect.left = Math.max(mTmpRect.left, mSlideableView.getRight());
        } else {
            mTmpRect.right = Math.min(mTmpRect.right, mSlideableView.getLeft());
        }
        canvas.clipRect(mTmpRect);
    }

    if (Build.VERSION.SDK_INT >= 11) { // HC
        result = super.drawChild(canvas, child, drawingTime);
    } else {
        if (lp.dimWhenOffset && mSlideOffset > 0) {
            if (!child.isDrawingCacheEnabled()) {
                child.setDrawingCacheEnabled(true);
            }
            final Bitmap cache = child.getDrawingCache();
            if (cache != null) {
                canvas.drawBitmap(cache, child.getLeft(), child.getTop(), lp.dimPaint);
                result = false;
            } else {
                Log.e(TAG, "drawChild: child view " + child + " returned null drawing cache");
                result = super.drawChild(canvas, child, drawingTime);
            }
        } else {
            if (child.isDrawingCacheEnabled()) {
                child.setDrawingCacheEnabled(false);
            }
            result = super.drawChild(canvas, child, drawingTime);
        }
    }

    canvas.restoreToCount(save);

    return result;
}

From source file:org.mariotaku.multivalueswitch.library.MultiValueSwitch.java

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    final Rect padding = mTempRect;
    final Drawable trackDrawable = mTrackDrawable;
    if (trackDrawable != null) {
        trackDrawable.getPadding(padding);
    } else {//from   www .  java 2  s  .  com
        padding.setEmpty();
    }

    final Drawable thumbDrawable = mThumbDrawable;
    if (trackDrawable != null) {
        if (mSplitTrack && thumbDrawable != null) {
            final Rect insets = MVS_DrawableUtilsAccessor.getOpticalBounds(thumbDrawable);
            thumbDrawable.copyBounds(padding);
            padding.left += insets.left;
            padding.right -= insets.right;

            final int saveCount = canvas.save();
            canvas.clipRect(padding, Region.Op.DIFFERENCE);
            trackDrawable.draw(canvas);
            canvas.restoreToCount(saveCount);
        } else {
            trackDrawable.draw(canvas);
        }

        final int thumbScrollStart = mSwitchLeft + mThumbWidth - mThumbWidth / 2;
        final int thumbScrollRange = mSwitchRight - mSwitchLeft - mThumbWidth;
        final int y = trackDrawable.getBounds().centerY();
        for (int i = 0, j = getMax(); i < j; i++) {
            if (i == 0 || i == j - 1)
                continue;

            canvas.drawCircle(thumbScrollStart + i * (thumbScrollRange / (j - 1)), y, mPointRadius,
                    mPointPaint);
        }
    }

    final int saveCount = canvas.save();

    if (thumbDrawable != null) {
        thumbDrawable.draw(canvas);
    }

    canvas.restoreToCount(saveCount);

}

From source file:cn.ieclipse.af.view.StaggeredGridView.java

@Override
public void draw(Canvas canvas) {
    super.draw(canvas);

    if (mTopEdge != null) {
        boolean needsInvalidate = false;
        if (!mTopEdge.isFinished()) {
            mTopEdge.draw(canvas);/* w  w  w.  j a v a2  s .  c  om*/
            needsInvalidate = true;
        }
        if (!mBottomEdge.isFinished()) {
            final int restoreCount = canvas.save();
            final int width = getWidth();
            canvas.translate(-width, getHeight());
            canvas.rotate(180, width, 0);
            mBottomEdge.draw(canvas);
            canvas.restoreToCount(restoreCount);
            needsInvalidate = true;
        }

        if (needsInvalidate) {
            ViewCompat.postInvalidateOnAnimation(this);
        }
    }
}

From source file:com.aidy.bottomdrawerlayout.DrawerLayout.java

@Override
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
    Log.i(TAG, "drawChild()");
    final int height = getHeight();
    final boolean drawingContent = isContentView(child);
    int clipLeft = 0, clipRight = getWidth();

    final int restoreCount = canvas.save();
    if (drawingContent) {
        final int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View v = getChildAt(i);
            if (v == child || v.getVisibility() != VISIBLE || !hasOpaqueBackground(v) || !isDrawerView(v)
                    || v.getHeight() < height) {
                Log.i(TAG, "drawChild() -- 0");
                continue;
            }/*from  ww  w . j  a  va2s . co  m*/

            if (checkDrawerViewAbsoluteGravity(v, Gravity.LEFT)) {
                Log.i(TAG, "drawChild() -- 1");
                final int vright = v.getRight();
                if (vright > clipLeft)
                    clipLeft = vright;
            } else {
                Log.i(TAG, "drawChild() -- 2");
                final int vleft = v.getLeft();
                if (vleft < clipRight)
                    clipRight = vleft;
            }
        }
        canvas.clipRect(clipLeft, 0, clipRight, getHeight());
    }
    final boolean result = super.drawChild(canvas, child, drawingTime);
    canvas.restoreToCount(restoreCount);

    if (mScrimOpacity > 0 && drawingContent) {
        Log.i(TAG, "drawChild() -- drawingContent");
        final int baseAlpha = (mScrimColor & 0xff000000) >>> 24;
        final int imag = (int) (baseAlpha * mScrimOpacity);
        final int color = imag << 24 | (mScrimColor & 0xffffff);
        mScrimPaint.setColor(color);

        canvas.drawRect(clipLeft, 0, clipRight, getHeight(), mScrimPaint);
    } else if (mShadowLeft != null && checkDrawerViewAbsoluteGravity(child, Gravity.LEFT)) {
        Log.i(TAG, "drawChild() -- Gravity.LEFT");
        final int shadowWidth = mShadowLeft.getIntrinsicWidth();
        final int childRight = child.getRight();
        final int drawerPeekDistance = mLeftDragger.getEdgeSize();
        final float alpha = Math.max(0, Math.min((float) childRight / drawerPeekDistance, 1.f));
        mShadowLeft.setBounds(childRight, child.getTop(), childRight + shadowWidth, child.getBottom());
        mShadowLeft.setAlpha((int) (0xff * alpha));
        mShadowLeft.draw(canvas);
    } else if (mShadowRight != null && checkDrawerViewAbsoluteGravity(child, Gravity.RIGHT)) {
        Log.i(TAG, "drawChild() -- Gravity.RIGHT");
        final int shadowWidth = mShadowRight.getIntrinsicWidth();
        final int childLeft = child.getLeft();
        final int showing = getWidth() - childLeft;
        final int drawerPeekDistance = mRightDragger.getEdgeSize();
        final float alpha = Math.max(0, Math.min((float) showing / drawerPeekDistance, 1.f));
        mShadowRight.setBounds(childLeft - shadowWidth, child.getTop(), childLeft, child.getBottom());
        mShadowRight.setAlpha((int) (0xff * alpha));
        mShadowRight.draw(canvas);
    }
    return result;
}

From source file:org.telegram.ui.Cells.DialogCell.java

@Override
protected void onDraw(Canvas canvas) {
    if (currentDialogId == 0) {
        return;// w  w  w  . java 2 s  . c  o m
    }

    if (isSelected) {
        canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), backPaint);
    }

    if (drawNameLock) {
        setDrawableBounds(lockDrawable, nameLockLeft, nameLockTop);
        lockDrawable.draw(canvas);
    } else if (drawNameGroup) {
        setDrawableBounds(groupDrawable, nameLockLeft, nameLockTop);
        groupDrawable.draw(canvas);
    } else if (drawNameBroadcast) {
        setDrawableBounds(broadcastDrawable, nameLockLeft, nameLockTop);
        broadcastDrawable.draw(canvas);
    } else if (drawNameBot) {
        setDrawableBounds(botDrawable, nameLockLeft, nameLockTop);
        botDrawable.draw(canvas);
    }

    if (nameLayout != null) {
        canvas.save();
        canvas.translate(nameLeft, AndroidUtilities.dp(13));
        nameLayout.draw(canvas);
        canvas.restore();
    }

    canvas.save();
    canvas.translate(timeLeft, timeTop);
    timeLayout.draw(canvas);
    canvas.restore();

    if (messageLayout != null) {
        canvas.save();
        canvas.translate(messageLeft, messageTop);
        try {
            messageLayout.draw(canvas);
        } catch (Exception e) {
            FileLog.e("tmessages", e);
        }
        canvas.restore();
    }

    if (drawClock) {
        setDrawableBounds(clockDrawable, checkDrawLeft, checkDrawTop);
        clockDrawable.draw(canvas);
    } else if (drawCheck2) {
        if (drawCheck1) {
            setDrawableBounds(halfCheckDrawable, halfCheckDrawLeft, checkDrawTop);
            halfCheckDrawable.draw(canvas);
            setDrawableBounds(checkDrawable, checkDrawLeft, checkDrawTop);
            checkDrawable.draw(canvas);
        } else {
            setDrawableBounds(checkDrawable, checkDrawLeft, checkDrawTop);
            checkDrawable.draw(canvas);
        }
    }

    if (dialogMuted && !drawVerified) {
        setDrawableBounds(muteDrawable, nameMuteLeft, AndroidUtilities.dp(16.5f));
        muteDrawable.draw(canvas);
    } else if (drawVerified) {
        setDrawableBounds(verifiedDrawable, nameMuteLeft, AndroidUtilities.dp(16.5f));
        verifiedDrawable.draw(canvas);
    }

    if (drawError) {
        setDrawableBounds(errorDrawable, errorLeft, errorTop);
        errorDrawable.draw(canvas);
    } else if (drawCount) {
        if (dialogMuted) {
            setDrawableBounds(countDrawableGrey, countLeft - AndroidUtilities.dp(5.5f), countTop,
                    countWidth + AndroidUtilities.dp(11), countDrawable.getIntrinsicHeight());
            countDrawableGrey.draw(canvas);
        } else {
            setDrawableBounds(countDrawable, countLeft - AndroidUtilities.dp(5.5f), countTop,
                    countWidth + AndroidUtilities.dp(11), countDrawable.getIntrinsicHeight());
            countDrawable.draw(canvas);
        }
        canvas.save();
        canvas.translate(countLeft, countTop + AndroidUtilities.dp(4));
        if (countLayout != null) {
            countLayout.draw(canvas);
        }
        canvas.restore();
    }

    if (useSeparator) {
        if (LocaleController.isRTL) {
            canvas.drawLine(0, getMeasuredHeight() - 1,
                    getMeasuredWidth() - AndroidUtilities.dp(AndroidUtilities.leftBaseline),
                    getMeasuredHeight() - 1, linePaint);
        } else {
            canvas.drawLine(AndroidUtilities.dp(AndroidUtilities.leftBaseline), getMeasuredHeight() - 1,
                    getMeasuredWidth(), getMeasuredHeight() - 1, linePaint);
        }
    }

    avatarImage.draw(canvas);
}

From source file:com.klinker.android.launcher.addons.view.LauncherDrawerLayout.java

@Override
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
    final int height = getHeight();
    final boolean drawingContent = isContentView(child);
    int clipLeft = 0, clipRight = getWidth();

    final int restoreCount = canvas.save();
    if (drawingContent) {
        final int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View v = getChildAt(i);
            if (v == child || v.getVisibility() != VISIBLE || !hasOpaqueBackground(v) || !isDrawerView(v)
                    || v.getHeight() < height) {
                continue;
            }//from w  w  w. j  a  va  2 s .c  o m

            if (checkDrawerViewAbsoluteGravity(v, Gravity.LEFT)) {
                final int vright = v.getRight();
                if (vright > clipLeft)
                    clipLeft = vright;
            } else {
                final int vleft = v.getLeft();
                if (vleft < clipRight)
                    clipRight = vleft;
            }
        }

        canvas.clipRect(clipLeft, 0, clipRight, getHeight());
    }
    final boolean result = super.drawChild(canvas, child, drawingTime);
    canvas.restoreToCount(restoreCount);

    if (mScrimOpacity > 0 && drawingContent) {
        final int baseAlpha = (mScrimColor & 0xff000000) >>> 24;
        final int imag = (int) (baseAlpha * mScrimOpacity);
        final int color = imag << 24 | (mScrimColor & 0xffffff);
        mScrimPaint.setColor(color);

        canvas.drawRect(clipLeft, 0, clipRight, getHeight(), mScrimPaint);
    } else if (mShadowLeft != null && checkDrawerViewAbsoluteGravity(child, Gravity.LEFT)) {
        final int shadowWidth = mShadowLeft.getIntrinsicWidth();
        final int childRight = child.getRight();
        final int drawerPeekDistance = mLeftDragger.getEdgeSize();
        final float alpha = Math.max(0, Math.min((float) childRight / drawerPeekDistance, 1.f));
        mShadowLeft.setBounds(childRight, child.getTop(), childRight + shadowWidth, child.getBottom());
        mShadowLeft.setAlpha((int) (0xff * alpha));
        mShadowLeft.draw(canvas);
    } else if (mShadowRight != null && checkDrawerViewAbsoluteGravity(child, Gravity.RIGHT)) {
        final int shadowWidth = mShadowRight.getIntrinsicWidth();
        final int childLeft = child.getLeft();
        final int showing = getWidth() - childLeft;
        final int drawerPeekDistance = mRightDragger.getEdgeSize();
        final float alpha = Math.max(0, Math.min((float) showing / drawerPeekDistance, 1.f));
        mShadowRight.setBounds(childLeft - shadowWidth, child.getTop(), childLeft, child.getBottom());
        mShadowRight.setAlpha((int) (0xff * alpha));
        mShadowRight.draw(canvas);
    }
    return result;
}

From source file:com.mjhram.geodata.GpsMainActivity.java

public Bitmap drawMultilineTextToBitmap(Context gContext, Bitmap bitmap, String gText) {
    // prepare canvas
    Resources resources = gContext.getResources();
    float scale = resources.getDisplayMetrics().density;
    //Bitmap bitmap = BitmapFactory.decodeResource(resources, gResId);

    android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig();
    // set default bitmap config if none
    if (bitmapConfig == null) {
        bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
    }// w ww  . j a  v  a 2s. c  o  m
    // resource bitmaps are imutable,
    // so we need to convert it to mutable one
    bitmap = bitmap.copy(bitmapConfig, true);

    Canvas canvas = new Canvas(bitmap);

    // new antialiased Paint
    TextPaint paint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
    // text color - #3D3D3D
    paint.setColor(Color.rgb(61, 61, 61));
    // text size in pixels
    paint.setTextSize((int) (20 * scale));
    // text shadow
    paint.setShadowLayer(1f, 0f, 1f, Color.WHITE);
    //canvas.drawText("This is", 100, 100, paint);
    //canvas.drawText("multi-line", 100, 150, paint);
    //canvas.drawText("text", 100, 200, paint);

    // set text width to canvas width minus 16dp padding
    int textWidth = canvas.getWidth() - (int) (16 * scale);

    // init StaticLayout for text
    StaticLayout textLayout = new StaticLayout(gText, paint, textWidth, Layout.Alignment.ALIGN_CENTER, 1.0f,
            0.0f, false);

    // get height of multiline text
    int textHeight = textLayout.getHeight();

    // get position of text's top left corner
    float x = (bitmap.getWidth() - textWidth) / 2;
    float y = bitmap.getHeight() - textHeight;

    // draw text to the Canvas center
    canvas.save();
    canvas.translate(x, y);
    textLayout.draw(canvas);
    canvas.restore();

    return bitmap;
}

From source file:com.morninz.ninepinview.widget.NinePINView.java

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    // draw circles around center points and connection line
    int drawnCount = mDrawnPoints.size();
    for (int j = 0; j < drawnCount; j++) {
        Point p1 = mDrawnPoints.get(j);
        canvas.drawCircle(p1.x, p1.y, mCircleRadius, mCirclePaint);
        if (j + 1 < drawnCount) {
            Point p2 = mDrawnPoints.get(j + 1);
            canvas.drawCircle(p2.x, p2.y, mCircleRadius, mCirclePaint);
            canvas.drawLine(p1.x, p1.y, p2.x, p2.y, mLinePaint);
            if (mWillDrawWrongTriangle) {
                // compute the wrong triangle's direction of this point.
                float angle = 0.f;
                if (p2.y == p1.y) {// x-axis
                    angle = p2.x > p1.x ? 0.f : 180.f;
                } else if (p2.x == p1.x) { // y-axis
                    angle = p2.y > p1.y ? 90.f : -90.f;
                } else {// in quadrants
                    double tanA = ((double) p2.y - (double) p1.y) / ((double) p2.x - (double) p1.x);
                    // in 1 or 4 quadrant
                    angle = (float) (Math.atan(tanA) * 180 / Math.PI);
                    // in 2 or 3 quadrant
                    if (p2.x < p1.x) {
                        angle += 180.f;//from  ww  w .j  av  a  2 s.c o m
                    }
                }
                Log.d(TAG, "angle " + angle);
                canvas.save();
                canvas.rotate(angle, p1.x, p1.y);
                canvas.drawPath(mWrongPaths[p1.index], mWrongPaint);
                canvas.restore();
            }
        }
    }

    // draw extra connection line
    if (mLastDrawnPoint != null) {
        canvas.drawLine(mLastDrawnPoint.x, mLastDrawnPoint.y, mCurrX, mCurrY, mLinePaint);
    }

    // draw 9 center points
    for (int i = 0; i < POINT_COUNT; i++) {
        Point p = mCenterPoints[i];
        canvas.drawCircle(p.x, p.y, mPointSize, mPointPaint);
    }
}

From source file:org.androfarsh.widget.DragGridLayout.java

private void drawCellGrid(Canvas canvas) {
    if (mCellDrawable != null) {
        int i = 0;
        for (final Cell cell : mCells) {
            mTmpRect.set(cell.rect);//from w w w  .jav  a2s .c  om

            final int[] stateSet = new int[] { (mEditMode ? 1 : -1) * R.attr.state_editing,
                    (mPressedCell == cell ? 1 : -1) * android.R.attr.state_pressed };

            canvas.save();
            canvas.clipRect(cell.rect);
            mCellDrawable.setState(stateSet);
            mCellDrawable.setBounds(cell.rect);
            mCellDrawable.draw(canvas);

            if (mDebugMode) {
                mPaint.setTextAlign(Align.CENTER);
                mPaint.setTextSize(30);
                mPaint.setTypeface(Typeface.DEFAULT_BOLD);
                mPaint.setColor(Color.GREEN);
                canvas.drawText(Integer.toString(i), cell.rect.centerX(), cell.rect.centerY(), mPaint);
                ++i;
            }

            canvas.restore();
        }
    }

    if (mDebugMode) {
        mPaint.setColor(Color.GREEN);
        mPaint.setStyle(Style.STROKE);
        mPaint.setStrokeWidth(1.5f);
        canvas.drawPath(mCellsRegion.getBoundaryPath(), mPaint);
    }
}