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:ca.barrenechea.widget.recyclerview.decoration.DoubleHeaderDecoration.java

/**
 * {@inheritDoc}/*from  w  ww . ja v  a  2 s .c  om*/
 */
@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
    final int count = parent.getChildCount();

    boolean headerDrawn = false;
    for (int layoutPos = 0; layoutPos < count; layoutPos++) {
        final View child = parent.getChildAt(layoutPos);
        boolean visible = getAnimatedTop(child) > -child.getHeight()/* && child.getTop() < parent.getHeight()*/;
        final int adapterPos = parent.getChildAdapterPosition(child);
        if (visible && adapterPos != RecyclerView.NO_POSITION && (!headerDrawn || hasSubHeader(adapterPos))) {
            int left, top;
            final View header = getHeader(parent, adapterPos).itemView;
            final View subHeader = getSubHeader(parent, adapterPos).itemView;

            c.save();
            left = child.getLeft();
            top = getSubHeaderTop(parent, child, header, subHeader, adapterPos, layoutPos);
            c.translate(left, top);
            subHeader.setTranslationX(left);
            subHeader.setTranslationY(top);
            subHeader.draw(c);
            c.restore();

            if (!headerDrawn || hasHeader(adapterPos)) {
                c.save();
                left = child.getLeft();
                top = getHeaderTop(parent, child, header, subHeader, adapterPos, layoutPos);
                c.translate(left, top);
                header.setTranslationX(left);
                header.setTranslationY(top);
                header.draw(c);
                c.restore();
            }

            headerDrawn = true;
        }
    }
}

From source file:android.widget.PinnedHeaderListView.java

private void drawHeader(final Canvas canvas, final PinnedHeader header, final long currentTime) {
    if (header.animating) {
        final int timeLeft = (int) (header.targetTime - currentTime);
        if (timeLeft <= 0) {
            header.y = header.targetY;// www  .  j ava 2s.  co m
            header.visible = header.targetVisible;
            header.animating = false;
        } else
            header.y = header.targetY + (header.sourceY - header.targetY) * timeLeft / mAnimationDuration;
    }
    if (header.visible) {
        final View view = header.view;
        final int saveCount = canvas.save();
        final int translateX = ViewUtil.isViewLayoutRtl(this)
                ? getWidth() - mHeaderPaddingStart - view.getWidth()
                : mHeaderPaddingStart;
        canvas.translate(translateX, header.y);
        if (header.state == FADING) {
            mBounds.set(0, 0, view.getWidth(), view.getHeight());
            canvas.saveLayerAlpha(mBounds, header.alpha, Canvas.ALL_SAVE_FLAG);
        }
        view.draw(canvas);
        canvas.restoreToCount(saveCount);
    }
}

From source file:com.justplay1.shoppist.features.search.widget.internal.SuggestionItemDecorator.java

@Override
public void onDraw(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
    int visibleCount = parent.getChildCount();
    int count = state.getItemCount();
    RecyclerView.Adapter adapter = parent.getAdapter();
    int adapterCount = adapter != null ? adapter.getItemCount() : 0;

    for (int i = 0; i < visibleCount; i++) {
        View view = parent.getChildAt(i);
        int position = parent.getChildAdapterPosition(view);
        float translationX = ViewCompat.getTranslationX(view);
        float translationY = ViewCompat.getTranslationY(view);
        float alpha = ViewCompat.getAlpha(view);
        RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) view.getLayoutParams();

        int shadows = LEFT | RIGHT;
        if (position == count - 1 && adapterCount != 0)
            shadows |= BOTTOM;/* w w  w. j  ava 2 s  .c om*/

        drawable.setAlpha((int) (255 * alpha));
        drawable.setShadow(shadows);
        drawable.setBounds(0, 0, parent.getWidth(), view.getHeight());
        int saved = canvas.save();
        canvas.translate(parent.getPaddingLeft() + translationX,
                view.getTop() + params.topMargin + translationY);
        drawable.draw(canvas);
        canvas.restoreToCount(saved);
    }
}

