Example usage for android.graphics Canvas restore

List of usage examples for android.graphics Canvas restore

Introduction

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

Prototype

public void restore() 

Source Link

Document

This call balances a previous call to save(), and is used to remove all modifications to the matrix/clip state since the last save call.

Usage

From source file:com.dm.xz.views.PinnedSectionListView.java

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

    if (mPinnedSection != null) {

        // prepare variables
        int pLeft = getListPaddingLeft();
        int pTop = getListPaddingTop();
        View view = mPinnedSection.view;

        // draw child
        canvas.save();// www . j a va 2  s.com

        int clipHeight = view.getHeight()
                + (mShadowDrawable == null ? 0 : Math.min(mShadowHeight, mSectionsDistanceY));
        canvas.clipRect(pLeft, pTop, pLeft + view.getWidth(), pTop + clipHeight);

        canvas.translate(pLeft, pTop + mTranslateY);
        drawChild(canvas, mPinnedSection.view, getDrawingTime());

        if (mShadowDrawable != null && mSectionsDistanceY > 0) {
            mShadowDrawable.setBounds(mPinnedSection.view.getLeft(), mPinnedSection.view.getBottom(),
                    mPinnedSection.view.getRight(), mPinnedSection.view.getBottom() + mShadowHeight);
            mShadowDrawable.draw(canvas);
        }

        canvas.restore();
    }
}

From source file:android.support.wear.widget.drawer.PageIndicatorView.java

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

    if (mNumberOfPositions > 1) {
        float dotCenterLeft = getPaddingLeft() + (mDotSpacing / 2f);
        float dotCenterTop = getHeight() / 2f;
        canvas.save();//w w  w. ja  v  a  2 s  .com
        canvas.translate(dotCenterLeft, dotCenterTop);
        for (int i = 0; i < mNumberOfPositions; i++) {
            if (i == mSelectedPosition) {
                float radius = mDotRadiusSelected + mDotShadowRadius;
                canvas.drawCircle(mDotShadowDx, mDotShadowDy, radius, mDotPaintShadowSelected);
                canvas.drawCircle(0, 0, mDotRadiusSelected, mDotPaintSelected);
            } else {
                float radius = mDotRadius + mDotShadowRadius;
                canvas.drawCircle(mDotShadowDx, mDotShadowDy, radius, mDotPaintShadow);
                canvas.drawCircle(0, 0, mDotRadius, mDotPaint);
            }
            canvas.translate(mDotSpacing, 0);
        }
        canvas.restore();
    }
}

From source file:com.ferdi2005.secondgram.AndroidUtilities.java

