Example usage for android.graphics Canvas restoreToCount

List of usage examples for android.graphics Canvas restoreToCount

Introduction

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

Prototype

public void restoreToCount(int saveCount) 

Source Link

Document

Efficient way to pop any calls to save() that happened after the save count reached saveCount.

Usage

From source file:com.gmy.widget.swiperefresh.SwipeProgressBar.java

void draw(Canvas canvas) {
    final int width = mBounds.width();
    final int height = mBounds.height();
    final int cx = width / 2;
    // final int cy = height / 2;
    final int cy = mBounds.bottom - height / 2;
    boolean drawTriggerWhileFinishing = false;
    int restoreCount = canvas.save();
    canvas.clipRect(mBounds);//from w  w w .  java 2s  . c  o m

    if (mRunning || (mFinishTime > 0)) {
        long now = AnimationUtils.currentAnimationTimeMillis();
        long elapsed = (now - mStartTime) % ANIMATION_DURATION_MS;
        long iterations = (now - mStartTime) / ANIMATION_DURATION_MS;
        float rawProgress = (elapsed / (ANIMATION_DURATION_MS / 100f));

        // If we're not running anymore, that means we're running through
        // the finish animation.
        if (!mRunning) {
            // If the finish animation is done, don't draw anything, and
            // don't repost.
            if ((now - mFinishTime) >= FINISH_ANIMATION_DURATION_MS) {
                mFinishTime = 0;
                return;
            }

            // Otherwise, use a 0 opacity alpha layer to clear the animation
            // from the inside out. This layer will prevent the circles from
            // drawing within its bounds.
            long finishElapsed = (now - mFinishTime) % FINISH_ANIMATION_DURATION_MS;
            float finishProgress = (finishElapsed / (FINISH_ANIMATION_DURATION_MS / 100f));
            float pct = (finishProgress / 100f);
            // Radius of the circle is half of the screen.
            float clearRadius = width / 2 * INTERPOLATOR.getInterpolation(pct);
            mClipRect.set(cx - clearRadius, 0, cx + clearRadius, height);
            canvas.saveLayerAlpha(mClipRect, 0, 0);
            // Only draw the trigger if there is a space in the center of
            // this refreshing view that needs to be filled in by the
            // trigger. If the progress view is just still animating, let it
            // continue animating.
            drawTriggerWhileFinishing = true;
        }

        // First fill in with the last color that would have finished
        // drawing.
        if (iterations == 0) {
            canvas.drawColor(mColor1);
        } else {
            if (rawProgress >= 0 && rawProgress < 25) {
                canvas.drawColor(mColor4);
            } else if (rawProgress >= 25 && rawProgress < 50) {
                canvas.drawColor(mColor1);
            } else if (rawProgress >= 50 && rawProgress < 75) {
                canvas.drawColor(mColor2);
            } else {
                canvas.drawColor(mColor3);
            }
        }

        // Then draw up to 4 overlapping concentric circles of varying
        // radii, based on how far
        // along we are in the cycle.
        // progress 0-50 draw mColor2
        // progress 25-75 draw mColor3
        // progress 50-100 draw mColor4
        // progress 75 (wrap to 25) draw mColor1
        if ((rawProgress >= 0 && rawProgress <= 25)) {
            float pct = (((rawProgress + 25) * 2) / 100f);
            drawCircle(canvas, cx, cy, mColor1, pct);
        }
        if (rawProgress >= 0 && rawProgress <= 50) {
            float pct = ((rawProgress * 2) / 100f);
            drawCircle(canvas, cx, cy, mColor2, pct);
        }
        if (rawProgress >= 25 && rawProgress <= 75) {
            float pct = (((rawProgress - 25) * 2) / 100f);
            drawCircle(canvas, cx, cy, mColor3, pct);
        }
        if (rawProgress >= 50 && rawProgress <= 100) {
            float pct = (((rawProgress - 50) * 2) / 100f);
            drawCircle(canvas, cx, cy, mColor4, pct);
        }
        if ((rawProgress >= 75 && rawProgress <= 100)) {
            float pct = (((rawProgress - 75) * 2) / 100f);
            drawCircle(canvas, cx, cy, mColor1, pct);
        }
        if (mTriggerPercentage > 0 && drawTriggerWhileFinishing) {
            // There is some portion of trigger to draw. Restore the canvas,
            // then draw the trigger. Otherwise, the trigger does not appear
            // until after the bar has finished animating and appears to
            // just jump in at a larger width than expected.
            canvas.restoreToCount(restoreCount);
            restoreCount = canvas.save();
            canvas.clipRect(mBounds);
            drawTrigger(canvas, cx, cy);
        }
        // Keep running until we finish out the last cycle.
        ViewCompat.postInvalidateOnAnimation(mParent);
    } else {
        // Otherwise if we're in the middle of a trigger, draw that.
        if (mTriggerPercentage > 0 && mTriggerPercentage <= 1.0) {
            drawTrigger(canvas, cx, cy);
        }
    }
    canvas.restoreToCount(restoreCount);
}

