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

The following examples show how to use android.graphics.Canvas#rotate() . 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: ScaleView.java    From ScaleView with Apache License 2.0 6 votes vote down vote up
/**
 * 画指示器
 *
 * @param canvas
 */
private void drawIndicator(Canvas canvas) {
    PathMeasure mPathMeasure = new PathMeasure();
    mPathMeasure.setPath(mArcPath, false);
    float[] tan = new float[2];
    float[] pos = new float[2];
    mPathMeasure.getPosTan(mPathMeasure.getLength() * (0.5f), pos, tan);
    canvas.save();
    double angle = calcArcAngle(Math.atan2(tan[1], tan[0])) + 90;
    canvas.rotate((float) angle, pos[0], pos[1]);
    //画直线
    canvas.drawLine(pos[0], pos[1], pos[0] + 80, pos[1], mIndicatorPaint);
    Path linePath = new Path();
    //画箭头
    linePath.moveTo(pos[0] + 80, pos[1] - 20);
    linePath.lineTo(pos[0] + 80 + 20, pos[1]);
    linePath.lineTo(pos[0] + 80, pos[1] + 20);
    canvas.drawPath(linePath, mIndicatorPaint);
    canvas.restore();
}
 
Example 2
Source File: Utils.java    From Kawaii_LoadingView with Apache License 2.0 6 votes vote down vote up
/**
 * 步骤4:绘制方块
 */

@Override
protected void onDraw(Canvas canvas) {

    // 1. 绘制内部方块(固定的)
    for (int i = 0; i < mfixedBlocks.length; i++) {
        // 根据标志位判断是否需要绘制
        if (mfixedBlocks[i].isShow) {
            // 传入方块位置参数、圆角 & 画笔属性
            canvas.drawRoundRect(mfixedBlocks[i].rectF, fixBlock_Angle, fixBlock_Angle, mPaint);
        }
    }
    // 2. 绘制移动的方块()
    if (mMoveBlock.isShow) {
        canvas.rotate(isClock_Wise ? mRotateDegree : -mRotateDegree, mMoveBlock.cx, mMoveBlock.cy);
        canvas.drawRoundRect(mMoveBlock.rectF, moveBlock_Angle, moveBlock_Angle, mPaint);
    }

}
 
Example 3
Source File: MaterialProgressDrawable.java    From NestRefreshLayout with MIT License 5 votes vote down vote up
@Override
public void draw(Canvas c) {
    final Rect bounds = getBounds();
    final int saveCount = c.save();
    c.rotate(mRotation, bounds.exactCenterX(), bounds.exactCenterY());
    mRing.draw(c, bounds);
    c.restoreToCount(saveCount);
}
 
Example 4
Source File: Sharingan.java    From mkloader with Apache License 2.0 5 votes vote down vote up
@Override public void draw(Canvas canvas) {
  canvas.save();
  canvas.scale(scale, scale, center.x, center.y);
  canvas.rotate(rotate, center.x, center.y);
  eye.draw(canvas);
  eyeBound.draw(canvas);
  for (int i = 0; i < numberOfSharingan; i++) {
    canvas.save();
    canvas.rotate(i * 120, center.x, center.y);
    sharingans[i].draw(canvas);
    canvas.restore();
  }
  canvas.restore();
}
 
Example 5
Source File: SecondPellet.java    From CoolAndroidAnim with Apache License 2.0 5 votes vote down vote up
private void drawAroundPoint(Canvas canvas) {
    mPaint.setStyle(Paint.Style.FILL);
    for (int i = 0; i < 8; i++) {
        mPaint.setAlpha(160);
        canvas.save();
        canvas.rotate(45 * i + mAroundLineDegrees, getCurX(), getCurY());
        canvas.drawCircle(getCurX(), getCurY() - MAX_RADIUS_CIRCLE / 2 - mAroundPointY, AROUND_POINT_RADIUS, mPaint);
        canvas.restore();
    }
}
 
