检索字母表字母的图形表示

时间:2017-11-16 11:15:22

标签: android

我在android中的字符空间表示(例如'A')。像点数组或路径对象或向量的东西。我将自己在屏幕上绘制字符,而不是使用canvas.drawText()。这可能吗?

2 个答案:

答案 0 :(得分:0)

您可以使用以下代码获取Path。我还在尝试查看是否有办法获得更有用的路径,因为此代码生成的路径是本机路径而不是简单路径

Path path = new Path();
Paint paint = new Paint();
paint.getTextPath("A", 0, 1, 0f, 0f, path);

答案 1 :(得分:0)

这是另一种返回点的方法:

private void textStuff() {
    int textSize = 32;
    char letter = 'A';
    getPointsForChar(textSize, letter);
}

@NonNull
private ArrayList<Point> getPointsForChar(int textSize, char letter) {
    Canvas canvas = new Canvas();
    Paint paint = new Paint();
    Bitmap bitmap = Bitmap.createBitmap((int) (float) textSize, (int) (float) textSize, Bitmap.Config.ALPHA_8);
    canvas.drawPaint(paint);
    canvas.setBitmap(bitmap);
    paint.setColor(Color.BLACK);
    paint.setTextSize(textSize);
    canvas.drawText(String.valueOf(letter), 0, (float) textSize, paint);
    Log.d("Font", "bitmap " + bitmap.toString());
    ArrayList<Point> points = new ArrayList<>();
    for (int x = 0; x < bitmap.getWidth(); x++) {
        for (int y = 0; y < bitmap.getHeight(); y++) {
            int pixel = bitmap.getPixel(x, y);
            if (pixel != Color.TRANSPARENT) {
                points.add(new Point(x, y));
            }
        }
    }

    StringBuilder stringBuilder = new StringBuilder("The following points make up '" + letter + "':");
    for (Point point : points) {
        stringBuilder.append("\n\t").append(point.toString()).append(", ");
    }

    Log.d("Font", stringBuilder.toString());
    return points;
}

上述方法的输出是:

The following points make up 'A':
    Point(1, 29),
    Point(1, 30),
    Point(1, 31),
    Point(2, 27),
    Point(2, 28),
    Point(2, 29),
    Point(2, 30),
    Point(2, 31),
    Point(3, 24),
    Point(3, 25),
    Point(3, 26),
    Point(3, 27),
    Point(3, 28),
    Point(3, 29),
    Point(3, 30),
    Point(3, 31),
    Point(4, 22),
    Point(4, 23),
    Point(4, 24),
    Point(4, 25),
    Point(4, 26),
    Point(4, 27),
    Point(4, 28),
    Point(5, 19),
    Point(5, 20),
    Point(5, 21),
    Point(5, 22),
    Point(5, 23),
    Point(5, 24),
    Point(5, 25),
    Point(5, 26),
    Point(6, 17),
    Point(6, 18),
    Point(6, 19),
    Point(6, 20),
    Point(6, 21),
    Point(6, 22),
    Point(6, 23),
    Point(6, 24),
    Point(6, 25),
    Point(7, 14),
    Point(7, 15),
    Point(7, 16),
    Point(7, 17),
    Point(7, 18),
    Point(7, 19),
    Point(7, 20),
    Point(7, 21),
    Point(7, 24),
    Point(7, 25),
    Point(8, 12),
    Point(8, 13),
    Point(8, 14),
    Point(8, 15),
    Point(8, 16),
    Point(8, 17),
    Point(8, 18),
    Point(8, 24),
    Point(8, 25),
    Point(9, 10),
    Point(9, 11),
    Point(9, 12),
    Point(9, 13),
    Point(9, 14),
    Point(9, 15),
    Point(9, 24),
    Point(9, 25),
    Point(10, 10),
    Point(10, 11),
    Point(10, 12),
    Point(10, 13),
    Point(10, 24),
    Point(10, 25),
    Point(11, 10),
    Point(11, 11),
    Point(11, 12),
    Point(11, 13),
    Point(11, 14),
    Point(11, 15),
    Point(11, 24),
    Point(11, 25),
    Point(12, 12),
    Point(12, 13),
    Point(12, 14),
    Point(12, 15),
    Point(12, 16),
    Point(12, 17),
    Point(12, 18),
    Point(12, 24),
    Point(12, 25),
    Point(13, 14),
    Point(13, 15),
    Point(13, 16),
    Point(13, 17),
    Point(13, 18),
    Point(13, 19),
    Point(13, 20),
    Point(13, 21),
    Point(13, 24),
    Point(13, 25),
    Point(14, 17),
    Point(14, 18),
    Point(14, 19),
    Point(14, 20),
    Point(14, 21),
    Point(14, 22),
    Point(14, 23),
    Point(14, 24),
    Point(14, 25),
    Point(15, 19),
    Point(15, 20),
    Point(15, 21),
    Point(15, 22),
    Point(15, 23),
    Point(15, 24),
    Point(15, 25),
    Point(15, 26),
    Point(16, 22),
    Point(16, 23),
    Point(16, 24),
    Point(16, 25),
    Point(16, 26),
    Point(16, 27),
    Point(16, 28),
    Point(16, 29),
    Point(17, 24),
    Point(17, 25),
    Point(17, 26),
    Point(17, 27),
    Point(17, 28),
    Point(17, 29),
    Point(17, 30),
    Point(17, 31),
    Point(18, 27),
    Point(18, 28),
    Point(18, 29),
    Point(18, 30),
    Point(18, 31),
    Point(19, 30),
    Point(19, 31),