From source file:com.swiperefreshlayout.SwipeProgressBar.java

void draw(Canvas canvas) {
    final int width = mBounds.width();
    final int height = mBounds.height();
    final int cx = width / 2;
    //        final int cy = height / 2;  
    final int cy = mBounds.bottom - height / 2;
    boolean drawTriggerWhileFinishing = false;
    int restoreCount = canvas.save();
    canvas.clipRect(mBounds);//from   ww w . ja  v  a2 s  .  c om

    if (mRunning || (mFinishTime > 0)) {
        long now = AnimationUtils.currentAnimationTimeMillis();
        long elapsed = (now - mStartTime) % ANIMATION_DURATION_MS;
        long iterations = (now - mStartTime) / ANIMATION_DURATION_MS;
        float rawProgress = (elapsed / (ANIMATION_DURATION_MS / 100f));

        // If we're not running anymore, that means we're running through  
        // the finish animation.  
        if (!mRunning) {
            // If the finish animation is done, don't draw anything, and  
            // don't repost.  
            if ((now - mFinishTime) >= FINISH_ANIMATION_DURATION_MS) {
                mFinishTime = 0;
                return;
            }

            // Otherwise, use a 0 opacity alpha layer to clear the animation  
            // from the inside out. This layer will prevent the circles from  
            // drawing within its bounds.  
            long finishElapsed = (now - mFinishTime) % FINISH_ANIMATION_DURATION_MS;
            float finishProgress = (finishElapsed / (FINISH_ANIMATION_DURATION_MS / 100f));
            float pct = (finishProgress / 100f);
            // Radius of the circle is half of the screen.  
            float clearRadius = width / 2 * INTERPOLATOR.getInterpolation(pct);
            mClipRect.set(cx - clearRadius, 0, cx + clearRadius, height);
            canvas.saveLayerAlpha(mClipRect, 0, 0);
            // Only draw the trigger if there is a space in the center of  
            // this refreshing view that needs to be filled in by the  
            // trigger. If the progress view is just still animating, let it  
            // continue animating.  
            drawTriggerWhileFinishing = true;
        }

        // First fill in with the last color that would have finished drawing.  
        if (iterations == 0) {
            canvas.drawColor(mColor1);
        } else {
            if (rawProgress >= 0 && rawProgress < 25) {
                canvas.drawColor(mColor4);
            } else if (rawProgress >= 25 && rawProgress < 50) {
                canvas.drawColor(mColor1);
            } else if (rawProgress >= 50 && rawProgress < 75) {
                canvas.drawColor(mColor2);
            } else {
                canvas.drawColor(mColor3);
            }
        }

        // Then draw up to 4 overlapping concentric circles of varying radii, based on how far  
        // along we are in the cycle.  
        // progress 0-50 draw mColor2  
        // progress 25-75 draw mColor3  
        // progress 50-100 draw mColor4  
        // progress 75 (wrap to 25) draw mColor1  
        if ((rawProgress >= 0 && rawProgress <= 25)) {
            float pct = (((rawProgress + 25) * 2) / 100f);
            drawCircle(canvas, cx, cy, mColor1, pct);
        }
        if (rawProgress >= 0 && rawProgress <= 50) {
            float pct = ((rawProgress * 2) / 100f);
            drawCircle(canvas, cx, cy, mColor2, pct);
        }
        if (rawProgress >= 25 && rawProgress <= 75) {
            float pct = (((rawProgress - 25) * 2) / 100f);
            drawCircle(canvas, cx, cy, mColor3, pct);
        }
        if (rawProgress >= 50 && rawProgress <= 100) {
            float pct = (((rawProgress - 50) * 2) / 100f);
            drawCircle(canvas, cx, cy, mColor4, pct);
        }
        if ((rawProgress >= 75 && rawProgress <= 100)) {
            float pct = (((rawProgress - 75) * 2) / 100f);
            drawCircle(canvas, cx, cy, mColor1, pct);
        }
        if (mTriggerPercentage > 0 && drawTriggerWhileFinishing) {
            // There is some portion of trigger to draw. Restore the canvas,  
            // then draw the trigger. Otherwise, the trigger does not appear  
            // until after the bar has finished animating and appears to  
            // just jump in at a larger width than expected.  
            canvas.restoreToCount(restoreCount);
            restoreCount = canvas.save();
            canvas.clipRect(mBounds);
            drawTrigger(canvas, cx, cy);
        }
        // Keep running until we finish out the last cycle.  
        ViewCompat.postInvalidateOnAnimation(mParent);
    } else {
        // Otherwise if we're in the middle of a trigger, draw that.  
        if (mTriggerPercentage > 0 && mTriggerPercentage <= 1.0) {
            drawTrigger(canvas, cx, cy);
        }
    }
    canvas.restoreToCount(restoreCount);
}