Example 6
Source File: CameraRotaeView.java    From Android_UE with Apache License 2.0 5 votes vote down vote up
@Override
    public void draw(Canvas canvas) {
        super.draw(canvas);

        float centerX = BITMAP_WIDTH;
        float centerY = BITMAP_WIDTH;

        // ----绘制上半部分
        canvas.save();
        canvas.translate(centerX, centerX);
        canvas.rotate(-degrees);
        canvas.clipRect(-BITMAP_WIDTH, -BITMAP_WIDTH, BITMAP_WIDTH, 0);
        canvas.rotate(degrees);
        canvas.translate(-centerX, -centerY);
        canvas.drawBitmap(mBitmap, centerX - BITMAP_WIDTH / 2, centerY - BITMAP_WIDTH / 2, mPaint);
        canvas.restore();


        // 绘制下半部分
        canvas.save();
        // 其实是把 x0 y0 移动到了中心点
        // canvas.translate 移动的是坐标原点,而不是画布
        canvas.translate(centerX, centerY);
        canvas.rotate(-degrees);
        // 然后将重心点移动到原点附近
//        if (degrees != 0)
            mCamera.applyToCanvas(canvas);
        canvas.clipRect(-BITMAP_WIDTH, 0, BITMAP_WIDTH, BITMAP_WIDTH);
        canvas.rotate(degrees);
        canvas.translate(-centerX, -centerY);
        canvas.drawBitmap(mBitmap, centerX - BITMAP_WIDTH / 2, centerY - BITMAP_WIDTH / 2, mPaint);
        canvas.restore();
    }
 
Example 7
Source File: SmallYellowBall.java    From CoolAndroidAnim with Apache License 2.0 5 votes vote down vote up
public void drawSelf(Canvas canvas) {
    if (isShow) {
        canvas.save();
        canvas.rotate(mAngle, mCurX, mCurY);
        canvas.drawOval(mRectF, mPaint);
        canvas.restore();
    }
}
 
Example 8
Source File: PullWaveView.java    From KotlinMVPRxJava2Dagger2GreenDaoRetrofitDemo with Apache License 2.0 5 votes vote down vote up
private void drawRefreshingState(Canvas canvas) {
    for (int i = 0; i < 12; i++) {
        mRefreshingPaint.setAlpha(((i + 1 + control) % 12 * 255) / 12);
        canvas.drawLine(mScreenWidth/2, 220, mScreenWidth/2, 200, mRefreshingPaint);
        canvas.rotate(30, mScreenWidth/2, 250);
    }
}
 
Example 9
Source File: OldCreditSesameView.java    From CreditSesameRingView with Apache License 2.0 5 votes vote down vote up
/**
 * 绘制大小刻度线
 */
private void drawCalibration(Canvas canvas) {

  int dst = (int) (2 * radius - mGradientRingPaint.getStrokeWidth());
  for (int i = 0; i <= 50; i++) {
    canvas.save();
    canvas.rotate(-(-10 + 4 * i), radius, radius);
    if (i % 10 == 0) {
      canvas.drawLine(dst, radius, 2 * radius, radius, mBigCalibrationPaint);
    } else {
      canvas.drawLine(dst, radius, 2 * radius, radius, mSmallCalibrationPaint);
    }
    canvas.restore();
  }
}
 
Example 10
Source File: CanvasTransformerBuilder.java    From BigApp_Discuz_Android with Apache License 2.0 5 votes vote down vote up
public CanvasTransformer rotate(final int openedDeg, final int closedDeg, 
		final int px, final int py, final Interpolator interp) {
	initTransformer();
	mTrans = new CanvasTransformer() {
		public void transformCanvas(Canvas canvas, float percentOpen) {
			mTrans.transformCanvas(canvas, percentOpen);
			float f = interp.getInterpolation(percentOpen);
			canvas.rotate((openedDeg - closedDeg) * f + closedDeg, 
					px, py);
		}			
	};
	return mTrans;
}
 
Example 11
Source File: SunLineView.java    From styT with Apache License 2.0 5 votes vote down vote up
/**
 * 绘制line
 *
 * @param canvas
 */
private void drawLines(Canvas canvas) {
    for (int i = 0; i <= 360; i++) {
        if (i % mLineLevel == 0) {
            canvas.save();
            canvas.rotate(i, mWidth / 2, mHeight / 2);
            canvas.drawLine(mLineLeft, mLineTop, mLineLeft, mLineBottom, mLinePaint);
            canvas.restore();
        }
    }
}
 
