Java Code Examples for android.graphics.Canvas#restore()

The following examples show how to use android.graphics.Canvas#restore() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CircleProgress.java    From SprintNBA with Apache License 2.0 6 votes vote down vote up
@Override protected void onDraw(Canvas canvas) {
        float yHeight = getProgress() / (float) getMax() * getHeight();
        float radius = getWidth() / 2f;
        float angle = (float) (Math.acos((radius - yHeight) / radius) * 180 / Math.PI);
        float startAngle = 90 + angle;
        float sweepAngle = 360 - angle * 2;
        paint.setColor(getUnfinishedColor());
        canvas.drawArc(rectF, startAngle, sweepAngle, false, paint);

        canvas.save();
        canvas.rotate(180, getWidth() / 2, getHeight() / 2);
        paint.setColor(getFinishedColor());
        canvas.drawArc(rectF, 270 - angle, angle * 2, false, paint);
        canvas.restore();

        // Also works.
//        paint.setColor(getFinishedColor());
//        canvas.drawArc(rectF, 90 - angle, angle * 2, false, paint);

        String text = getDrawText();
        if (!TextUtils.isEmpty(text)) {
            float textHeight = textPaint.descent() + textPaint.ascent();
            canvas.drawText(text, (getWidth() - textPaint.measureText(text)) / 2.0f, (getWidth() - textHeight) / 2.0f, textPaint);
        }
    }
 
Example 2
Source File: GeoGebraLogoBox.java    From AndroidMathKeyboard with Apache License 2.0 6 votes vote down vote up
public void draw(Canvas g2, float x, float y) {
	g2.save();
	Paint st = AjLatexMath.getPaint();
	int c = st.getColor();
	Style s = st.getStyle();
	float w = st.getStrokeWidth();

	g2.translate(x + 0.25f * height / 2.15f, y - 1.75f / 2.15f * height);
	st.setColor(gray);
	st.setStrokeWidth(3.79999995f);
	g2.scale(0.05f * height / 2.15f, 0.05f * height / 2.15f);
	g2.rotate((float) Math.toDegrees((-26 * Math.PI / 180)), 20.5f, 17.5f);
	g2.drawArc(new RectF(0f, 0f, 43f, 32f), 0f, 360f, false, st);
	g2.rotate((float) Math.toDegrees((26 * Math.PI / 180)), 20.5f, 17.5f);
	st.setStyle(Style.STROKE);
	drawCircle(st, g2, 16f, -5f);
	drawCircle(st, g2, -1f, 7f);
	drawCircle(st, g2, 5f, 28f);
	drawCircle(st, g2, 27f, 24f);
	drawCircle(st, g2, 36f, 3f);

	st.setColor(c);
	st.setStyle(s);
	st.setStrokeWidth(w);
	g2.restore();
}
 
Example 3
Source File: ArcSeekBar.java    From arc-seekbar with Apache License 2.0 6 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {
    canvas.save();
    canvas.rotate(mRotateAngle, mCenterX, mCenterY);
    mShadowPaint.setShadowLayer(mShadowRadius * 2, 0, 0, getColor());
    canvas.drawPath(mBorderPath, mShadowPaint);
    canvas.drawPath(mSeekPath, mArcPaint);
    if (mBorderWidth > 0) {
        canvas.drawPath(mBorderPath, mBorderPaint);
    }
    if (mThumbShadowRadius > 0) {
        mThumbPaint.setShadowLayer(mThumbShadowRadius, 0, 0, mThumbShadowColor);
        canvas.drawCircle(mThumbX, mThumbY, mThumbRadius, mThumbPaint);
        mThumbPaint.clearShadowLayer();
    }
    canvas.drawCircle(mThumbX, mThumbY, mThumbRadius, mThumbPaint);
    canvas.restore();
}
 