From source file:com.aidy.bottomdrawerlayout.AllDrawerLayout.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();
    int clipTop = 0, clipBottom = getHeight();

    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   w  w w.  j  a  v  a  2 s. c om*/
            switch (getDrawerViewAbsoluteGravity(v)) {
            case Gravity.LEFT:
                Log.i(TAG, "drawChild() -- 1");
                if (checkDrawerViewAbsoluteGravity(v, Gravity.LEFT)) {
                    final int vright = v.getRight();
                    if (vright > clipLeft)
                        clipLeft = vright;
                }
                break;
            case Gravity.RIGHT:
                Log.i(TAG, "drawChild() -- 2");
                if (checkDrawerViewAbsoluteGravity(v, Gravity.RIGHT)) {
                    final int vleft = v.getLeft();
                    if (vleft < clipRight)
                        clipRight = vleft;
                }
                break;
            case Gravity.TOP:
                Log.i(TAG, "drawChild() -- 3");
                if (checkDrawerViewAbsoluteGravity(v, Gravity.TOP)) {
                    final int vbottom = v.getBottom();
                    if (vbottom > clipTop) {
                        clipTop = vbottom;
                    }
                }
                break;
            case Gravity.BOTTOM:
                Log.i(TAG, "drawChild() -- 4");
                if (checkDrawerViewAbsoluteGravity(v, Gravity.BOTTOM)) {
                    final int vtop = v.getTop();
                    if (vtop < clipBottom) {
                        clipBottom = vtop;
                    }
                }
                break;
            default:
                Log.i(TAG, "drawChild() -- 5");
                final int vtop = v.getTop();
                if (vtop < clipBottom) {
                    clipBottom = vtop;
                }
                break;
            }
        }
        canvas.clipRect(clipLeft, clipTop, clipRight, clipBottom);
    }
    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, clipTop, clipRight, clipBottom, mScrimPaint);
    } else if (mShadowLeft != null && checkDrawerViewAbsoluteGravity(child, Gravity.LEFT)) {
        Log.i(TAG, "drawChild() -- 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);
    } else if (mShadowTop != null && checkDrawerViewAbsoluteGravity(child, Gravity.TOP)) {
        Log.i(TAG, "drawChild() -- Gravity.TOP");
        final int shadowHeight = mShadowTop.getIntrinsicHeight();
        final int childBottom = child.getBottom();
        final int drawerPeekDistance = mTopDragger.getEdgeSize();
        final float alpha = Math.max(0, Math.min((float) childBottom / drawerPeekDistance, 1.f));
        mShadowTop.setBounds(child.getLeft(), childBottom, child.getRight(), childBottom + shadowHeight);
        mShadowTop.setAlpha((int) (0xff * alpha));
        mShadowTop.draw(canvas);
    } else if (mShadowBottom != null && checkDrawerViewAbsoluteGravity(child, Gravity.BOTTOM)) {
        Log.i(TAG, "drawChild() -- Gravity.BOTTOM");
        final int shadowHeight = mShadowBottom.getIntrinsicWidth();
        final int childTop = child.getTop();
        final int showing = getHeight() - childTop;
        final int drawerPeekDistance = mBottomDragger.getEdgeSize();
        final float alpha = Math.max(0, Math.min((float) showing / drawerPeekDistance, 1.f));
        mShadowRight.setBounds(child.getLeft(), childTop - shadowHeight, child.getRight(), childTop);
        mShadowRight.setAlpha((int) (0xff * alpha));
        mShadowRight.draw(canvas);
    }
    return result;
}