Example 12
Source File: MaterialProgressDrawable.java    From ucar-weex-core with Apache License 2.0 5 votes vote down vote up
@Override
public void draw(Canvas c) {
  final Rect bounds = getBounds();
  final int saveCount = c.save();
  c.rotate(mRotation, bounds.exactCenterX(), bounds.exactCenterY());
  mRing.draw(c, bounds);
  c.restoreToCount(saveCount);
}
 
Example 13
Source File: AudioVisualizerDrawable.java    From Telegram with GNU General Public License v2.0 4 votes vote down vote up
public void draw(Canvas canvas, float cx, float cy, boolean outOwner) {
    if (outOwner) {
        p1.setColor(Theme.getColor(Theme.key_chat_outLoader));
        p1.setAlpha(ALPHA);
    } else {
        p1.setColor(Theme.getColor(Theme.key_chat_inLoader));
        p1.setAlpha(ALPHA);
    }
    for (int i = 0; i < 8; i++) {
        if (animateTo[i] != current[i]) {
            current[i] += dt[i] * 16;
            if ((dt[i] > 0 && current[i] > animateTo[i]) || (dt[i] < 0 && current[i] < animateTo[i])) {
                current[i] = animateTo[i];
            }
            parentView.invalidate();
        }
    }

    if (idleScaleInc) {
        idleScale += 0.02f;
        if (idleScale > 1f) {
            idleScaleInc = false;
            idleScale = 1f;
        }
    } else {
        idleScale -= 0.02f;
        if (idleScale < 0) {
            idleScaleInc = true;
            idleScale = 0;
        }
    }

    float enterProgress = current[7];
    float radiusProgress = current[6] * current[0];

    if (enterProgress == 0 && radiusProgress == 0) {
        return;
    }
    // float idleProgress = radiusProgress > 0.4f ? 0 : (1f - radiusProgress / 0.4f);

    for (int i = 0; i < 3; i++) {
        tmpWaveform[i] = (int) (current[i] * WAVE_RADIUS);
    }

    //drawables[0].idleStateDiff = enterProgress * idleProgress * IDLE_AMPLITUDE;
    //drawables[1].idleStateDiff = enterProgress * idleProgress * IDLE_AMPLITUDE;

    drawables[0].setAdditionals(tmpWaveform);

    for (int i = 0; i < 3; i++) {
        tmpWaveform[i] = (int) (current[i + 3] * WAVE_RADIUS);
    }
    drawables[1].setAdditionals(tmpWaveform);
    float radius = AndroidUtilities.dp(22) +
            AndroidUtilities.dp(4) * radiusProgress +
            IDLE_RADIUS * enterProgress;

    if (radius > AndroidUtilities.dp(26)) {
        radius = AndroidUtilities.dp(26);
    }
    drawables[0].radius = drawables[1].radius = radius;

    canvas.save();
    rotation += 0.6;
    canvas.rotate(rotation, cx, cy);
    canvas.save();
    float s = 1f + 0.04f * idleScale;
    canvas.scale(s, s, cx, cy);
    drawables[0].draw(cx, cy, canvas, p1);
    canvas.restore();

    canvas.rotate(60, cx, cy);
    s = 1f + 0.04f * (1f - idleScale);
    canvas.scale(s, s, cx, cy);
    drawables[1].draw(cx, cy, canvas, p1);
    canvas.restore();
}
 
