private void drawTriangle(Canvas c, float startAngle, float sweepAngle, Rect bounds) {
      if (mShowArrow) {
        if (mArrow == null) {
          mArrow = new Path();
          mArrow.setFillType(Path.FillType.EVEN_ODD);
        } else {
          mArrow.reset();
        }

        // Adjust the position of the triangle so that it is inset as
        // much as the arc, but also centered on the arc.
        float inset = (int) mStrokeInset / 2 * mArrowScale;
        float x = (float) (mRingCenterRadius * Math.cos(0) + bounds.exactCenterX());
        float y = (float) (mRingCenterRadius * Math.sin(0) + bounds.exactCenterY());

        // Update the path each time. This works around an issue in SKIA
        // where concatenating a rotation matrix to a scale matrix
        // ignored a starting negative rotation. This appears to have
        // been fixed as of API 21.
        mArrow.moveTo(0, 0);
        mArrow.lineTo(mArrowWidth * mArrowScale, 0);
        mArrow.lineTo((mArrowWidth * mArrowScale / 2), (mArrowHeight * mArrowScale));
        mArrow.offset(x - inset, y);
        mArrow.close();
        // draw a triangle
        mArrowPaint.setColor(mColors[mColorIndex]);
        c.rotate(
            startAngle + sweepAngle - ARROW_OFFSET_ANGLE,
            bounds.exactCenterX(),
            bounds.exactCenterY());
        c.drawPath(mArrow, mArrowPaint);
      }
    }
  private void computeGlyphPath() {
    drawableArea.set(getBounds());
    drawableArea.inset(padding, padding);
    glyphPaint.getTextPath(glyph, 0, 1, 0, 0, glyphPath);
    // Add an extra path point to fix the icon remaining blank on a Galaxy Note 2 running 4.1.2.
    glyphPath.computeBounds(glyphPathBounds, false);
    final float centerX = glyphPathBounds.centerX();
    final float centerY = glyphPathBounds.centerY();
    glyphPath.moveTo(centerX, centerY);
    glyphPath.lineTo(centerX + 0.001f, centerY + 0.001f);
    final float areaWidthF = (float) drawableArea.width();
    final float areaHeightF = (float) drawableArea.height();
    final float scaleX = areaWidthF / glyphPathBounds.width();
    final float scaleY = areaHeightF / glyphPathBounds.height();
    final float scaleFactor = Math.min(scaleX, scaleY);
    glyphPathTransform.setScale(scaleFactor, scaleFactor);
    glyphPath.transform(glyphPathTransform);

    // TODO this two pass calculation irks me.
    // It has to be possible to push this into a single Matrix transform; what makes it hard is
    // that the origin of Text is not top-left, but baseline-left so need to account for that.
    glyphPath.computeBounds(glyphPathBounds, false);
    final float areaLeftF = (float) drawableArea.left;
    final float areaTopF = (float) drawableArea.top;
    float transX = areaLeftF - glyphPathBounds.left;
    transX += 0.5f * Math.abs(areaWidthF - glyphPathBounds.width());
    float transY = areaTopF - glyphPathBounds.top;
    transY += 0.5f * Math.abs(areaHeightF - glyphPathBounds.height());
    glyphPath.offset(transX, transY);

    invalidateSelf();
  }
Beispiel #3
0
  @Override
  protected void onDraw(Canvas canvas) {
    Bitmap sweet = BitmapFactory.decodeResource(getResources(), R.drawable.sq);
    canvas.drawColor(color.black);
    canvas.drawBitmap(sweet, 0, 0, null);

    Paint paint = new Paint();

    // draw a red circle
    paint.setColor(Color.RED);
    canvas.drawCircle(20, 50, 25, paint);

    // a blue circle with antialiasing turned on
    paint.setAntiAlias(true);
    paint.setColor(Color.BLUE);
    canvas.drawCircle(60, 50, 25, paint);

    // Now we wiil create some triangles.Create a Path object to store
    // out triangle's segments. Use.offset to draw in many locations.
    paint.setStyle(Paint.Style.FILL_AND_STROKE);
    paint.setStrokeWidth(2);
    paint.setColor(Color.GREEN);
    Path path = new Path();
    path.moveTo(4, -10);
    path.lineTo(20, 0);
    path.lineTo(-9, 0);
    path.close();
    path.offset(60, 40);
    canvas.drawPath(path, paint);
    path.offset(90, 100);
    canvas.drawPath(path, paint);
    path.offset(80, 150);
    canvas.drawPath(path, paint);

    // Now draw a text using FILL style:
    paint.setStyle(Paint.Style.FILL);
    paint.setAntiAlias(true);
    paint.setTextSize(20);
    canvas.drawText("Hello Android! Fill...", 50, 230, paint);

    // rotate the text, and you can draw it where you want:
    int x = 75;
    int y = 185;
    paint.setColor(Color.GRAY);
    paint.setTextSize(25);
    String rotatedtext = "Rotated hellowandroid:)";

    // draw bounding rect befor rotating text:
    Rect rect = new Rect();
    paint.getTextBounds(rotatedtext, 0, rotatedtext.length(), rect);
    canvas.translate(x, y);
    paint.setStyle(Paint.Style.FILL);

    canvas.drawText("Rotated helloandroid :)", 0, 0, paint);
    paint.setStyle(Paint.Style.STROKE);
    canvas.drawRect(rect, paint);

    canvas.translate(-x, -y);

    paint.setColor(Color.RED);
    canvas.rotate(-45, x + rect.exactCenterX(), y + rect.exactCenterY());
    paint.setStyle(Paint.Style.FILL);
    canvas.drawText(rotatedtext, x, y, paint);

    // draw a draw a thick dashed line in angle
    DashPathEffect dashPath = new DashPathEffect(new float[] {10, 40}, 1);
    paint.setPathEffect(dashPath);
    paint.setStrokeWidth(8);
    canvas.drawLine(0, 60, 320, 300, paint);
  }