From source file:com.lovely3x.eavlibrary.EasyAdapterView.java

/**
 * overscroll/*from  w w  w  .  j av a  2 s .  com*/
 *
 * @param canvas
 */
protected void drawOverScrollEffect(Canvas canvas) {
    final int scrollY = getScrollY();
    final int scrollX = getScrollX();

    final int width;
    final int height;
    final int translateX;
    final int translateY;

    width = getWidth();
    height = getHeight();

    translateX = 0;
    translateY = 0;

    if (mTopEdgeEffect != null) {//
        if (!mTopEdgeEffect.isFinished()) {
            final int restoreCount = canvas.save();
            canvas.clipRect(translateX, translateY, translateX + width,
                    translateY + mTopEdgeEffect.getMaxHeight());
            final int edgeY = Math.min(0, scrollY) + translateY;
            canvas.translate(translateX, edgeY);
            mTopEdgeEffect.setSize(width, height);
            if (mTopEdgeEffect.draw(canvas)) {
                invalidateTopGlow();
            }
            canvas.restoreToCount(restoreCount);
        }
    }

    if (mBottomEdgeEffect != null) {//
        if (!mBottomEdgeEffect.isFinished()) {
            final int restoreCount = canvas.save();
            canvas.clipRect(translateX, translateY + height - mBottomEdgeEffect.getMaxHeight(),
                    translateX + width, translateY + height);
            final int edgeX = -width + translateX;
            final int edgeY = Math.max(getHeight(), scrollY);
            canvas.translate(edgeX, edgeY);
            canvas.rotate(180, width, 0);
            mBottomEdgeEffect.setSize(width, height);
            if (mBottomEdgeEffect.draw(canvas)) {
                invalidateBottomGlow();
            }
            canvas.restoreToCount(restoreCount);
        }
    }

    if (mLeftEdgeEffect != null) {//
        if (!mLeftEdgeEffect.isFinished()) {
            canvas.clipRect(translateX, translateY, translateX + mLeftEdgeEffect.getMaxHeight(),
                    translateY + height);
            final int restoreCount = canvas.save();
            canvas.translate(-width, getHeight() - getWidth());//?
            canvas.rotate(270, width, 0);//
            mLeftEdgeEffect.setSize(height, width);//?
            if (mLeftEdgeEffect.draw(canvas)) {
                invalidateLeftGlow();
            }
            canvas.restoreToCount(restoreCount);
        }
    }

    if (mRightEdgeEffect != null) {//?
        if (!mRightEdgeEffect.isFinished()) {
            //     canvas.clipRect(translateX, width - mRightEdgeEffect.getMaxHeight(), width, translateY + height);
            final int restoreCount = canvas.save();
            canvas.translate(0, getWidth());//?
            canvas.rotate(-270, width, 0);//
            mRightEdgeEffect.setSize(height, width);//?
            if (mRightEdgeEffect.draw(canvas)) {
                invalidateRightGlow();
            }
            canvas.restoreToCount(restoreCount);
        }
    }
}

From source file:org.srr.dev.view.refreshlayout.SwipeProgressBar.java