Example 14
Source File: MaterialMenuDrawable.java    From WeGit with Apache License 2.0 4 votes vote down vote up
private void drawMiddleLine(Canvas canvas, float ratio) {
    canvas.restore();
    canvas.save();

    float rotation = 0;
    float pivotX = width / 2;
    float pivotY = width / 2;
    float startX = sidePadding;
    float startY = topPadding + dip3 / 2 * 5;
    float stopX = width - sidePadding;
    float stopY = topPadding + dip3 / 2 * 5;
    int alpha = 255;

    switch (animationState) {
        case BURGER_ARROW:
            // rotate by 180
            if (isMorphingForward()) {
                rotation = ratio * ARROW_MID_LINE_ANGLE;
            } else {
                rotation = ARROW_MID_LINE_ANGLE + (1 - ratio) * ARROW_MID_LINE_ANGLE;
            }
            // shorten one end
            stopX -= ratio * resolveStrokeModifier(ratio) / 2;
            break;
        case BURGER_X:
            // fade out
            alpha = (int) ((1 - ratio) * 255);
            break;
        case ARROW_X:
            // fade out and shorten one end
            alpha = (int) ((1 - ratio) * 255);
            startX += (1 - ratio) * dip2;
            break;
        case ARROW_CHECK:
            if (isMorphingForward()) {
                // rotate until required angle
                rotation = ratio * CHECK_MIDDLE_ANGLE;
            } else {
                // rotate back to starting angle
                rotation = CHECK_MIDDLE_ANGLE - CHECK_MIDDLE_ANGLE * (1 - ratio);
            }
            // shorten one end and lengthen the other
            startX += dip3 / 2 + dip4 - (1 - ratio) * dip2;
            stopX += ratio * dip1;
            pivotX = width / 2 + dip3 + diph;
            break;
        case BURGER_CHECK:
            // rotate until required angle
            rotation = ratio * CHECK_MIDDLE_ANGLE;
            // lengthen both ends
            startX += ratio * (dip4 + dip3 / 2);
            stopX += ratio * dip1;
            pivotX = width / 2 + dip3 + diph;
            break;
        case X_CHECK:
            // fade in
            alpha = (int) (ratio * 255);
            // rotation to check angle
            rotation = ratio * CHECK_MIDDLE_ANGLE;
            // lengthen both ends
            startX += ratio * (dip4 + dip3 / 2);
            stopX += ratio * dip1;
            pivotX = width / 2 + dip3 + diph;
            break;
    }

    iconPaint.setAlpha(alpha);
    canvas.rotate(rotation, pivotX, pivotY);
    canvas.drawLine(startX, startY, stopX, stopY, iconPaint);
    iconPaint.setAlpha(255);
}
 
Example 15
Source File: ZoomControlView.java    From Telegram-FOSS with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {
    int cx = getMeasuredWidth() / 2;
    int cy = getMeasuredHeight() / 2;
    boolean isPortrait = getMeasuredWidth() > getMeasuredHeight();

    if (isPortrait) {
        minusCx = AndroidUtilities.dp(16 + 25);
        minusCy = cy;
        plusCx = getMeasuredWidth() - AndroidUtilities.dp(16 + 25);
        plusCy = cy;

        progressStartX = minusCx + AndroidUtilities.dp(18);
        progressStartY = cy;

        progressEndX = plusCx - AndroidUtilities.dp(18);
        progressEndY = cy;
    } else {
        minusCx = cx;
        minusCy = AndroidUtilities.dp(16 + 25);
        plusCx = cx;
        plusCy = getMeasuredHeight() - AndroidUtilities.dp(16 + 25);

        progressStartX = cx;
        progressStartY = minusCy + AndroidUtilities.dp(18);

        progressEndX = cx;
        progressEndY = plusCy - AndroidUtilities.dp(18);
    }
    minusDrawable.setBounds(minusCx - AndroidUtilities.dp(7), minusCy - AndroidUtilities.dp(7), minusCx + AndroidUtilities.dp(7), minusCy + AndroidUtilities.dp(7));
    minusDrawable.draw(canvas);
    plusDrawable.setBounds(plusCx - AndroidUtilities.dp(7), plusCy - AndroidUtilities.dp(7), plusCx + AndroidUtilities.dp(7), plusCy + AndroidUtilities.dp(7));
    plusDrawable.draw(canvas);

    int totalX = progressEndX - progressStartX;
    int totalY = progressEndY - progressStartY;
    int knobX = (int) (progressStartX + totalX * zoom);
    int knobY = (int) (progressStartY + totalY * zoom);

    if (isPortrait) {
        progressDrawable.setBounds(progressStartX, progressStartY - AndroidUtilities.dp(3), progressEndX, progressStartY + AndroidUtilities.dp(3));
        filledProgressDrawable.setBounds(progressStartX, progressStartY - AndroidUtilities.dp(3), knobX, progressStartY + AndroidUtilities.dp(3));
    } else {
        progressDrawable.setBounds(progressStartY, 0, progressEndY, AndroidUtilities.dp(6));
        filledProgressDrawable.setBounds(progressStartY, 0, knobY, AndroidUtilities.dp(6));
        canvas.save();
        canvas.rotate(90);
        canvas.translate(0, -progressStartX - AndroidUtilities.dp(3));
    }
    progressDrawable.draw(canvas);
    filledProgressDrawable.draw(canvas);
    if (!isPortrait) {
        canvas.restore();
    }

    Drawable drawable = knobPressed ? pressedKnobDrawable : knobDrawable;
    int size = drawable.getIntrinsicWidth();
    drawable.setBounds(knobX - size / 2, knobY - size / 2, knobX + size / 2, knobY + size / 2);
    drawable.draw(canvas);
}
 
