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.android.launcher3.folder.FolderIcon.java

private void drawPreviewItem(Canvas canvas, PreviewItemDrawingParams params) {
    canvas.save();
    canvas.translate(params.transX, params.transY);
    canvas.scale(params.scale, params.scale);
    Drawable d = params.drawable;//from  w ww  . ja  va  2s .  c  o  m

    if (d != null) {
        mOldBounds.set(d.getBounds());
        d.setBounds(0, 0, mIntrinsicIconSize, mIntrinsicIconSize);
        if (d instanceof FastBitmapDrawable) {
            FastBitmapDrawable fd = (FastBitmapDrawable) d;
            float oldBrightness = fd.getBrightness();
            fd.setBrightness(params.overlayAlpha);
            d.draw(canvas);
            fd.setBrightness(oldBrightness);
        } else {
            d.setColorFilter(Color.argb((int) (params.overlayAlpha * 255), 255, 255, 255),
                    PorterDuff.Mode.SRC_ATOP);
            d.draw(canvas);
            d.clearColorFilter();
        }
        d.setBounds(mOldBounds);
    }
    canvas.restore();
}

From source file:com.evilduck.piano.views.instrument.PianoView.java

@Override
protected void onDraw(Canvas canvas) {
    if (isInEditMode()) {
        canvas.drawColor(Color.GRAY);
        return;//  ww w  . j a  v a2s.  com
    }

    if (measurementChanged) {
        measurementChanged = false;
        keyboard.initializeInstrument(getMeasuredHeight(), getContext());

        float oldInstrumentWidth = instrumentWidth;
        instrumentWidth = keyboard.getWidth();

        float ratio = (float) instrumentWidth / oldInstrumentWidth;
        xOffset = (int) (xOffset * ratio);
    }

    int localXOffset = getOffsetInsideOfBounds();

    canvas.save();
    canvas.scale(scaleX, 1.0f);
    canvas.translate(-localXOffset, 0);

    keyboard.updateBounds(localXOffset, canvasWidth + localXOffset);
    keyboard.draw(canvas);

    if (!notesToDraw.isEmpty()) {
        keyboard.drawOverlays(notesToDraw, canvas);
    }

    canvas.restore();
}

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