private boolean draw(Canvas canvas, boolean first) {
    Rect bounds = mBounds;/*from   w w w  . jav a  2s.c o m*/
    final int width = bounds.width();
    final int cx = bounds.centerX();
    final int cy = bounds.centerY();
    boolean drawTriggerWhileFinishing = false;
    boolean drawAgain = false;
    int restoreCount = canvas.save();
    canvas.clipRect(bounds);

    if (mRunning || (mFinishTime > 0)) {
        long now = AnimationUtils.currentAnimationTimeMillis();
        long elapsed = (now - mStartTime) % ANIMATION_DURATION_MS;
        long iterations = (now - mStartTime) / ANIMATION_DURATION_MS;
        float rawProgress = (elapsed / (ANIMATION_DURATION_MS / 100f));

        // If we're not running anymore, that means we're running through
        // the finish animation.
        if (!mRunning) {
            // If the finish animation is done, don't draw anything, and
            // don't re-post.
            if ((now - mFinishTime) >= FINISH_ANIMATION_DURATION_MS) {
                mFinishTime = 0;
                return false;
            }

            // Otherwise, use a 0 opacity alpha layer to clear the animation
            // from the inside out. This layer will prevent the circles from
            // drawing within its bounds.
            long finishElapsed = (now - mFinishTime) % FINISH_ANIMATION_DURATION_MS;
            float finishProgress = (finishElapsed / (FINISH_ANIMATION_DURATION_MS / 100f));
            float pct = (finishProgress / 100f);
            // Radius of the circle is half of the screen.
            float clearRadius = width / 2 * INTERPOLATOR.getInterpolation(pct);
            if (SUPPORT_CLIP_RECT_DIFFERENCE) {
                mClipRect.set(cx - clearRadius, bounds.top, cx + clearRadius, bounds.bottom);
                canvas.clipRect(mClipRect, Region.Op.DIFFERENCE);
            } else {
                if (first) {
                    // First time left
                    drawAgain = true;
                    mClipRect.set(bounds.left, bounds.top, cx - clearRadius, bounds.bottom);
                } else {
                    // Second time right
                    mClipRect.set(cx + clearRadius, bounds.top, bounds.right, bounds.bottom);
                }
                canvas.clipRect(mClipRect);
            }
            // Only draw the trigger if there is a space in the center of
            // this refreshing view that needs to be filled in by the
            // trigger. If the progress view is just still animating, let it
            // continue animating.
            drawTriggerWhileFinishing = true;
        }

        // First fill in with the last color that would have finished drawing.
        if (iterations == 0) {
            canvas.drawColor(mColor1);
        } else {
            if (rawProgress >= 0 && rawProgress < 25) {
                canvas.drawColor(mColor4);
            } else if (rawProgress >= 25 && rawProgress < 50) {
                canvas.drawColor(mColor1);
            } else if (rawProgress >= 50 && rawProgress < 75) {
                canvas.drawColor(mColor2);
            } else {
                canvas.drawColor(mColor3);
            }
        }

        // Then draw up to 4 overlapping concentric circles of varying radii, based on how far
        // along we are in the cycle.
        // progress 0-50 draw mColor2
        // progress 25-75 draw mColor3
        // progress 50-100 draw mColor4
        // progress 75 (wrap to 25) draw mColor1
        if ((rawProgress >= 0 && rawProgress <= 25)) {
            float pct = (((rawProgress + 25) * 2) / 100f);
            drawCircle(canvas, cx, cy, mColor1, pct);
        }
        if (rawProgress >= 0 && rawProgress <= 50) {
            float pct = ((rawProgress * 2) / 100f);
            drawCircle(canvas, cx, cy, mColor2, pct);
        }
        if (rawProgress >= 25 && rawProgress <= 75) {
            float pct = (((rawProgress - 25) * 2) / 100f);
            drawCircle(canvas, cx, cy, mColor3, pct);
        }
        if (rawProgress >= 50 && rawProgress <= 100) {
            float pct = (((rawProgress - 50) * 2) / 100f);
            drawCircle(canvas, cx, cy, mColor4, pct);
        }
        if ((rawProgress >= 75 && rawProgress <= 100)) {
            float pct = (((rawProgress - 75) * 2) / 100f);
            drawCircle(canvas, cx, cy, mColor1, pct);
        }
        if (mTriggerPercentage > 0 && drawTriggerWhileFinishing) {
            // There is some portion of trigger to draw. Restore the canvas,
            // then draw the trigger. Otherwise, the trigger does not appear
            // until after the bar has finished animating and appears to
            // just jump in at a larger width than expected.
            canvas.restoreToCount(restoreCount);
            restoreCount = canvas.save();
            canvas.clipRect(bounds);
            drawTrigger(canvas, cx, cy);
        }
        // Keep running until we finish out the last cycle.
        ViewCompat.postInvalidateOnAnimation(mParent, bounds.left, bounds.top, bounds.right, bounds.bottom);
    } else {
        // Otherwise if we're in the middle of a trigger, draw that.
        if (mTriggerPercentage > 0 && mTriggerPercentage <= 1.0) {
            drawTrigger(canvas, cx, cy);
        }
    }
    canvas.restoreToCount(restoreCount);
    return drawAgain;
}