private static Intent createShortcutIntent(long did, boolean forDelete) {
    Intent shortcutIntent = new Intent(ApplicationLoader.applicationContext, OpenChatReceiver.class);

    int lower_id = (int) did;
    int high_id = (int) (did >> 32);

    TLRPC.User user = null;/*from w  ww .  ja va2  s .c o m*/
    TLRPC.Chat chat = null;
    if (lower_id == 0) {
        shortcutIntent.putExtra("encId", high_id);
        TLRPC.EncryptedChat encryptedChat = MessagesController.getInstance().getEncryptedChat(high_id);
        if (encryptedChat == null) {
            return null;
        }
        user = MessagesController.getInstance().getUser(encryptedChat.user_id);
    } else if (lower_id > 0) {
        shortcutIntent.putExtra("userId", lower_id);
        user = MessagesController.getInstance().getUser(lower_id);
    } else if (lower_id < 0) {
        chat = MessagesController.getInstance().getChat(-lower_id);
        shortcutIntent.putExtra("chatId", -lower_id);
    } else {
        return null;
    }
    if (user == null && chat == null) {
        return null;
    }

    String name;
    TLRPC.FileLocation photo = null;

    if (user != null) {
        name = ContactsController.formatName(user.first_name, user.last_name);
        if (user.photo != null) {
            photo = user.photo.photo_small;
        }
    } else {
        name = chat.title;
        if (chat.photo != null) {
            photo = chat.photo.photo_small;
        }
    }

    shortcutIntent.setAction("com.tmessages.openchat" + did);
    shortcutIntent.addFlags(0x4000000);

    Intent addIntent = new Intent();
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
    addIntent.putExtra("duplicate", false);
    if (!forDelete) {
        Bitmap bitmap = null;
        if (photo != null) {
            try {
                File path = FileLoader.getPathToAttach(photo, true);
                bitmap = BitmapFactory.decodeFile(path.toString());
                if (bitmap != null) {
                    int size = AndroidUtilities.dp(58);
                    Bitmap result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
                    result.eraseColor(Color.TRANSPARENT);
                    Canvas canvas = new Canvas(result);
                    BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP,
                            Shader.TileMode.CLAMP);
                    if (roundPaint == null) {
                        roundPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
                        bitmapRect = new RectF();
                    }
                    float scale = size / (float) bitmap.getWidth();
                    canvas.save();
                    canvas.scale(scale, scale);
                    roundPaint.setShader(shader);
                    bitmapRect.set(0, 0, bitmap.getWidth(), bitmap.getHeight());
                    canvas.drawRoundRect(bitmapRect, bitmap.getWidth(), bitmap.getHeight(), roundPaint);
                    canvas.restore();
                    Drawable drawable = ApplicationLoader.applicationContext.getResources()
                            .getDrawable(R.drawable.book_logo);
                    int w = AndroidUtilities.dp(15);
                    int left = size - w - AndroidUtilities.dp(2);
                    int top = size - w - AndroidUtilities.dp(2);
                    drawable.setBounds(left, top, left + w, top + w);
                    drawable.draw(canvas);
                    try {
                        canvas.setBitmap(null);
                    } catch (Exception e) {
                        //don't promt, this will crash on 2.x
                    }
                    bitmap = result;
                }
            } catch (Throwable e) {
                FileLog.e(e);
            }
        }
        if (bitmap != null) {
            addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON, bitmap);
        } else {
            if (user != null) {
                if (user.bot) {
                    addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource
                            .fromContext(ApplicationLoader.applicationContext, R.drawable.book_bot));
                } else {
                    addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource
                            .fromContext(ApplicationLoader.applicationContext, R.drawable.book_user));
                }
            } else if (chat != null) {
                if (ChatObject.isChannel(chat) && !chat.megagroup) {
                    addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource
                            .fromContext(ApplicationLoader.applicationContext, R.drawable.book_channel));
                } else {
                    addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource
                            .fromContext(ApplicationLoader.applicationContext, R.drawable.book_group));
                }
            }
        }
    }
    return addIntent;
}

From source file:eu.davidea.flexibleadapter.common.FlexibleItemDecoration.java

@SuppressLint("NewApi")
private void drawVertical(Canvas canvas, RecyclerView parent) {
    canvas.save();//from w ww  . j  a v a2  s  .  com
    final int left;
    final int right;
    if (parent.getClipToPadding()) {
        left = parent.getPaddingLeft();
        right = parent.getWidth() - parent.getPaddingRight();
        canvas.clipRect(left, parent.getPaddingTop(), right, parent.getHeight() - parent.getPaddingBottom());
    } else {
        left = 0;
        right = parent.getWidth();
    }

    final int itemCount = parent.getChildCount();
    for (int i = 0; i < itemCount - 1; i++) {
        final View child = parent.getChildAt(i);
        parent.getDecoratedBoundsWithMargins(child, mBounds);
        final int bottom = mBounds.bottom + Math.round(ViewCompat.getTranslationY(child));
        final int top = bottom - mDivider.getIntrinsicHeight();
        mDivider.setBounds(left, top, right, bottom);
        mDivider.draw(canvas);
    }
    canvas.restore();
}

From source file:eu.davidea.flexibleadapter.common.FlexibleItemDecoration.java

@SuppressLint("NewApi")
private void drawHorizontal(Canvas canvas, RecyclerView parent) {
    canvas.save();//w w  w . j  ava2 s . co m
    final int top;
    final int bottom;
    if (parent.getClipToPadding()) {
        top = parent.getPaddingTop();
        bottom = parent.getHeight() - parent.getPaddingBottom();
        canvas.clipRect(parent.getPaddingLeft(), top, parent.getWidth() - parent.getPaddingRight(), bottom);
    } else {
        top = 0;
        bottom = parent.getHeight();
    }

    final int itemCount = parent.getChildCount();
    for (int i = 0; i < itemCount - 1; i++) {
        final View child = parent.getChildAt(i);
        parent.getLayoutManager().getDecoratedBoundsWithMargins(child, mBounds);
        final int right = mBounds.right + Math.round(ViewCompat.getTranslationX(child));
        final int left = right - mDivider.getIntrinsicWidth();
        mDivider.setBounds(left, top, right, bottom);
        mDivider.draw(canvas);
    }
    canvas.restore();
}