From source file:com.kmagic.solitaire.DrawMaster.java

/**
 * Draw card value and small suit below value
 * in both top left and bottom right corners
 * bottom right is reversed/upside down//w  ww  . ja v a  2  s . c  om
 * @param paint paint styled for card value and small suit
 * @param canvas canvas to draw on
 * @param value card value
 * @param smallSuit small card suit
 * @param suitIdx suit index
 */
private void drawCardValue(final Paint paint, final Canvas canvas, final String value, final Bitmap smallSuit,
        final int suitIdx) {
    if ((suitIdx & 1) == 1) {
        paint.setARGB(255, 255, 0, 0);
    } else {
        paint.setARGB(255, 0, 0, 0);
    }
    canvas.drawText(value, mSuitsSize4th, mSuitsSize, paint);
    canvas.drawBitmap(smallSuit, mSuitsSize4th, mSuitsSize4th + mSuitsSize, mSuitPaint);
    canvas.save();
    canvas.rotate(180, Card.WIDTH / 2, Card.HEIGHT / 2);
    canvas.drawBitmap(smallSuit, mSuitsSize4th, mSuitsSize4th + mSuitsSize, mSuitPaint);
    canvas.drawText(value, mSuitsSize4th, mSuitsSize, paint);
    canvas.restore();
}

From source file:com.appunite.scroll.ScaleImageView.java

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

    // Clips the next few drawing operations to the content area
    int clipRestoreCount = canvas.save();
    canvas.clipRect(mContentRect);/*from   ww w  .j  a v a2  s.c  o m*/

    if (mSrc != null) {
        getRealTranslation(mTranslation, mRealTranslation);
        canvas.translate(mRealTranslation.x, mRealTranslation.y);
        canvas.scale(mScale, mScale);
        Log.v(TAG, "scale=" + mScale + " translation=" + mTranslation.toString() + " realTranslation="
                + mRealTranslation.toString());
        mSrc.draw(canvas);
    }

    // Removes clipping rectangle
    canvas.restoreToCount(clipRestoreCount);

    drawEdgeEffectsUnclipped(canvas);

}

From source file:com.acbelter.scheduleview.ScheduleView.java