From source file:com.hippo.refreshlayout.SwipeProgressBar.java

private boolean draw(Canvas canvas, boolean first) {
    Rect bounds = mBounds;/*www .  j a v  a  2  s . c  om*/
    final int width = bounds.width();
    final int cx = bounds.centerX();
    final int cy = bounds.centerY();
    final int colors = mColors.length;
    boolean drawTriggerWhileFinishing = false;
    boolean drawAgain = false;
    int restoreCount = canvas.save();
    canvas.clipRect(bounds);

    if (mRunning || (mFinishTime > 0)) {
        long now = AnimationUtils.currentAnimationTimeMillis();
        long elapsed = (now - mStartTime) % mAnimationDuration;
        long iterations = (now - mStartTime) / ANIMATION_DURATION_MS_PER_COLOR;
        float rawProgress = (elapsed / (mAnimationDuration / (float) colors));

        // If we're not running anymore, that means we're running through
        // the finish animation.
        if (!mRunning) {
            // If the finish animation is done, don't draw anything, and
            // don't repost.
            if ((now - mFinishTime) >= FINISH_ANIMATION_DURATION_MS) {
                mFinishTime = 0;
                return false;
            }

            // Otherwise, use a 0 opacity alpha layer to clear the animation
            // from the inside out. This layer will prevent the circles from
            // drawing within its bounds.
            long finishElapsed = (now - mFinishTime) % FINISH_ANIMATION_DURATION_MS;
            float finishProgress = (finishElapsed / (FINISH_ANIMATION_DURATION_MS / 100f));
            float pct = (finishProgress / 100f);
            // Radius of the circle is half of the screen.
            float clearRadius = width / 2 * INTERPOLATOR.getInterpolation(pct);
            if (SUPPORT_CLIP_RECT_DIFFERENCE) {
                mClipRect.set(cx - clearRadius, bounds.top, cx + clearRadius, bounds.bottom);
                canvas.clipRect(mClipRect, Region.Op.DIFFERENCE);
            } else {
                if (first) {
                    // First time left
                    drawAgain = true;
                    mClipRect.set(bounds.left, bounds.top, cx - clearRadius, bounds.bottom);
                } else {
                    // Second time right
                    mClipRect.set(cx + clearRadius, bounds.top, bounds.right, bounds.bottom);
                }
                canvas.clipRect(mClipRect);
            }
            // Only draw the trigger if there is a space in the center of
            // this refreshing view that needs to be filled in by the
            // trigger. If the progress view is just still animating, let it
            // continue animating.
            drawTriggerWhileFinishing = true;
        }

        // First fill in with the last color that would have finished drawing.
        if (iterations == 0) {
            canvas.drawColor(mColors[0]);
        } else {
            int index = colors - 1;
            float left = 0.0f;
            float right = 1.0f;
            for (int i = 0; i < colors; ++i) {
                if ((rawProgress >= left && rawProgress < right) || i == colors - 1) {
                    canvas.drawColor(mColors[index]);
                    break;
                }
                index = (index + 1) % colors;
                left += 1.0f;
                right += 1.0f;
            }
        }

        // Then draw up to 4 overlapping concentric circles of varying radii, based on how far
        // along we are in the cycle.
        // progress 0-50 draw mColor2
        // progress 25-75 draw mColor3
        // progress 50-100 draw mColor4
        // progress 75 (wrap to 25) draw mColor1
        if (colors > 1) {
            if ((rawProgress >= 0.0f && rawProgress <= 1.0f)) {
                float pct = (rawProgress + 1.0f) / 2;
                drawCircle(canvas, cx, cy, mColors[0], pct);
            }
            float left = 0.0f;
            float right = 2.0f;
            for (int i = 1; i < colors; ++i) {
                if (rawProgress >= left && rawProgress <= right) {
                    float pct = (rawProgress - i + 1.0f) / 2;
                    drawCircle(canvas, cx, cy, mColors[i], pct);
                }
                left += 1.0f;
                right += 1.0f;
            }
            if ((rawProgress >= colors - 1.0f && rawProgress <= colors)) {
                float pct = (rawProgress - colors + 1.0f) / 2;
                drawCircle(canvas, cx, cy, mColors[0], pct);
            }
        }
        if (mTriggerPercentage > 0 && drawTriggerWhileFinishing) {
            // There is some portion of trigger to draw. Restore the canvas,
            // then draw the trigger. Otherwise, the trigger does not appear
            // until after the bar has finished animating and appears to
            // just jump in at a larger width than expected.
            canvas.restoreToCount(restoreCount);
            restoreCount = canvas.save();
            canvas.clipRect(bounds);
            drawTrigger(canvas, cx, cy);
        }
        // Keep running until we finish out the last cycle.
        ViewCompat.postInvalidateOnAnimation(mParent, bounds.left, bounds.top, bounds.right, bounds.bottom);
    } else {
        // Otherwise if we're in the middle of a trigger, draw that.
        if (mTriggerPercentage > 0 && mTriggerPercentage <= 1.0) {
            drawTrigger(canvas, cx, cy);
        }
    }
    canvas.restoreToCount(restoreCount);
    return drawAgain;
}