Example 4
Source File: WindowImageView.java    From WindowImageView with MIT License 6 votes vote down vote up
@Override
public void draw(Canvas canvas) {
    super.draw(canvas);

    Drawable drawable = mDrawableController.getTargetDrawable();
    if (drawable != null) {
        if (drawable instanceof BitmapDrawable) {
            BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
            if (bitmapDrawable.getBitmap().isRecycled()) {
                return;
            }
        }
        canvas.save();
        canvas.translate(0, disPlayTop);
        drawable.setBounds(0, 0, getWidth(), rescaleHeight);
        drawable.draw(canvas);
        canvas.restore();
    }
}
 
Example 5
Source File: CenteredImageSpan.java    From arcusandroid with Apache License 2.0 6 votes vote down vote up
@Override
public void draw(@NonNull Canvas canvas, CharSequence text,
                 int start, int end, float x,
                 int top, int y, int bottom, @NonNull Paint paint) {
    Drawable b = getCachedDrawable();
    canvas.save();

    int drawableHeight = b.getIntrinsicHeight();
    int fontAscent = paint.getFontMetricsInt().ascent;
    int fontDescent = paint.getFontMetricsInt().descent;
    int transY = bottom - b.getBounds().bottom +  // align bottom to bottom
            (drawableHeight - fontDescent + fontAscent) / 2;  // align center to center

    canvas.translate(x, transY);
    b.draw(canvas);
    canvas.restore();
}
 
Example 6
Source File: ShiftDrawable.java    From focus-android with Mozilla Public License 2.0 6 votes vote down vote up
@Override
public void draw(Canvas canvas) {
    final Drawable d = getWrappedDrawable();
    final float fraction = mAnimator.getAnimatedFraction();
    final int width = mVisibleRect.width();
    final int offset = (int) (width * fraction);
    final int stack = canvas.save();

    canvas.clipPath(mPath);

    // shift from right to left.
    // draw left-half part
    canvas.save();
    canvas.translate(-offset, 0);
    d.draw(canvas);
    canvas.restore();

    // draw right-half part
    canvas.save();
    canvas.translate(width - offset, 0);
    d.draw(canvas);
    canvas.restore();

    canvas.restoreToCount(stack);
}
 
Example 7
Source File: VerticalImageSpan.java    From browser with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void draw(Canvas canvas, CharSequence text, int start, int end,
                 float x, int top, int y, int bottom, Paint paint) {

    Drawable drawable = getDrawable();
    canvas.save();

    int length = 0;
    int dy = bottom - drawable.getBounds().bottom + mPadding;
    if(mVerticalAlignment == ALIGN_BASELINE) {
        length = text.length();
    }

    for(int i = 0 ; i < length ; i++) {
        if (!(Character.isLetterOrDigit(text.charAt(i)))) {
            continue;
        }
        dy -= paint.getFontMetricsInt().descent;
        canvas.translate(x, dy);
        drawable.draw(canvas);
        canvas.restore();
        return ;
    }
}
 
Example 8
Source File: YAxisRender.java    From LChart with Apache License 2.0 5 votes vote down vote up
@Override
public void renderGridline(Canvas canvas) {
    super.renderGridline(canvas);

    canvas.save();
    canvas.clipRect(_rectMain);// 限制绘制区域

    double[] values = _Axis.getLabelValues();
    int labelCount = _Axis.getLabelCount();

    float y = 0;

    float left = _rectMain.left;
    float right = _rectMain.right;

    _PathGrid.reset();

    for (int i = 0; i < labelCount; i++) {
        if (values.length < (i + 1)) {
            break;
        }
        double value = values[i];

        SingleF_XY xy = _MappingManager.getPxByValue(0, value);
        y = xy.getY();

        _PathGrid.moveTo(left, y);
        _PathGrid.lineTo(right, y);
    }

    // grid line
    canvas.drawPath(_PathGrid, _PaintGridline);

    canvas.restore();
}
 
Example 9
Source File: BossTriangleBullet.java    From StormPlane with Apache License 2.0 5 votes vote down vote up
@Override
public void drawSelf(Canvas canvas) {
	if (isAlive) {
		canvas.save();
		canvas.clipRect(object_x, object_y, object_x + object_width,
				object_y + object_height);
		canvas.drawBitmap(bullet, object_x, object_y, paint);
		canvas.restore();
		logic();
	}
}
 