@Override
protected void onDraw(Canvas canvas) {
    if (titleLayout != null) {
        canvas.save();
        canvas.translate(AndroidUtilities.dp(LocaleController.isRTL ? 8 : AndroidUtilities.leftBaseline),
                titleY);//from  ww  w.  j a  va  2 s.  c  o  m
        titleLayout.draw(canvas);
        canvas.restore();
    }

    if (descriptionLayout != null) {
        descriptionTextPaint.setColor(DESCRIPTION_TEXT_COLOR/*0xff8a8a8a*/);
        canvas.save();
        canvas.translate(AndroidUtilities.dp(LocaleController.isRTL ? 8 : AndroidUtilities.leftBaseline),
                descriptionY);
        descriptionLayout.draw(canvas);
        canvas.restore();
    }

    if (linkLayout != null) {
        descriptionTextPaint.setColor(Theme.MSG_LINK_TEXT_COLOR);
        canvas.save();
        canvas.translate(AndroidUtilities.dp(LocaleController.isRTL ? 8 : AndroidUtilities.leftBaseline),
                linkY);
        linkLayout.draw(canvas);
        canvas.restore();
    }

    if (!mediaWebpage) {
        if (documentAttachType == DOCUMENT_ATTACH_TYPE_AUDIO
                || documentAttachType == DOCUMENT_ATTACH_TYPE_MUSIC) {
            radialProgress.setProgressColor(buttonPressed ? Theme.MSG_IN_AUDIO_SELECTED_PROGRESS_COLOR
                    : Theme.MSG_IN_AUDIO_PROGRESS_COLOR);
            radialProgress.draw(canvas);
        } else if (inlineResult != null && inlineResult.type.equals("file")) {
            int w = Theme.inlineDocDrawable.getIntrinsicWidth();
            int h = Theme.inlineDocDrawable.getIntrinsicHeight();
            int x = linkImageView.getImageX() + (AndroidUtilities.dp(52) - w) / 2;
            int y = linkImageView.getImageY() + (AndroidUtilities.dp(52) - h) / 2;
            canvas.drawRect(linkImageView.getImageX(), linkImageView.getImageY(),
                    linkImageView.getImageX() + AndroidUtilities.dp(52),
                    linkImageView.getImageY() + AndroidUtilities.dp(52), LetterDrawable.paint);
            Theme.inlineDocDrawable.setBounds(x, y, x + w, y + h);
            Theme.inlineDocDrawable.draw(canvas);
        } else if (inlineResult != null
                && (inlineResult.type.equals("audio") || inlineResult.type.equals("voice"))) {
            int w = Theme.inlineAudioDrawable.getIntrinsicWidth();
            int h = Theme.inlineAudioDrawable.getIntrinsicHeight();
            int x = linkImageView.getImageX() + (AndroidUtilities.dp(52) - w) / 2;
            int y = linkImageView.getImageY() + (AndroidUtilities.dp(52) - h) / 2;
            canvas.drawRect(linkImageView.getImageX(), linkImageView.getImageY(),
                    linkImageView.getImageX() + AndroidUtilities.dp(52),
                    linkImageView.getImageY() + AndroidUtilities.dp(52), LetterDrawable.paint);
            Theme.inlineAudioDrawable.setBounds(x, y, x + w, y + h);
            Theme.inlineAudioDrawable.draw(canvas);
        } else if (inlineResult != null
                && (inlineResult.type.equals("venue") || inlineResult.type.equals("geo"))) {
            int w = Theme.inlineLocationDrawable.getIntrinsicWidth();
            int h = Theme.inlineLocationDrawable.getIntrinsicHeight();
            int x = linkImageView.getImageX() + (AndroidUtilities.dp(52) - w) / 2;
            int y = linkImageView.getImageY() + (AndroidUtilities.dp(52) - h) / 2;
            canvas.drawRect(linkImageView.getImageX(), linkImageView.getImageY(),
                    linkImageView.getImageX() + AndroidUtilities.dp(52),
                    linkImageView.getImageY() + AndroidUtilities.dp(52), LetterDrawable.paint);
            Theme.inlineLocationDrawable.setBounds(x, y, x + w, y + h);
            Theme.inlineLocationDrawable.draw(canvas);
        } else {
            letterDrawable.draw(canvas);
        }
    } else {
        if (inlineResult != null && (inlineResult.send_message instanceof TLRPC.TL_botInlineMessageMediaGeo
                || inlineResult.send_message instanceof TLRPC.TL_botInlineMessageMediaVenue)) {
            int w = Theme.inlineLocationDrawable.getIntrinsicWidth();
            int h = Theme.inlineLocationDrawable.getIntrinsicHeight();
            int x = linkImageView.getImageX() + (linkImageView.getImageWidth() - w) / 2;
            int y = linkImageView.getImageY() + (linkImageView.getImageHeight() - h) / 2;
            canvas.drawRect(linkImageView.getImageX(), linkImageView.getImageY(),
                    linkImageView.getImageX() + linkImageView.getImageWidth(),
                    linkImageView.getImageY() + linkImageView.getImageHeight(), LetterDrawable.paint);
            Theme.inlineLocationDrawable.setBounds(x, y, x + w, y + h);
            Theme.inlineLocationDrawable.draw(canvas);
        }
    }
    if (drawLinkImageView) {
        canvas.save();
        if (scaled && scale != 0.8f || !scaled && scale != 1.0f) {
            long newTime = System.currentTimeMillis();
            long dt = (newTime - lastUpdateTime);
            lastUpdateTime = newTime;
            if (scaled && scale != 0.8f) {
                scale -= dt / 400.0f;
                if (scale < 0.8f) {
                    scale = 0.8f;
                }
            } else {
                scale += dt / 400.0f;
                if (scale > 1.0f) {
                    scale = 1.0f;
                }
            }
            invalidate();
        }
        canvas.scale(scale, scale, getMeasuredWidth() / 2, getMeasuredHeight() / 2);
        linkImageView.draw(canvas);
        canvas.restore();
    }
    if (mediaWebpage && (documentAttachType == DOCUMENT_ATTACH_TYPE_PHOTO
            || documentAttachType == DOCUMENT_ATTACH_TYPE_GIF)) {
        radialProgress.setProgressColor(0xffffffff);
        radialProgress.draw(canvas);
    }

    if (needDivider && !mediaWebpage) {
        if (LocaleController.isRTL) {
            canvas.drawLine(0, getMeasuredHeight() - 1,
                    getMeasuredWidth() - AndroidUtilities.dp(AndroidUtilities.leftBaseline),
                    getMeasuredHeight() - 1, paint);
        } else {
            canvas.drawLine(AndroidUtilities.dp(AndroidUtilities.leftBaseline), getMeasuredHeight() - 1,
                    getMeasuredWidth(), getMeasuredHeight() - 1, paint);
        }
    }
    if (needShadow && shadowDrawable != null) {
        shadowDrawable.setBounds(0, 0, getMeasuredWidth(), AndroidUtilities.dp(3));
        shadowDrawable.draw(canvas);
    }
}