From source file:com.github.jokar.rxupload.widget.ProgressDownloadView.java

/**
 * MARK: Overrides/*  w  w  w.  jav  a 2  s. c om*/
 */

@Override
protected void onDraw(Canvas canvas) {
    if (mPathWhite != null && mPathBlack != null) {

        float textX = Math.max(getPaddingLeft() - (int) (mBubbleWidth / 4.0f),
                mProgress * mWidth / 100 - (int) (mBubbleWidth / 4.0f));
        float textY = mHeight / 2 - mBubbleHeight / 2 + calculateDeltaY();

        switch (mState) {
        case STATE_WORKING:
            // Save and restore prevent the rest of the canvas to not be rotated
            canvas.save();
            float speed = (getProgress() - mTarget) / 20;
            mBubbleAngle += speed * 10;
            if (mBubbleAngle > 20) {
                mBubbleAngle = 20;
            }
            if (mBubbleAngle < -20) {
                mBubbleAngle = -20;
            }
            if (Math.abs(speed) < 1) {
                mSpeedAngle -= mBubbleAngle / 20;
                mSpeedAngle *= .9f;
            }
            mBubbleAngle += mSpeedAngle;

            canvas.rotate(mBubbleAngle, bubbleAnchorX, bubbleAnchorY);
            canvas.drawPath(mPathBubble, mPaintBubble);
            canvas.drawText(String.valueOf((int) mProgress) + " %", textX, textY, mPaintText);
            canvas.restore();
            break;
        case STATE_FAILED:
            canvas.save();
            canvas.rotate(mFailAngle, bubbleAnchorX, bubbleAnchorY);
            canvas.drawPath(mPathBubble, mPaintBubble);
            canvas.rotate(mFailAngle, bubbleAnchorX, textY - mBubbleHeight / 7);
            //                    mPaintText.setColor(getResources().getColor(R.color.red_wine));
            textX = Math.max(getPaddingLeft() - (int) (mBubbleWidth / 3.2f),
                    mProgress * mWidth / 100 - (int) (mBubbleWidth / 3.2f));
            canvas.drawText(getResources().getString(R.string.failed), textX, textY, mPaintText);
            canvas.restore();
            break;
        case STATE_SUCCESS:
            canvas.save();
            //                    mPaintText.setColor(getResources().getColor(R.color.green_grass));
            textX = Math.max(getPaddingLeft() - (int) (mBubbleWidth / 3.2f),
                    mProgress * mWidth / 100 - (int) (mBubbleWidth / 3.2f));
            Matrix flipMatrix = new Matrix();
            flipMatrix.setScale(mFlipFactor, 1, bubbleAnchorX, bubbleAnchorY);
            canvas.concat(flipMatrix);
            canvas.drawPath(mPathBubble, mPaintBubble);
            canvas.concat(flipMatrix);
            canvas.drawText(getResources().getString(R.string.done), textX, textY, mPaintText);
            canvas.restore();
            break;
        }

        canvas.drawPath(mPathBlack, mPaintBlack);
        canvas.drawPath(mPathWhite, mPaintWhite);
    }
}

From source file:org.telegram.ui.Components.AvatarDrawable.java

@Override
public void draw(Canvas canvas) {
    Rect bounds = getBounds();//from   w  w w  .  jav  a  2s.c o m
    if (bounds == null) {
        return;
    }
    int size = bounds.width();
    paint.setColor(color);
    canvas.save();
    canvas.translate(bounds.left, bounds.top);
    canvas.drawCircle(size / 2, size / 2, size / 2, paint);

    if (drawBrodcast && broadcastDrawable != null) {
        int x = (size - broadcastDrawable.getIntrinsicWidth()) / 2;
        int y = (size - broadcastDrawable.getIntrinsicHeight()) / 2;
        broadcastDrawable.setBounds(x, y, x + broadcastDrawable.getIntrinsicWidth(),
                y + broadcastDrawable.getIntrinsicHeight());
        broadcastDrawable.draw(canvas);
    } else {
        if (textLayout != null) {
            canvas.translate((size - textWidth) / 2 - textLeft, (size - textHeight) / 2);
            textLayout.draw(canvas);
        } else if (drawPhoto && photoDrawable != null) {
            int x = (size - photoDrawable.getIntrinsicWidth()) / 2;
            int y = (size - photoDrawable.getIntrinsicHeight()) / 2;
            photoDrawable.setBounds(x, y, x + photoDrawable.getIntrinsicWidth(),
                    y + photoDrawable.getIntrinsicHeight());
            photoDrawable.draw(canvas);
        }
    }
    canvas.restore();
}