Example 10
Source File: AdImageView.java    From AndroidAnimationExercise with Apache License 2.0 5 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {

    Drawable drawable = getDrawable();
    int w = getWidth();
    int h = (int) (getWidth() * 1.0f / drawable.getIntrinsicWidth() * drawable.getIntrinsicHeight());
    drawable.setBounds(0, 0, w, h);
    canvas.save();
    canvas.translate(0, -mDx);
    super.onDraw(canvas);
    canvas.restore();
}
 
Example 11
Source File: DrawClickableEditText.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
private void drawableClearable(Canvas canvas) {
    int vspace = getBottom() - getTop() - getCompoundPaddingBottom() - getCompoundPaddingTop();
    int rightOffset = getCompoundDrawablePadding();
    Drawable[] compoundDrawables = getCompoundDrawables();
    if(compoundDrawables[2] != null) {
        rightOffset += compoundDrawables[2].getIntrinsicWidth() + getCompoundDrawablePadding();
    }

    canvas.save();
    canvas.translate(getScrollX() + getRight() - getLeft()
                    - clearDrawable.getIntrinsicWidth() * 1.1f - rightOffset,
            getScrollY() + getCompoundPaddingTop() + (vspace - clearDrawable.getIntrinsicHeight()) / 2);
    clearDrawable.draw(canvas);
    canvas.restore();
}
 
Example 12
Source File: LeftDrawer.java    From Roid-Library with Apache License 2.0 5 votes vote down vote up
@Override
protected void drawIndicator(Canvas canvas, int offsetPixels) {
    if (mActiveView != null && isViewDescendant(mActiveView)) {
        Integer position = (Integer) mActiveView.getTag(R.id.mdActiveViewPosition);
        final int pos = position == null ? 0 : position;

        if (pos == mActivePosition) {
            final float openRatio = ((float) offsetPixels) / mMenuSize;

            mActiveView.getDrawingRect(mActiveRect);
            offsetDescendantRectToMyCoords(mActiveView, mActiveRect);

            final float interpolatedRatio = 1.f - INDICATOR_INTERPOLATOR.getInterpolation((1.f - openRatio));
            final int interpolatedWidth = (int) (mActiveIndicator.getWidth() * interpolatedRatio);

            if (mIndicatorAnimating) {
                final int indicatorFinalTop = mActiveRect.top
                        + ((mActiveRect.height() - mActiveIndicator.getHeight()) / 2);
                final int indicatorStartTop = mIndicatorStartPos;
                final int diff = indicatorFinalTop - indicatorStartTop;
                final int startOffset = (int) (diff * mIndicatorOffset);
                mIndicatorTop = indicatorStartTop + startOffset;
            } else {
                mIndicatorTop = mActiveRect.top + ((mActiveRect.height() - mActiveIndicator.getHeight()) / 2);
            }
            final int right = offsetPixels;
            final int left = right - interpolatedWidth;

            canvas.save();
            canvas.clipRect(left, 0, right, getHeight());
            canvas.drawBitmap(mActiveIndicator, left, mIndicatorTop, null);
            canvas.restore();
        }
    }
}
 
Example 13
Source File: SeekBar.java    From MusicPlayer with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 绘制按钮和提示背景和文字
 * Draw buttons and tips for background and text
 *
 * @param canvas Canvas
 */