From source file:com.ruesga.timelinechart.TimelineChartView.java

private void drawBarItems(Canvas c, LongSparseArray<Pair<double[], int[]>> data, double maxValue) {

    final float halfItemBarWidth = mBarItemWidth / 2;
    final float height = mGraphArea.height();
    final Paint[] seriesBgPaint;
    final Paint[] highlightSeriesBgPaint;
    synchronized (mLock) {
        seriesBgPaint = mSeriesBgPaint;/*from   w w w. j  a v a  2  s.co m*/
        highlightSeriesBgPaint = mHighlightSeriesBgPaint;
    }

    // Apply zoom animation
    final float zoom = mCurrentZoom;
    final float cx = mGraphArea.left + (mGraphArea.width() / 2);
    int restoreCount = 0;
    if (zoom != 1.f) {
        restoreCount = c.save();
        c.scale(zoom, zoom, cx, mGraphArea.bottom);
    }

    final int size = data.size() - 1;
    for (int i = mItemsOnScreen[1]; i >= mItemsOnScreen[0]; i--) {
        final float x = cx + mCurrentOffset - (mBarWidth * (size - i));
        float bw = mBarItemWidth / mSeries;
        double[] values = data.valueAt(i).first;
        int[] indexes = data.valueAt(i).second;

        float y1, y2 = height;
        float x1 = x - halfItemBarWidth, x2 = x + halfItemBarWidth;
        if (mGraphMode != GRAPH_MODE_BARS_STACK) {
            int count = values.length - 1;
            for (int j = count, n = 0; j >= 0; j--, n++) {
                y2 = height;
                final Paint paint;
                if (mGraphMode == GRAPH_MODE_BARS_SIDE_BY_SIDE) {
                    y1 = (float) (height - ((height * ((values[n] * 100) / maxValue)) / 100));
                    x1 = x - halfItemBarWidth + (bw * n);
                    x2 = x1 + bw;
                    paint = (x - halfItemBarWidth) < cx && (x + halfItemBarWidth) > cx
                            && (mLastTimestamp == mCurrentTimestamp || (mState != STATE_SCROLLING))
                                    ? highlightSeriesBgPaint[indexes[n]]
                                    : seriesBgPaint[indexes[n]];
                } else {
                    y1 = (float) (height - ((height * ((values[j] * 100) / maxValue)) / 100));
                    paint = x1 < cx && x2 > cx
                            && (mLastTimestamp == mCurrentTimestamp || (mState != STATE_SCROLLING))
                                    ? highlightSeriesBgPaint[indexes[j]]
                                    : seriesBgPaint[indexes[j]];
                }

                c.drawRect(x1, mGraphArea.top + y1, x2, mGraphArea.top + y2, paint);
            }
        } else {
            int count = values.length;
            for (int j = 0; j < count; j++) {
                float h = (float) ((height * ((values[j] * 100) / maxValue)) / 100);
                y1 = y2 - h;

                final Paint paint = x1 < cx && x2 > cx
                        && (mLastTimestamp == mCurrentTimestamp || (mState != STATE_SCROLLING))
                                ? highlightSeriesBgPaint[indexes[j]]
                                : seriesBgPaint[indexes[j]];
                c.drawRect(x1, mGraphArea.top + y1, x2, mGraphArea.top + y2, paint);
                y2 -= h;
            }
        }
    }

    // Restore from zoom
    if (zoom != 1.f) {
        c.restoreToCount(restoreCount);
    }
}