private void drawEdgeEffects(Canvas canvas) {
    boolean needsInvalidate = false;

    final int overScrollMode = ViewCompat.getOverScrollMode(this);
    if (overScrollMode == ViewCompat.OVER_SCROLL_ALWAYS
            || overScrollMode == ViewCompat.OVER_SCROLL_IF_CONTENT_SCROLLS) {
        if (!mTopEdgeEffect.isFinished()) {
            int saveCount = canvas.save();
            int width = mViewWidth - getPaddingLeft() - getPaddingRight();
            int height = mViewHeight - getPaddingTop() - getPaddingBottom();

            canvas.translate(0, getPaddingTop());

            mTopEdgeEffect.setSize(width, height);
            needsInvalidate |= mTopEdgeEffect.draw(canvas);
            canvas.restoreToCount(saveCount);
        }/*from   ww  w  .  java 2 s  .  c o m*/
        if (!mBottomEdgeEffect.isFinished()) {
            int saveCount = canvas.save();
            int width = mViewWidth - getPaddingLeft() - getPaddingRight();
            int height = mViewHeight - getPaddingTop() - getPaddingBottom();

            canvas.translate(mViewWidth, mViewHeight - getPaddingBottom());
            canvas.rotate(180);

            mBottomEdgeEffect.setSize(width, height);
            needsInvalidate |= mBottomEdgeEffect.draw(canvas);
            canvas.restoreToCount(saveCount);
        }
    } else {
        mTopEdgeEffect.finish();
        mBottomEdgeEffect.finish();
    }

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

From source file:com.android.gallery3d.filtershow.imageshow.ImageShow.java

public void drawCompareImage(Canvas canvas, Bitmap image) {
    MasterImage master = MasterImage.getImage();
    boolean showsOriginal = master.showsOriginal();
    if (!showsOriginal && !mTouchShowOriginal)
        return;/*from   ww w .ja  v a 2  s . c  o  m*/
    canvas.save();
    if (image != null) {
        if (mShowOriginalDirection == 0) {
            if (Math.abs(mTouch.y - mTouchDown.y) > Math.abs(mTouch.x - mTouchDown.x)) {
                mShowOriginalDirection = UNVEIL_VERTICAL;
            } else {
                mShowOriginalDirection = UNVEIL_HORIZONTAL;
            }
        }

        int px = 0;
        int py = 0;
        if (mShowOriginalDirection == UNVEIL_VERTICAL) {
            px = mImageBounds.width();
            py = mTouch.y - mImageBounds.top;
        } else {
            px = mTouch.x - mImageBounds.left;
            py = mImageBounds.height();
            if (showsOriginal) {
                px = mImageBounds.width();
            }
        }

        Rect d = new Rect(mImageBounds.left, mImageBounds.top, mImageBounds.left + px, mImageBounds.top + py);
        if (mShowOriginalDirection == UNVEIL_HORIZONTAL) {
            if (mTouchDown.x - mTouch.x > 0) {
                d.set(mImageBounds.left + px, mImageBounds.top, mImageBounds.right, mImageBounds.top + py);
            }
        } else {
            if (mTouchDown.y - mTouch.y > 0) {
                d.set(mImageBounds.left, mImageBounds.top + py, mImageBounds.left + px, mImageBounds.bottom);
            }
        }
        canvas.clipRect(d);
        Matrix m = master.computeImageToScreen(image, 0, false);
        canvas.drawBitmap(image, m, mPaint);
        Paint paint = new Paint();
        paint.setColor(Color.BLACK);
        paint.setStrokeWidth(3);

        if (mShowOriginalDirection == UNVEIL_VERTICAL) {
            canvas.drawLine(mImageBounds.left, mTouch.y, mImageBounds.right, mTouch.y, paint);
        } else {
            canvas.drawLine(mTouch.x, mImageBounds.top, mTouch.x, mImageBounds.bottom, paint);
        }

        Rect bounds = new Rect();
        paint.setAntiAlias(true);
        paint.setTextSize(mOriginalTextSize);
        paint.getTextBounds(mOriginalText, 0, mOriginalText.length(), bounds);
        paint.setColor(Color.BLACK);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(3);
        canvas.drawText(mOriginalText, mImageBounds.left + mOriginalTextMargin,
                mImageBounds.top + bounds.height() + mOriginalTextMargin, paint);
        paint.setStyle(Paint.Style.FILL);
        paint.setStrokeWidth(1);
        paint.setColor(Color.WHITE);
        canvas.drawText(mOriginalText, mImageBounds.left + mOriginalTextMargin,
                mImageBounds.top + bounds.height() + mOriginalTextMargin, paint);
    }
    canvas.restore();
}

From source file:org.chromium.chrome.browser.omnibox.SuggestionView.java

/**
 * Constructs a new omnibox suggestion view.
 *
 * @param context The context used to construct the suggestion view.
 * @param locationBar The location bar showing these suggestions.
 *///w ww  .  jav  a 2 s .c o m
public SuggestionView(Context context, LocationBar locationBar) {
    super(context);
    mLocationBar = locationBar;

    mSuggestionHeight = context.getResources().getDimensionPixelOffset(R.dimen.omnibox_suggestion_height);
    mSuggestionAnswerHeight = context.getResources()
            .getDimensionPixelOffset(R.dimen.omnibox_suggestion_answer_height);

    TypedArray a = getContext().obtainStyledAttributes(new int[] { R.attr.selectableItemBackground });
    Drawable itemBackground = a.getDrawable(0);
    a.recycle();

    mContentsView = new SuggestionContentsContainer(context, itemBackground);
    addView(mContentsView);

    mRefineView = new View(context) {
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);

            if (mRefineIcon == null)
                return;
            canvas.save();
            canvas.translate((getMeasuredWidth() - mRefineIcon.getIntrinsicWidth()) / 2f,
                    (getMeasuredHeight() - mRefineIcon.getIntrinsicHeight()) / 2f);
            mRefineIcon.draw(canvas);
            canvas.restore();
        }

        @Override
        public void setVisibility(int visibility) {
            super.setVisibility(visibility);

            if (visibility == VISIBLE) {
                setClickable(true);
                setFocusable(true);
            } else {
                setClickable(false);
                setFocusable(false);
            }
        }

        @Override
        protected void drawableStateChanged() {
            super.drawableStateChanged();

            if (mRefineIcon != null && mRefineIcon.isStateful()) {
                mRefineIcon.setState(getDrawableState());
            }
        }
    };
    mRefineView.setContentDescription(getContext().getString(R.string.accessibility_omnibox_btn_refine));

    // Although this has the same background as the suggestion view, it can not be shared as
    // it will result in the state of the drawable being shared and always showing up in the
    // refine view.
    mRefineView.setBackground(itemBackground.getConstantState().newDrawable());
    mRefineView.setId(R.id.refine_view_id);
    mRefineView.setClickable(true);
    mRefineView.setFocusable(true);
    mRefineView.setLayoutParams(new LayoutParams(0, 0));
    addView(mRefineView);

    mRefineWidth = getResources().getDimensionPixelSize(R.dimen.omnibox_suggestion_refine_width);

    mUrlBar = (UrlBar) locationBar.getContainerView().findViewById(R.id.url_bar);

    mPhoneUrlBarLeftOffsetPx = getResources()
            .getDimensionPixelOffset(R.dimen.omnibox_suggestion_phone_url_bar_left_offset);
    mPhoneUrlBarLeftOffsetRtlPx = getResources()
            .getDimensionPixelOffset(R.dimen.omnibox_suggestion_phone_url_bar_left_offset_rtl);
}