protected void draw(Canvas canvas) {
    int offset = (int) (lineWidth * currPercent);
    canvas.save();
    canvas.translate(offset, 0);
    SeekBarState[] states = rangeSeekBar.getRangeSeekBarState();
    String text2Draw = userText2Draw;
    if (isLeft) {
        if (userText2Draw == null) {
            if (indicatorTextDecimalFormat != null){
                text2Draw = indicatorTextDecimalFormat.format(states[0].value);
            }else {
                text2Draw = states[0].indicatorText;
            }
        }
    } else {
        if (userText2Draw == null) {
            if (indicatorTextDecimalFormat != null){
                text2Draw = indicatorTextDecimalFormat.format(states[1].value);
            }else {
                text2Draw = states[1].indicatorText;
            }
        }
    }
    if (indicatorTextStringFormat != null){
        text2Draw = String.format(indicatorTextStringFormat, text2Draw);
    }
    paint.setTextSize(indicatorTextSize);
    paint.getTextBounds(text2Draw, 0, text2Draw.length(), indicatorTextRect);
    // translate canvas, then don't care left
    canvas.translate(left, 0);
    if (indicatorShowMode == INDICATOR_MODE_ALWAYS_SHOW) {
        setShowIndicatorEnable(true);
    }
    if (isShowIndicator) {
        drawIndicator(canvas, text2Draw);
    }
    drawThumb(canvas);
    canvas.restore();
}
 
Example 14
Source File: BossGThunderBullet.java    From StormPlane with Apache License 2.0 5 votes vote down vote up
@Override
public void drawSelf(Canvas canvas) {
	if (isAlive) {
		canvas.save();
		canvas.clipRect(object_x, object_y, object_x + object_width,
				object_y + object_height);
		canvas.drawBitmap(bullet, object_x, object_y, paint);
		canvas.restore();
		logic();
	}
}
 
Example 15
Source File: CompositionLayer.java    From lottie-android with Apache License 2.0 5 votes vote down vote up
@Override void drawLayer(Canvas canvas, Matrix parentMatrix, int parentAlpha) {
  L.beginSection("CompositionLayer#draw");
  newClipRect.set(0, 0, layerModel.getPreCompWidth(), layerModel.getPreCompHeight());
  parentMatrix.mapRect(newClipRect);

  // Apply off-screen rendering only when needed in order to improve rendering performance.
  boolean isDrawingWithOffScreen = lottieDrawable.isApplyingOpacityToLayersEnabled() && layers.size() > 1 && parentAlpha != 255;
  if (isDrawingWithOffScreen) {
    layerPaint.setAlpha(parentAlpha);
    Utils.saveLayerCompat(canvas, newClipRect, layerPaint);
  } else {
    canvas.save();
  }

  int childAlpha = isDrawingWithOffScreen ? 255 : parentAlpha;
  for (int i = layers.size() - 1; i >= 0; i--) {
    boolean nonEmptyClip = true;
    if (!newClipRect.isEmpty()) {
      nonEmptyClip = canvas.clipRect(newClipRect);
    }
    if (nonEmptyClip) {
      BaseLayer layer = layers.get(i);
      layer.draw(canvas, parentMatrix, childAlpha);
    }
  }
  canvas.restore();
  L.endSection("CompositionLayer#draw");
}
 
Example 16
Source File: Pulse.java    From mkloader with Apache License 2.0 5 votes vote down vote up
@Override public void draw(Canvas canvas) {
  for (int i = 0; i < numberOfLines; i++) {
    canvas.save();
    canvas.translate(i * (lineWidth + lineDistance), 0);
    canvas.scale(1, scaleY[i], lines[i].getPoint1().x, center.y);
    lines[i].draw(canvas);
    canvas.restore();
  }
}
 
Example 17
Source File: ParallaxScrimageView.java    From materialup with Apache License 2.0 5 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {
    if (imageOffset != 0) {
        canvas.save();
        canvas.translate(0f, imageOffset);
        canvas.clipRect(0f, 0f, canvas.getWidth(), canvas.getHeight() + imageOffset);
        super.onDraw(canvas);
        canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), scrimPaint);
        canvas.restore();
    } else {
        super.onDraw(canvas);
        canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), scrimPaint);
    }
}
 
Example 18
Source File: CustomViewAccessibilityActivity.java    From codeexamples-android with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.save();
    canvas.translate(getPaddingLeft(), getPaddingRight());
    Layout switchText = mChecked ? mOnLayout : mOffLayout;
    switchText.draw(canvas);
    canvas.restore();
}
 