From source file:om.sstvencoder.CropView.java

private void drawBitmap(Canvas canvas) {
    int w = mImageWidth;
    int h = mImageHeight;
    for (int i = 0; i < mOrientation / 90; ++i) {
        int tmp = w;
        w = h;/*from ww  w.  j ava  2  s  .  c o  m*/
        h = tmp;
        //noinspection SuspiciousNameCombination
        mImageDrawRect.set(mImageDrawRect.top, h - mImageDrawRect.left, mImageDrawRect.bottom,
                h - mImageDrawRect.right);
        //noinspection SuspiciousNameCombination
        mCanvasDrawRect.set(mCanvasDrawRect.top, -mCanvasDrawRect.right, mCanvasDrawRect.bottom,
                -mCanvasDrawRect.left);
    }
    mImageDrawRect.sort();
    canvas.save();
    canvas.rotate(mOrientation);
    if (!mSmallImage) {
        int sampleSize = getSampleSize();
        if (sampleSize < mCacheSampleSize || !mCacheRect.contains(mImageDrawRect)) {
            if (mCacheBitmap != null)
                mCacheBitmap.recycle();
            int cacheWidth = mImageDrawRect.width();
            int cacheHeight = mImageDrawRect.height();
            while (cacheWidth * cacheHeight < (sampleSize * 1024 * sampleSize * 1024)) {
                cacheWidth += mImageDrawRect.width();
                cacheHeight += mImageDrawRect.height();
            }
            mCacheRect.set(Math.max(0, ~(sampleSize - 1) & (mImageDrawRect.centerX() - cacheWidth / 2)),
                    Math.max(0, ~(sampleSize - 1) & (mImageDrawRect.centerY() - cacheHeight / 2)),
                    Math.min(mRegionDecoder.getWidth(),
                            ~(sampleSize - 1) & (mImageDrawRect.centerX() + cacheWidth / 2 + sampleSize - 1)),
                    Math.min(mRegionDecoder.getHeight(),
                            ~(sampleSize - 1) & (mImageDrawRect.centerY() + cacheHeight / 2 + sampleSize - 1)));
            mBitmapOptions.inSampleSize = mCacheSampleSize = sampleSize;
            mCacheBitmap = mRegionDecoder.decodeRegion(mCacheRect, mBitmapOptions);
        }
        mImageDrawRect.offset(-mCacheRect.left, -mCacheRect.top);
        mImageDrawRect.left /= mCacheSampleSize;
        mImageDrawRect.top /= mCacheSampleSize;
        mImageDrawRect.right /= mCacheSampleSize;
        mImageDrawRect.bottom /= mCacheSampleSize;
    }
    canvas.drawBitmap(mCacheBitmap, mImageDrawRect, mCanvasDrawRect, mPaint);
    canvas.restore();
}