Example 16
Source File: RotateImageView.java    From Social with Apache License 2.0 4 votes vote down vote up
protected void onDraw(Canvas canvas) {
	canvas.rotate(rotation, getWidth() / 2, getHeight() / 2);
	super.onDraw(canvas);
}
 
Example 17
Source File: RoundRectDrawableWithShadow.java    From Slice with Apache License 2.0 4 votes vote down vote up
protected void drawShadow(Canvas canvas) {
    final float edgeShadowTop = -mCornerRadius - mShadowSize;
    final float inset = mCornerRadius + mInsetShadow + mRawShadowSize / 2;
    final boolean drawHorizontalEdges = mCardBounds.width() - 2 * inset > 0;
    final boolean drawVerticalEdges = mCardBounds.height() - 2 * inset > 0;
    // LT
    int saved = canvas.save();
    canvas.translate(mCardBounds.left + inset, mCardBounds.top + inset);
    canvas.drawPath(mCornerShadowPath, mCornerShadowPaint);
    if (drawHorizontalEdges) {
        canvas.drawRect(0, edgeShadowTop,
                mCardBounds.width() - 2 * inset, -mCornerRadius,
                mEdgeShadowPaint);
    }
    canvas.restoreToCount(saved);
    // RB
    saved = canvas.save();
    canvas.translate(mCardBounds.right - inset, mCardBounds.bottom - inset);
    canvas.rotate(180f);
    canvas.drawPath(mCornerShadowPath, mCornerShadowPaint);
    if (drawHorizontalEdges) {
        canvas.drawRect(0, edgeShadowTop,
                mCardBounds.width() - 2 * inset, -mCornerRadius + mShadowSize,
                mEdgeShadowPaint);
    }
    canvas.restoreToCount(saved);
    // LB
    saved = canvas.save();
    canvas.translate(mCardBounds.left + inset, mCardBounds.bottom - inset);
    canvas.rotate(270f);
    canvas.drawPath(mCornerShadowPath, mCornerShadowPaint);
    if (drawVerticalEdges) {
        canvas.drawRect(0, edgeShadowTop,
                mCardBounds.height() - 2 * inset, -mCornerRadius, mEdgeShadowPaint);
    }
    canvas.restoreToCount(saved);
    // RT
    saved = canvas.save();
    canvas.translate(mCardBounds.right - inset, mCardBounds.top + inset);
    canvas.rotate(90f);
    canvas.drawPath(mCornerShadowPath, mCornerShadowPaint);
    if (drawVerticalEdges) {
        canvas.drawRect(0, edgeShadowTop,
                mCardBounds.height() - 2 * inset, -mCornerRadius, mEdgeShadowPaint);
    }
    canvas.restoreToCount(saved);
}
 