From source file:com.example.view.wheel.WheelView.java

/**
 * Draws value and label layout/*from   w  w  w.java  2s . co m*/
 * @param canvas the canvas for drawing
 */
private void drawValue(Canvas canvas) {
    valuePaint.setColor(VALUE_TEXT_COLOR);
    valuePaint.drawableState = getDrawableState();

    Rect bounds = new Rect();
    itemsLayout.getLineBounds(visibleItems / 2, bounds);

    // draw label
    if (labelLayout != null) {
        canvas.save();
        canvas.translate(itemsLayout.getWidth() + LABEL_OFFSET, bounds.top);
        labelLayout.draw(canvas);
        canvas.restore();
    }

    // draw current value
    if (valueLayout != null) {
        canvas.save();
        canvas.translate(0, bounds.top + scrollingOffset);
        valueLayout.draw(canvas);
        canvas.restore();
    }
}

From source file:com.mappn.gfan.ui.HomeTabActivity.java

private Bitmap drawText(DisplayMetrics dm, Resources res, Bitmap bm, int num) {
    final int height = bm.getScaledHeight(dm);
    final int width = bm.getScaledWidth(dm);
    Bitmap newBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(newBitmap);
    canvas.drawBitmap(bm, new Matrix(), new Paint());
    Paint textPainter = new Paint(Paint.ANTI_ALIAS_FLAG);
    textPainter.setColor(res.getColor(R.color.tab_app_num));
    textPainter.setTextSize(dm.scaledDensity * 12);
    textPainter.setTypeface(Typeface.DEFAULT_BOLD);
    float textWidth = textPainter.measureText(String.valueOf(num)) / 2;
    canvas.drawText(String.valueOf(num), width / 2 - textWidth, height / 2 + (dm.scaledDensity * 6),
            textPainter);//from  w w w.  ja  v  a2 s . co m
    canvas.save();
    return newBitmap;
}