From source file:pl.motyczko.scrollheader.PagerSlidingTabStrip.java

private void drawTabs(Canvas canvas) {
    canvas.save();
    canvas.translate(0, getHeight() - tabsContainer.getHeight());
    if (isInEditMode() || tabCount == 0) {
        return;/*  www  .  j a  v  a2  s. co  m*/
    }

    final int height = tabsContainer.getHeight();

    // draw indicator line

    rectPaint.setColor(indicatorColor);

    // default: line below current tab
    View currentTab = tabsContainer.getChildAt(currentPosition);
    float lineLeft = currentTab.getLeft();
    float lineRight = currentTab.getRight();

    // if there is an offset, start interpolating left and right coordinates between current and next tab
    if (currentPositionOffset > 0f && currentPosition < tabCount - 1) {

        View nextTab = tabsContainer.getChildAt(currentPosition + 1);
        final float nextTabLeft = nextTab.getLeft();
        final float nextTabRight = nextTab.getRight();

        lineLeft = (currentPositionOffset * nextTabLeft + (1f - currentPositionOffset) * lineLeft);
        lineRight = (currentPositionOffset * nextTabRight + (1f - currentPositionOffset) * lineRight);
    }

    canvas.drawRect(lineLeft, height - indicatorHeight, lineRight, height, rectPaint);

    // draw underline

    rectPaint.setColor(underlineColor);
    canvas.drawRect(0, height - underlineHeight, tabsContainer.getWidth(), height, rectPaint);

    // draw divider

    dividerPaint.setColor(dividerColor);
    for (int i = 0; i < tabCount - 1; i++) {
        View tab = tabsContainer.getChildAt(i);
        canvas.drawLine(tab.getRight(), dividerPadding, tab.getRight(), height - dividerPadding, dividerPaint);
    }

    canvas.restore();
}

From source file:pl.motyczko.scrollheader.PagerSlidingTabStrip.java

private void drawIcon(Canvas canvas) {
    if (mIcon == null)
        return;/* w ww .j  a va  2 s. com*/
    canvas.save();
    float translationX = calculateIconTranslationX();
    float translationY = calculateIconTranslationY();
    canvas.translate(translationX, translationY);
    float scale = calculateIconScale();
    canvas.scale(scale, scale);
    mIcon.draw(canvas);
    canvas.restore();
}

From source file:kankan.wheel.widget.WheelView.java

/**
 * Draws items/*from  w  w w.ja va2 s  .  c om*/
 * @param canvas the canvas for drawing
 */
private void drawItems(Canvas canvas) {
    canvas.save();

    int top = (currentItem - firstItem) * getItemHeight() + (getItemHeight() - getHeight()) / 2;
    canvas.translate(PADDING, -top + scrollingOffset);

    //??
    AbstractWheelTextAdapter adapter = null;
    if (viewAdapter instanceof AbstractWheelTextAdapter) {
        adapter = (AbstractWheelTextAdapter) viewAdapter;
    }
    if (adapter != null && adapter.isEnableMultiTextColor()) {
        int targetIndex = -1;
        int minDis = Integer.MAX_VALUE;
        for (int i = 0; i < itemsLayout.getChildCount(); i++) {
            TextView child = (TextView) itemsLayout.getChildAt(i);
            int realTop = child.getTop() - top + scrollingOffset;
            int viewCenter = realTop + child.getHeight() / 2;
            Rect bounds = centerDrawable.getBounds();
            int dis = Math.abs(viewCenter - bounds.centerY());
            if (dis < minDis) {
                minDis = dis;
                targetIndex = i;
            }
        }

        for (int i = 0; i < itemsLayout.getChildCount(); i++) {
            TextView view = (TextView) itemsLayout.getChildAt(i);
            view.setAlpha(0.5f);
            view.setTextColor(adapter.getTextColor());
            view.setTextSize(adapter.getTextSize());
            adapter.setTextViewPadding(view, adapter.getTextPaddingTop(), adapter.getTextPaddingBottom());

        }

        if (targetIndex != -1) {
            TextView view = (TextView) itemsLayout.getChildAt(targetIndex);
            view.setAlpha(1);
            view.setTextColor(adapter.getTextSelectedColor());
            view.setTextSize(adapter.getTextSize() + 4);
            adapter.setTextViewPadding(view, adapter.getTextPaddingTop() - 2,
                    adapter.getTextPaddingBottom() - 2);
        }

        TextView view = (TextView) itemsLayout.getChildAt(targetIndex - 1);
        if (view != null)
            view.setAlpha(1);
        view = (TextView) itemsLayout.getChildAt(targetIndex + 1);
        if (view != null)
            view.setAlpha(1);
    }

    itemsLayout.draw(canvas);

    canvas.restore();
}

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

