// ==============================================================================
  public final int[] renderGlyph(
      char glyph, Paint paint, android.graphics.Matrix matrix, Rect bounds) {
    Path p = new Path();
    paint.getTextPath(String.valueOf(glyph), 0, 1, 0.0f, 0.0f, p);

    RectF boundsF = new RectF();
    p.computeBounds(boundsF, true);
    matrix.mapRect(boundsF);

    boundsF.roundOut(bounds);
    bounds.left--;
    bounds.right++;

    final int w = bounds.width();
    final int h = bounds.height();

    Bitmap bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);

    Canvas c = new Canvas(bm);
    matrix.postTranslate(-bounds.left, -bounds.top);
    c.setMatrix(matrix);
    c.drawPath(p, paint);

    final int sizeNeeded = w * h;
    if (cachedRenderArray.length < sizeNeeded) cachedRenderArray = new int[sizeNeeded];

    bm.getPixels(cachedRenderArray, 0, w, 0, 0, w, h);
    bm.recycle();
    return cachedRenderArray;
  }
Пример #2
0
  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();
  }
Пример #3
0
  private void generateString(String s, double size, int key) {
    if (s != null) {
      // generate an image
      Paint paint_temp = new Paint();
      paint_temp.setAntiAlias(true);
      paint_temp.setStyle(Style.FILL);
      paint_temp.setColor(Color.WHITE);
      paint_temp.setTextSize((float) size);
      paint_temp.setFilterBitmap(false);

      Paint paint_stroke = new Paint();
      paint_stroke.setColor(Color.BLACK);
      paint_stroke.setTextAlign(Paint.Align.CENTER);
      paint_stroke.setTextSize((float) size);
      paint_stroke.setTypeface(Typeface.DEFAULT_BOLD);
      paint_stroke.setStyle(Paint.Style.STROKE);
      paint_stroke.setStrokeWidth(6);
      paint_stroke.setAntiAlias(true);
      paint_stroke.setFilterBitmap(false);

      // prep
      ArrayList<Path> path_splits = new ArrayList<Path>();

      int height = padding;
      int width = 0;

      // split it apart
      for (String line : s.split("\n")) {
        line = line.trim();

        // mini measurements
        Rect rect_temp = new Rect();
        paint_stroke.getTextBounds(line, 0, line.length(), rect_temp);

        int this_line_height = Math.abs(rect_temp.top - rect_temp.bottom);
        int this_line_width = rect_temp.left + rect_temp.right;

        int old_height = height;

        height += this_line_height + line_height;
        if (this_line_width > width) width = this_line_width;

        // create some paths
        Path line_path = new Path();
        paint_temp.getTextPath(
            line, 0, line.length(), padding, Math.abs(rect_temp.top) + old_height, line_path);
        path_splits.add(line_path);
      }

      // proper padding
      width += padding * 2;
      height -= line_height;
      height += padding;

      // fullscale measurements
      Bitmap bitmap_temp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444);
      Canvas canvas_temp = new Canvas(bitmap_temp);

      // finaly after all that, draw it
      for (int i = path_splits.size() - 1; i >= 0; i--) {
        canvas_temp.drawPath(path_splits.get(i), paint_stroke);
        canvas_temp.drawPath(path_splits.get(i), paint_temp);
      }

      // stuff it in the buffer
      // note this automatically destroys the bitmap
      bitmap_buffer.put(
          key, new Quad(key, bitmap_temp, bitmap_temp.getWidth(), bitmap_temp.getHeight()));
    }
  }