Example 19
Source File: MatchView.java    From UltimateAndroid with Apache License 2.0 4 votes vote down vote up
@Override
public void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    float progress = mProgress;
    int c1 = canvas.save();
    int len = mItemList.size();
    for (int i = 0; i < mItemList.size(); i++) {
        canvas.save();
        MatchItem LoadingViewItem = mItemList.get(i);
        float offsetX = mOffsetX + LoadingViewItem.midPoint.x;
        float offsetY = mOffsetY + LoadingViewItem.midPoint.y;

        if (mIsInLoading) {
            LoadingViewItem.getTransformation(getDrawingTime(), mTransformation);
            canvas.translate(offsetX, offsetY);
        } else {

            if (progress == 0) {
                LoadingViewItem.resetPosition(horizontalRandomness);
                continue;
            }

            float startPadding = (1 - internalAnimationFactor) * i / len;
            float endPadding = 1 - internalAnimationFactor - startPadding;

            // done
            if (progress == 1 || progress >= 1 - endPadding) {
                canvas.translate(offsetX, offsetY);
                LoadingViewItem.setAlpha(mBarDarkAlpha);
            } else {
                float realProgress;
                if (progress <= startPadding) {
                    realProgress = 0;
                } else {
                    realProgress = Math.min(1, (progress - startPadding) / internalAnimationFactor);
                }
                offsetX += LoadingViewItem.translationX * (1 - realProgress);
                offsetY += -mDropHeight * (1 - realProgress);
                Matrix matrix = new Matrix();
                matrix.postRotate(360 * realProgress);
                matrix.postScale(realProgress, realProgress);
                matrix.postTranslate(offsetX, offsetY);
                LoadingViewItem.setAlpha(mBarDarkAlpha * realProgress);
                canvas.concat(matrix);
            }
        }
        LoadingViewItem.draw(canvas);
        canvas.restore();
    }
    if (mIsInLoading) {
        invalidate();
    }
    canvas.restoreToCount(c1);
}
 
Example 20
Source File: ApplicationPackageManager.java    From AndroidComponentPlugin with Apache License 2.0 4 votes vote down vote up
private Drawable getBadgedDrawable(Drawable drawable, Drawable badgeDrawable,
        Rect badgeLocation, boolean tryBadgeInPlace) {
    final int badgedWidth = drawable.getIntrinsicWidth();
    final int badgedHeight = drawable.getIntrinsicHeight();
    final boolean canBadgeInPlace = tryBadgeInPlace
            && (drawable instanceof BitmapDrawable)
            && ((BitmapDrawable) drawable).getBitmap().isMutable();

    final Bitmap bitmap;
    if (canBadgeInPlace) {
        bitmap = ((BitmapDrawable) drawable).getBitmap();
    } else {
        bitmap = Bitmap.createBitmap(badgedWidth, badgedHeight, Bitmap.Config.ARGB_8888);
    }
    Canvas canvas = new Canvas(bitmap);

    if (!canBadgeInPlace) {
        drawable.setBounds(0, 0, badgedWidth, badgedHeight);
        drawable.draw(canvas);
    }

    if (badgeLocation != null) {
        if (badgeLocation.left < 0 || badgeLocation.top < 0
                || badgeLocation.width() > badgedWidth || badgeLocation.height() > badgedHeight) {
            throw new IllegalArgumentException("Badge location " + badgeLocation
                    + " not in badged drawable bounds "
                    + new Rect(0, 0, badgedWidth, badgedHeight));
        }
        badgeDrawable.setBounds(0, 0, badgeLocation.width(), badgeLocation.height());

        canvas.save();
        canvas.translate(badgeLocation.left, badgeLocation.top);
        badgeDrawable.draw(canvas);
        canvas.restore();
    } else {
        badgeDrawable.setBounds(0, 0, badgedWidth, badgedHeight);
        badgeDrawable.draw(canvas);
    }

    if (!canBadgeInPlace) {
        BitmapDrawable mergedDrawable = new BitmapDrawable(mContext.getResources(), bitmap);

        if (drawable instanceof BitmapDrawable) {
            BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
            mergedDrawable.setTargetDensity(bitmapDrawable.getBitmap().getDensity());
        }

        return mergedDrawable;
    }

    return drawable;
}