From source file:com.justwayward.reader.view.RVPIndicator.java

/**
 * (?view)/*from   w  w w .jav  a  2  s .  c  o  m*/
 */
@Override
protected void dispatchDraw(Canvas canvas) {
    // ?
    canvas.save();

    switch (mIndicatorStyle) {

    case STYLE_BITMAP:

        canvas.translate(mTranslationX, 0);
        canvas.drawBitmap(mBitmap, null, mRectF, mPaint);

        break;
    case STYLE_LINE:

        canvas.translate(mTranslationX, getHeight() - mIndicatorHeight);
        canvas.drawRect(mRectF, mPaint);

        break;
    case STYLE_SQUARE:

        canvas.translate(mTranslationX, 0);
        canvas.drawRect(mRectF, mPaint);

        break;
    case STYLE_TRIANGLE:

        canvas.translate(mTranslationX, 0);
        // 
        // mPaint.setPathEffect(new CornerPathEffect(10));
        mPath = new Path();
        int midOfTab = getWidth() / mTabVisibleCount / 2;
        mPath.moveTo(midOfTab, getHeight() - mIndicatorHeight);
        mPath.lineTo(midOfTab - mIndicatorWidth / 2, getHeight());
        mPath.lineTo(midOfTab + mIndicatorWidth / 2, getHeight());
        mPath.close();
        canvas.drawPath(mPath, mPaint);

        break;
    }

    // ??
    canvas.restore();
    super.dispatchDraw(canvas);
}

From source file:com.am.pagergradienttab.view.PagerGradientTabStrip.java

/**
 * //from w  ww  .j  av  a 2 s . c  om
 * 
 * @param canvas
 */
private void drawIndicator(Canvas canvas) {

    canvas.save();
    canvas.translate(
            getPaddingLeft() + currectPager * (itemWidth + intervalWidth) + indicatorPadding
                    + indicatorOffset * (itemWidth + intervalWidth),
            getHeight() - underLineHeight - getPaddingBottom() - indicatorHeight);
    if (showIndicator) {
        mTextPaint.setColor(indicatorColor);
        if (tabs.size() > 1)
            canvas.drawRect(0, 0, itemWidth - 2 * indicatorPadding, indicatorHeight, mTextPaint);
        else if (tabs.size() > 0)
            canvas.drawRect(itemWidth / 4, 0, itemWidth - itemWidth / 4 - 2 * indicatorPadding, indicatorHeight,
                    mTextPaint);
        else
            canvas.drawRect(itemWidth / 4, 0, itemWidth - itemWidth / 4 - 1 * indicatorPadding, indicatorHeight,
                    mTextPaint);
    }
    canvas.restore();
}

From source file:net.networksaremadeofstring.rhybudd.RhybuddDock.java

private void drawGaugeNeedle(Canvas canvas, int count, int Scale) {
    canvas.save(Canvas.MATRIX_SAVE_FLAG);
    float divisor = 360.0f / Scale;

    canvas.rotate((float) (divisor * count), 100, 100);

    //Inside//from   w ww .j  a  va 2  s.co m
    Paint needleInsidePaint = new Paint();
    needleInsidePaint.setStyle(Paint.Style.FILL_AND_STROKE);
    needleInsidePaint.setColor(Color.WHITE);
    needleInsidePaint.setStrokeWidth(4);
    needleInsidePaint.setAntiAlias(true);

    Paint needleEdgePaint = new Paint();
    needleEdgePaint.setStyle(Paint.Style.STROKE);
    needleEdgePaint.setColor(Color.DKGRAY);
    needleEdgePaint.setStrokeWidth(0.5f);
    needleEdgePaint.setAntiAlias(true);

    canvas.drawOval(new RectF(95, 95, 105, 105), needleInsidePaint);
    canvas.drawOval(new RectF(95, 96, 105, 105), needleEdgePaint);

    Path needleInside = new Path();
    needleInside.moveTo(98, 98);
    needleInside.lineTo(100, 20);
    needleInside.lineTo(102, 102);
    canvas.drawPath(needleInside, needleInsidePaint);

    Path needleEdge = new Path();
    needleInside.moveTo(99, 99);
    needleInside.lineTo(99, 19);
    needleInside.lineTo(103, 103);

    canvas.drawPath(needleEdge, needleEdgePaint);
    canvas.restore();
}