List of usage examples for android.graphics Canvas restore
public void restore()
From source file:com.android.andryyu.lifehelper.widget.RippleView.java
private void doRippleWork(Canvas canvas, int offect) { canvas.save();/*from w ww .j a va 2s .c om*/ if (rippleDuration <= timer * frameRate) { // There is problem on Android M where canvas.restore() seems to be called automatically // For now, don't call canvas.restore() manually on Android M (API 23) canvas.drawCircle(x, y, (radiusMax * (((float) timer * frameRate) / rippleDuration)), paint); if (onCompletionListener != null && rippleStatus != RIPPLE_ACTION_MOVE && rippleStatus != RIPPLE_LONG_PRESS) { onCompletionListener.onComplete(this); } if (rippleStatus != RIPPLE_LONG_PRESS) { animationRunning = false; rippleStatus = RIPPLE_NORMAL; timer = 0; durationEmpty = -1; timerEmpty = 0; if (Build.VERSION.SDK_INT != 23) { canvas.restore(); } } invalidate(); return; } else canvasHandler.postDelayed(runnable, frameRate); if (timer == 0) canvas.save(); canvas.drawCircle(x, y, (radiusMax * (((float) timer * frameRate) / rippleDuration)), paint); paint.setColor(Color.parseColor("#ffff4444")); if (rippleType == 1 && originBitmap != null && (((float) timer * frameRate) / rippleDuration) > 0.4f) { if (durationEmpty == -1) durationEmpty = rippleDuration - timer * frameRate; timerEmpty++; final Bitmap tmpBitmap = getCircleBitmap( (int) ((radiusMax) * (((float) timerEmpty * frameRate) / (durationEmpty)))); canvas.drawBitmap(tmpBitmap, 0, 0, paint); tmpBitmap.recycle(); } paint.setColor(rippleColor); if (!isListMode) { if (rippleType == 1) { if ((((float) timer * frameRate) / rippleDuration) > 0.6f) paint.setAlpha((int) (rippleAlpha - ((rippleAlpha) * (((float) timerEmpty * frameRate) / (durationEmpty))))); else paint.setAlpha(rippleAlpha); } else paint.setAlpha( (int) (rippleAlpha - ((rippleAlpha) * (((float) timer * frameRate) / rippleDuration)))); } timer += offect; }
From source file:android.widget.PinnedHeaderListView.java
@Override protected void dispatchDraw(final Canvas canvas) { final long currentTime = mAnimating ? System.currentTimeMillis() : 0; int top = 0;/* w w w .ja v a 2 s .c o m*/ final int right = 0; int bottom = getBottom(); boolean hasVisibleHeaders = false; for (int i = 0; i < mSize; i++) { final PinnedHeader header = mHeaders[i]; if (header.visible) { hasVisibleHeaders = true; if (header.state == BOTTOM && header.y < bottom) bottom = header.y; else if (header.state == TOP || header.state == FADING) { final int newTop = header.y + header.height; if (newTop > top) top = newTop; } } } if (hasVisibleHeaders) canvas.save(); super.dispatchDraw(canvas); if (hasVisibleHeaders) { canvas.restore(); // If the first item is visible and if it has a positive top that is greater than the // first header's assigned y-value, use that for the first header's y value. This way, // the header inherits any padding applied to the list view. if (mSize > 0 && getFirstVisiblePosition() == 0) { final View firstChild = getChildAt(0); final PinnedHeader firstHeader = mHeaders[0]; if (firstHeader != null) { final int firstHeaderTop = firstChild != null ? firstChild.getTop() : 0; firstHeader.y = Math.max(firstHeader.y, firstHeaderTop); } } // First draw top headers, then the bottom ones to handle the Z axis correctly for (int i = mSize; --i >= 0;) { final PinnedHeader header = mHeaders[i]; if (header.visible && (header.state == TOP || header.state == FADING)) drawHeader(canvas, header, currentTime); } for (int i = 0; i < mSize; i++) { final PinnedHeader header = mHeaders[i]; if (header.visible && header.state == BOTTOM) drawHeader(canvas, header, currentTime); } } invalidateIfAnimating(); }
From source file:com.example.view.wheel.WheelView.java
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (itemsLayout == null) { if (itemsWidth == 0) { calculateLayoutWidth(getWidth(), MeasureSpec.EXACTLY); } else {/* w w w . j a v a2 s . com*/ createLayouts(itemsWidth, labelWidth); } } if (itemsWidth > 0) { canvas.save(); // Skip padding space and hide a part of top and bottom items canvas.translate(PADDING, -ITEM_OFFSET); drawItems(canvas); drawValue(canvas); canvas.restore(); } drawCenterRect(canvas); // drawShadows(canvas); }
From source file:am.widget.tabstrip.HorizontalLinearTabStripLayout.java
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); final int count = getChildCount(); if (mCenter == null && !isShowingDividers() && count <= 0) return;//from w w w. j av a2s. c o m final int paddingStart = ViewCompat.getPaddingStart(this); final int paddingTop = getPaddingTop(); final int paddingBottom = getPaddingBottom(); final int childWidth = mChildWidth; final boolean show = isShowingDividers(); final Drawable dd = mDivider; final int divider = show ? dd.getIntrinsicWidth() : 0; final int padding = mDividerPadding; int start = paddingStart; if (show) dd.setBounds(1, paddingTop + padding, divider + 1, getHeight() - paddingBottom - padding); if (count == 1) { if (show && (mShowDividers & SHOW_DIVIDER_BEGINNING) == SHOW_DIVIDER_BEGINNING) { canvas.save(); canvas.translate(start, 0); dd.draw(canvas); canvas.restore(); start += divider; } start += childWidth; if (show && (mShowDividers & SHOW_DIVIDER_END) == SHOW_DIVIDER_END) { canvas.save(); canvas.translate(start, 0); dd.draw(canvas); canvas.restore(); } } else { final boolean middle = (mShowDividers & SHOW_DIVIDER_MIDDLE) == SHOW_DIVIDER_MIDDLE; for (int i = 0; i < count; i++) { if (i == 0) { if (show && (mShowDividers & SHOW_DIVIDER_BEGINNING) == SHOW_DIVIDER_BEGINNING) { canvas.save(); canvas.translate(start, 0); dd.draw(canvas); canvas.restore(); start += divider; } start += childWidth; } else if (i == count - 1) { if (show && middle) { canvas.save(); canvas.translate(start, 0); dd.draw(canvas); canvas.restore(); start += divider; } start += childWidth; if (show && (mShowDividers & SHOW_DIVIDER_END) == SHOW_DIVIDER_END) { start = getWidth() - ViewCompat.getPaddingEnd(this) - divider; canvas.save(); canvas.translate(start, 0); dd.draw(canvas); canvas.restore(); break; } } else { if (count % 2 == 0 && count / 2 == i && mCenter != null) { if (show && middle && mCenterAsItem) { canvas.save(); canvas.translate(start, 0); dd.draw(canvas); canvas.restore(); start += divider; } final Drawable center = mCenter; final int p = mCenterPadding; center.setBounds(0, paddingTop + p, center.getIntrinsicWidth(), getHeight() - paddingBottom - p); canvas.save(); canvas.translate(start, 0); center.draw(canvas); canvas.restore(); start += center.getIntrinsicWidth(); if (show && middle && mCenterAsItem) { canvas.save(); canvas.translate(start, 0); dd.draw(canvas); canvas.restore(); start += divider; } } else { if (show && middle) { canvas.save(); canvas.translate(start, 0); dd.draw(canvas); canvas.restore(); start += divider; } } start += childWidth; } } } }
From source file:com.example.nitish.welcomapp.widgetpt.PeriodicTableView.java
/** * Draw the edge effects to the supplied Canvas. * * @param canvas The Canvas/*from w w w .j a v a 2s .c om*/ */ private void drawEdgeEffects(@NonNull Canvas canvas) { boolean invalidate = false; if (!mEdgeEffectTop.isFinished()) { mEdgeEffectTop.draw(canvas); invalidate = true; } if (!mEdgeEffectBottom.isFinished()) { canvas.save(); canvas.rotate(180, getWidth() / 2, getHeight() / 2); mEdgeEffectBottom.draw(canvas); canvas.restore(); invalidate = true; } if (!mEdgeEffectLeft.isFinished()) { canvas.save(); canvas.translate(0, getHeight()); canvas.rotate(-90); mEdgeEffectLeft.draw(canvas); canvas.restore(); invalidate = true; } if (!mEdgeEffectRight.isFinished()) { canvas.save(); canvas.translate(getWidth(), 0); canvas.rotate(90, 0, 0); mEdgeEffectRight.draw(canvas); canvas.restore(); invalidate = true; } if (invalidate) { ViewCompat.postInvalidateOnAnimation(this); } }
From source file:com.scvngr.levelup.core.ui.view.LevelUpCodeView.java
/** * Draws the QR code to the canvas, scaling it up to fit the measured size of the view. If * enabled, this also draws the colored target areas per * {@link #drawColoredTargetAreas(Canvas, LevelUpQrCodeImage)}. * * @param canvas the drawing canvas./* w ww.j av a2 s .com*/ * @param levelUpQrCodeImage the image of the QR code with target marker information. */ private void drawQrCode(@NonNull final Canvas canvas, @NonNull final LevelUpQrCodeImage levelUpQrCodeImage) { final Bitmap codeBitmap = levelUpQrCodeImage.getBitmap(); /* * The code is cached in the smallest size and must be scaled before being displayed. It is * necessary to draw it directly onto a canvas and scale it in the same operation for * efficiency (so we do not have any perceivable lag when switching tip values). */ mCodeScalingMatrix.setScale((float) getMeasuredWidth() / codeBitmap.getWidth(), (float) getMeasuredHeight() / codeBitmap.getHeight()); // Save the canvas without the scaling matrix. canvas.save(); canvas.concat(mCodeScalingMatrix); canvas.drawBitmap(codeBitmap, 0, 0, mQrCodePaint); if (mIsColorizeSet) { drawColoredTargetAreas(canvas, levelUpQrCodeImage); } canvas.restore(); }
From source file:com.dongdong.wheel.WheelView.java
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (mItemsLayout == null) { if (mItemsWidth == 0) { calculateLayoutWidth(getWidth(), MeasureSpec.EXACTLY); } else {// w w w . j ava2 s . c om createLayouts(mItemsWidth, mLabelWidth); } } if (mItemsWidth > 0) { canvas.save(); // Skip padding space and hide a part of top and bottom items canvas.translate(PADDING, -ITEM_OFFSET); drawItems(canvas); drawValue(canvas); canvas.restore(); } drawCenterRect(canvas); drawShadows(canvas); }
From source file:com.mylikes.likes.etchasketch.Slate.java
private void drawStrokeDebugInfo(Canvas c) { final int ROW_HEIGHT = 24; final int ROW_MARGIN = 6; final int COLUMN_WIDTH = 55; final float FIRM_PRESSURE_LOW = 0.85f; final float FIRM_PRESSURE_HIGH = 1.25f; if (mStrokeDebugGraph == null) { final int width = c.getWidth() - 128; final int height = ROW_HEIGHT * mStrokes.length + 2 * ROW_MARGIN; mStrokeDebugGraph = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); if (mStrokeDebugGraph == null) { throw new RuntimeException( "drawStrokeDebugInfo: couldn't create debug bitmap (" + width + "x" + height + ")"); }/*w w w . j av a2 s .c om*/ mGraphPaint1 = new Paint(Paint.ANTI_ALIAS_FLAG); } Canvas graph = new Canvas(mStrokeDebugGraph); graph.save(); graph.clipRect(new Rect(0, 0, COLUMN_WIDTH, graph.getHeight())); graph.drawColor(0, PorterDuff.Mode.CLEAR); graph.restore(); int left = 4; int bottom = graph.getHeight() - ROW_MARGIN; final int STEP = 4; for (MarkersPlotter st : mStrokes) { float r = st.getLastPressure(); if (r >= FIRM_PRESSURE_LOW && r <= FIRM_PRESSURE_HIGH) mGraphPaint1.setColor(0xFF33FF33); else if (r < FIRM_PRESSURE_LOW) mGraphPaint1.setColor(0xFF808080); else mGraphPaint1.setColor(0xFFFF8000); String s = (r < 0) ? "--" : String.format("%s %.4f", ((st.getLastTool() == MotionEvent.TOOL_TYPE_STYLUS) ? "S" : "F"), r); graph.drawText(s, left, bottom - 2, mGraphPaint1); if (mGraphX + COLUMN_WIDTH > graph.getWidth()) { mGraphX = 0; graph.save(); graph.clipRect(new Rect(30, 0, graph.getWidth(), graph.getHeight())); graph.drawColor(0, PorterDuff.Mode.CLEAR); graph.restore(); } if (r >= 0) { int barsize = (int) (r * ROW_HEIGHT); graph.drawRect(mGraphX + COLUMN_WIDTH, bottom - barsize, mGraphX + COLUMN_WIDTH + STEP, bottom, mGraphPaint1); } else { graph.drawPoint(mGraphX + COLUMN_WIDTH + STEP, bottom, mGraphPaint1); } bottom -= (ROW_HEIGHT + ROW_MARGIN); } mGraphX += STEP; final int x = 96; final int y = 64; c.drawBitmap(mStrokeDebugGraph, x, y, null); invalidate(new Rect(x, y, x + c.getWidth(), y + c.getHeight())); }
From source file:com.apptentive.android.sdk.module.messagecenter.view.MessageCenterListView.java
@Override protected void dispatchDraw(Canvas canvas) { super.dispatchDraw(canvas); if (stickyWrapper != null) { int pLeft = getListPaddingLeft(); int pTop = getListPaddingTop(); View view = stickyWrapper.view; int headerTop = view.getTop(); pLeft += stickyWrapper.additionalIndent; // draw child canvas.save();/*from ww w. j a v a 2 s .c o m*/ int clipHeight = view.getHeight() + (shadowDrawable == null ? 0 : shadowHeight); canvas.clipRect(pLeft, pTop, pLeft + view.getWidth() - 2 * stickyWrapper.additionalIndent, pTop + clipHeight); canvas.translate(pLeft - stickyWrapper.additionalIndent, pTop - headerTop); drawChild(canvas, stickyWrapper.view, getDrawingTime()); if (shadowDrawable != null) { shadowDrawable.setBounds(stickyWrapper.view.getLeft(), stickyWrapper.view.getBottom(), stickyWrapper.view.getRight(), stickyWrapper.view.getBottom() + shadowHeight); shadowDrawable.draw(canvas); } canvas.restore(); } }
From source file:net.yanzm.actionbarprogress.MaterialIndeterminateProgressDrawable.java
@Override public void draw(Canvas canvas) { final int level = getLevel(); final float input = level / 10000f; canvas.drawColor(trackColor);//from w ww.j ava2 s. c o m canvas.save(); canvas.translate(canvas.getWidth() / 2f, 0); canvas.scale(canvas.getWidth() / 360f, 1); paint.setColor(accentColor); { final int saveCount = canvas.save(); float translateX2 = translateInterpolator2.getInterpolation(input); canvas.translate(-197.60001f + translateX2 * 620.20002f, 0); float x = scaleInterpolator2.getInterpolation(input); float scaleX = 1.6199005127f * -Math.abs(x - 0.5f) + 0.909950256348f; rect2.set(-144f * scaleX, 0, 144f * scaleX, canvas.getHeight()); canvas.drawRect(rect2, paint); canvas.restoreToCount(saveCount); } { final int saveCount = canvas.save(); float translateX1 = translateInterpolator1.getInterpolation(input); canvas.translate(-522.59998f + translateX1 * 722.19999f, 0); float x = scaleInterpolator1.getInterpolation(input); float scaleX = 1.45369842529f * -Math.abs(x - 0.5f) + 0.826849212646f; rect1.set(-144f * scaleX, 0, 144f * scaleX, canvas.getHeight()); canvas.drawRect(rect1, paint); canvas.restoreToCount(saveCount); } canvas.restore(); }