@Override
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
    boolean result = super.drawChild(canvas, child, drawingTime);
    if (child == imageView) {
        if (countLayout != null) {
            int top = AndroidUtilities.dp(6);
            int left = AndroidUtilities.dp(54);
            int x = left - AndroidUtilities.dp(5.5f);
            if (MessagesController.getInstance().isDialogMuted(dialog_id)) {
                countDrawableGrey.setBounds(x, top, x + countWidth + AndroidUtilities.dp(11),
                        top + countDrawableGrey.getIntrinsicHeight());
                countDrawableGrey.draw(canvas);
            } else {
                countDrawable.setBounds(x, top, x + countWidth + AndroidUtilities.dp(11),
                        top + countDrawable.getIntrinsicHeight());
                countDrawable.draw(canvas);
            }/*from   ww w.ja va  2  s  .  c o  m*/
            canvas.save();
            canvas.translate(left, top + AndroidUtilities.dp(4));
            countLayout.draw(canvas);
            canvas.restore();
        }
    }
    return result;
}

From source file:kr.wdream.storyshop.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. j a  v a  2 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("tmessages", 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:com.hippo.widget.Slider.java

@Override
protected void onDraw(@NonNull Canvas canvas) {
    int width = getWidth();
    int height = getHeight();
    if (width < LayoutUtils.dp2pix(mContext, 24)) {
        canvas.drawRect(0, 0, width, getHeight(), mPaint);
    } else {/*ww w.  j a v  a2 s  .c  om*/
        int paddingLeft = getPaddingLeft();
        int paddingTop = getPaddingTop();
        int paddingRight = getPaddingRight();
        int paddingBottom = getPaddingBottom();
        float thickness = mThickness;
        float radius = mRadius;
        float halfThickness = thickness / 2;

        int saved = canvas.save();

        canvas.translate(0, paddingTop + ((height - paddingTop - paddingBottom) / 2));

        float currentX = paddingLeft + radius + (width - radius - radius - paddingLeft - paddingRight)
                * (mReverse ? (1.0f - mDrawPercent) : mDrawPercent);

        mLeftRectF.set(paddingLeft + radius, -halfThickness, currentX, halfThickness);
        mRightRectF.set(currentX, -halfThickness, width - paddingRight - radius, halfThickness);

        // Draw bar
        if (mReverse) {
            canvas.drawRect(mRightRectF, mPaint);
            canvas.drawRect(mLeftRectF, mBgPaint);
        } else {
            canvas.drawRect(mLeftRectF, mPaint);
            canvas.drawRect(mRightRectF, mBgPaint);
        }

        // Draw controller
        float scale = 1.0f - mDrawBubbleScale;
        if (scale != 0.0f) {
            canvas.scale(scale, scale, currentX, 0);
            canvas.drawCircle(currentX, 0, radius, mPaint);
        }

        canvas.restoreToCount(saved);
    }
}