Example 18
Source File: ClockView.java    From ClockView with Apache License 2.0 4 votes vote down vote up
private void drawContent(Canvas mCanvas) {
        mHour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
        mMinute = Calendar.getInstance().get(Calendar.MINUTE);
        mSecond = Calendar.getInstance().get(Calendar.SECOND);
        // 1.将坐标系原点移至去除内边距后的画布中心
        // 默认在画布左上角,这样做是为了更方便的绘制

        mCanvas.drawColor(Color.WHITE);
        mCanvas.translate(getWidth() / 2 +(getPaddingLeft() - getPaddingRight())/2, getHeight() / 2 +(getPaddingTop() -getPaddingBottom())/2);
// 2.绘制圆盘
        mPaint.setStrokeWidth(5f); // 画笔设置2个像素的宽度
        mCanvas.drawCircle(0, 0, mRadius-5f, mPaint); // 到这一步就能知道第一步的好处了,否则害的去计算园的中心点坐标
// 3.绘制时刻度
        for (int i = 0; i < 12; i++) {
            mCanvas.drawLine(0, mRadius, 0, mRadius - mHourDegreeLength, mPaint);
            mCanvas.rotate(30); // 360°平均分成12份,每份30°
        }
// 4.绘制秒刻度
        mPaint.setStrokeWidth(1.5f);
        for (int i = 0; i < 60; i++) {
            //时刻度绘制过的区域不在绘制
            if (i % 5 != 0) {
                mCanvas.drawLine(0, mRadius, 0, mRadius - mSecondDegreeLength, mPaint);
            }
            mCanvas.rotate(6); // 360°平均分成60份,每份6°
        }
// 5.绘制数字
        mPointPaint.setColor(Color.BLACK);
        for (int i = 0; i < 12; i++) {
            String number = 6 + i < 12 ? String.valueOf(6 + i) : (6 + i) > 12
                    ? String.valueOf(i - 6) : "12";
            mCanvas.drawText(number, 0, mRadius * 5.5f / 7, mPointPaint);
            mCanvas.rotate(30);
        }
// 6.绘制上下午
        mCanvas.drawText(mHour < 12 ? "AM" : "PM", 0, mRadius * 1.5f / 4, mPointPaint);
        mCanvas.drawText(mHour+":"+mMinute+":"+mSecond, 0, mRadius * 1.3f, mPointPaint);
// 7.绘制时针
        Path path = new Path();
        path.moveTo(0, 0);
        int[] hourPointerCoordinates = getPointerCoordinates(mHourPointLength);
        path.lineTo(hourPointerCoordinates[0], hourPointerCoordinates[1]);
        path.lineTo(hourPointerCoordinates[2], hourPointerCoordinates[3]);
        path.lineTo(hourPointerCoordinates[4], hourPointerCoordinates[5]);
        path.close();
        mCanvas.save();
        mCanvas.rotate(180 + mHour % 12 * 30 + mMinute * 1.0f / 60 * 30);
        mCanvas.drawPath(path, mPointPaint);
        mCanvas.restore();
// 8.绘制分针
        path.reset();
        path.moveTo(0, 0);
        int[] minutePointerCoordinates = getPointerCoordinates(mMinutePointLength);
        path.lineTo(minutePointerCoordinates[0], minutePointerCoordinates[1]);
        path.lineTo(minutePointerCoordinates[2], minutePointerCoordinates[3]);
        path.lineTo(minutePointerCoordinates[4], minutePointerCoordinates[5]);
        path.close();
        mCanvas.save();
        mCanvas.rotate(180 + mMinute * 6);
        mCanvas.drawPath(path, mPointPaint);
        mCanvas.restore();
// 9.绘制秒针
        mPointPaint.setColor(Color.RED);
        path.reset();
        path.moveTo(0, 0);
        int[] secondPointerCoordinates = getPointerCoordinates(mSecondPointLength);
        path.lineTo(secondPointerCoordinates[0], secondPointerCoordinates[1]);
        path.lineTo(secondPointerCoordinates[2], secondPointerCoordinates[3]);
        path.lineTo(secondPointerCoordinates[4], secondPointerCoordinates[5]);
        path.close();
        mCanvas.save();
        mCanvas.rotate(180 + mSecond * 6);
        mCanvas.drawPath(path, mPointPaint);
        mCanvas.restore();

        //       这里比较难的可能就是指针的绘制,因为我们的指针是个规则形状,其中getPointerCoordinates便是得到这个不规则形状的3个定点坐标,有兴趣的同学可以去研究一下我的逻辑,也可以定义你自己的逻辑。我的逻辑如下(三角函数学的号的同学应该一眼就能看懂):

    }
 