From source file:com.example.libwidgettv.bak.AbsListView.java

@Override
public void draw(Canvas canvas) {
    super.draw(canvas);
    if (mFastScroller != null) {
        final int scrollY = getScrollY();
        if (scrollY != 0) {
            // Pin to the top/bottom during overscroll
            int restoreCount = canvas.save();
            canvas.translate(0, (float) scrollY);
            mFastScroller.draw(canvas);//from  ww  w .j a  v  a  2s . co m
            canvas.restoreToCount(restoreCount);
        } else {
            mFastScroller.draw(canvas);
        }
    }
}

From source file:android.support.v7.widget.RecyclerView.java

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

    final int count = mItemDecorations.size();
    for (int i = 0; i < count; i++) {
        mItemDecorations.get(i).onDrawOver(c, this);
    }//w w  w . j  av a  2s  . com

    boolean needsInvalidate = false;
    if (mLeftGlow != null && !mLeftGlow.isFinished()) {
        final int restore = c.save();
        c.rotate(270);
        c.translate(-getHeight() + getPaddingTop(), 0);
        needsInvalidate = mLeftGlow != null && mLeftGlow.draw(c);
        c.restoreToCount(restore);
    }
    if (mTopGlow != null && !mTopGlow.isFinished()) {
        c.translate(getPaddingLeft(), getPaddingTop());
        needsInvalidate |= mTopGlow != null && mTopGlow.draw(c);
    }
    if (mRightGlow != null && !mRightGlow.isFinished()) {
        final int restore = c.save();
        final int width = getWidth();

        c.rotate(90);
        c.translate(-getPaddingTop(), -width);
        needsInvalidate |= mRightGlow != null && mRightGlow.draw(c);
        c.restoreToCount(restore);
    }
    if (mBottomGlow != null && !mBottomGlow.isFinished()) {
        final int restore = c.save();
        c.rotate(180);
        c.translate(-getWidth() + getPaddingLeft(), -getHeight() + getPaddingTop());
        needsInvalidate |= mBottomGlow != null && mBottomGlow.draw(c);
        c.restoreToCount(restore);
    }

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

From source file:com.aliasapps.seq.scroller.TwoWayView.java

private boolean drawStartEdge(Canvas canvas) {
    if (mStartEdge.isFinished()) {
        return false;
    }/*from  www .j ava2  s.co  m*/

    if (mIsVertical) {
        return mStartEdge.draw(canvas);
    }

    final int restoreCount = canvas.save();
    final int height = getHeight() - getPaddingTop() - getPaddingBottom();

    canvas.translate(0, height);
    canvas.rotate(270);

    final boolean needsInvalidate = mStartEdge.draw(canvas);
    canvas.restoreToCount(restoreCount);
    return needsInvalidate;
}