Example 19
Source File: Utils.java    From Ticket-Analysis with MIT License 4 votes vote down vote up
public static void drawMultilineText(Canvas c, StaticLayout textLayout,
                                     float x, float y,
                                     TextPaint paint,
                                     MPPointF anchor, float angleDegrees) {

    float drawOffsetX = 0.f;
    float drawOffsetY = 0.f;
    float drawWidth;
    float drawHeight;

    final float lineHeight = paint.getFontMetrics(mFontMetricsBuffer);

    drawWidth = textLayout.getWidth();
    drawHeight = textLayout.getLineCount() * lineHeight;

    // Android sometimes has pre-padding
    drawOffsetX -= mDrawTextRectBuffer.left;

    // Android does not snap the bounds to line boundaries,
    //  and draws from bottom to top.
    // And we want to normalize it.
    drawOffsetY += drawHeight;

    // To have a consistent point of reference, we always draw left-aligned
    Paint.Align originalTextAlign = paint.getTextAlign();
    paint.setTextAlign(Paint.Align.LEFT);

    if (angleDegrees != 0.f) {

        // Move the text drawing rect in a way that it always rotates around its center
        drawOffsetX -= drawWidth * 0.5f;
        drawOffsetY -= drawHeight * 0.5f;

        float translateX = x;
        float translateY = y;

        // Move the "outer" rect relative to the anchor, assuming its centered
        if (anchor.x != 0.5f || anchor.y != 0.5f) {
            final FSize rotatedSize = getSizeOfRotatedRectangleByDegrees(
                    drawWidth,
                    drawHeight,
                    angleDegrees);

            translateX -= rotatedSize.width * (anchor.x - 0.5f);
            translateY -= rotatedSize.height * (anchor.y - 0.5f);
            FSize.recycleInstance(rotatedSize);
        }

        c.save();
        c.translate(translateX, translateY);
        c.rotate(angleDegrees);

        c.translate(drawOffsetX, drawOffsetY);
        textLayout.draw(c);

        c.restore();
    } else {
        if (anchor.x != 0.f || anchor.y != 0.f) {

            drawOffsetX -= drawWidth * anchor.x;
            drawOffsetY -= drawHeight * anchor.y;
        }

        drawOffsetX += x;
        drawOffsetY += y;

        c.save();

        c.translate(drawOffsetX, drawOffsetY);
        textLayout.draw(c);

        c.restore();
    }

    paint.setTextAlign(originalTextAlign);
}
 
Example 20
Source File: PhotoFilterBlurControl.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    Point centerPoint = getActualCenterPoint();
    float innerRadius = getActualInnerRadius();
    float outerRadius = getActualOuterRadius();
    canvas.translate(centerPoint.x, centerPoint.y);

    if (type == 0) {
        canvas.rotate(angle);

        float space = AndroidUtilities.dp(6.0f);
        float length = AndroidUtilities.dp(12.0f);
        float thickness = AndroidUtilities.dp(1.5f);
        for (int i = 0; i < 30; i++) {
            canvas.drawRect(i * (length + space), -innerRadius, i * (length + space) + length, thickness - innerRadius, paint);
            canvas.drawRect(-i * (length + space) - space - length, -innerRadius, -i * (length + space) - space, thickness - innerRadius, paint);

            canvas.drawRect(i * (length + space), innerRadius, length + i * (length + space), thickness + innerRadius, paint);
            canvas.drawRect(-i * (length + space) - space - length, innerRadius, -i * (length + space) - space, thickness + innerRadius, paint);
        }

        length = AndroidUtilities.dp(6.0f);
        for (int i = 0; i < 64; i++) {
            canvas.drawRect(i * (length + space), -outerRadius, length + i * (length + space), thickness - outerRadius, paint);
            canvas.drawRect(-i * (length + space) - space - length, -outerRadius, -i * (length + space) - space, thickness - outerRadius, paint);

            canvas.drawRect(i * (length + space), outerRadius, length + i * (length + space), thickness + outerRadius, paint);
            canvas.drawRect(-i * (length + space) - space - length, outerRadius, -i * (length + space) - space, thickness + outerRadius, paint);
        }
    } else if (type == 1) {
        float radSpace = 6.15f;
        float radLen = 10.2f;
        arcRect.set(-innerRadius, -innerRadius, innerRadius, innerRadius);
        for (int i = 0; i < 22; i++) {
            canvas.drawArc(arcRect, i * (radSpace + radLen), radLen, false, arcPaint);
        }

        radSpace = 2.02f;
        radLen = 3.6f;
        arcRect.set(-outerRadius, -outerRadius, outerRadius, outerRadius);
        for (int i = 0; i < 64; i++) {
            canvas.drawArc(arcRect, i * (radSpace + radLen), radLen, false, arcPaint);
        }
    }
    canvas.drawCircle(0, 0, AndroidUtilities.dp(8), paint);
}