将重力应用于StaticLayout

时间:2017-04-17 07:10:12

标签: android android-layout

将重力应用于StaticLayout。我尝试过Paint.Align.x,但文本的某些部分是不可见的。我正在使用MotionView和Layer。任何帮助将不胜感激,谢谢。

1 个答案:

答案 0 :(得分:0)

    int boundsWidth = canvasWidth;


    textPaint.setStyle(Paint.Style.FILL);
    textPaint.setTextSize(textLayer.getEditText().getTextSize());
    textPaint.setColor(textLayer.getEditText().getCurrentTextColor());
    textPaint.setTypeface(textLayer.getEditText().getTypeface());
    int value = textLayer.getEditText().getGravity();

    Log.d("entity gravity value==", "gravity value==" + value);
    if (textLayer.getEditText().getGravity() == 53) {
        gravity = Paint.Align.RIGHT;
    } else if (textLayer.getEditText().getGravity() == 51) {
        gravity = Paint.Align.LEFT;
    } else if (textLayer.getEditText().getGravity() == 17) {
        gravity = Paint.Align.CENTER;
    }

    textPaint.setTextAlign(gravity);

    // Static layout which will be drawn on canvas
    StaticLayout sl = new StaticLayout(
            textLayer.getEditText().getText(),
            textPaint,
            boundsWidth,// - width of the layout
            Layout.Alignment.ALIGN_CENTER, // - layout alignment
            1.0f, // 1 - text spacing multiply
            1.0f, // 1 - text spacing add
            false); // true - include padding


    int boundsHeight = sl.getHeight();


    // calculate height for the entity, min - Limits.MIN_BITMAP_HEIGHT

    int bmpHeight = (int) (canvasHeight * Math.max(TextLayer.Limits.MIN_BITMAP_HEIGHT,
            1.0F * boundsHeight / canvasHeight));


    // create bitmap where text will be drawn
    Bitmap bmp;
    if (reuseBmp != null && reuseBmp.getWidth() == boundsWidth
            && reuseBmp.getHeight() == bmpHeight) {
        // if previous bitmap exists, and it's width/height is the same - reuse it
        bmp = reuseBmp;
        bmp.eraseColor(Color.TRANSPARENT);
        // erase color when reusing
    } else {
        bmp = Bitmap.createBitmap(boundsWidth, bmpHeight, Bitmap.Config.ARGB_8888);
    }


    Log.d("TEXT_ENTITY", "bmp_height== " + bmpHeight + "bmp_width== " + boundsWidth + "bound_height==" + boundsHeight);
    Canvas canvas = new Canvas(bmp);
    canvas.save();

    //text will be drawn from left

    // move text to center if bitmap is bigger that text
    if (boundsHeight < bmpHeight) {
        //calculate Y coordinate - In this case we want to draw the text in the
        //center of the canvas so we move Y coordinate to center.
        float textYCoordinate = (bmpHeight - boundsHeight) / 2;
        canvas.translate(0, textYCoordinate);
    }

    //draws static layout on canvas

    sl.draw(canvas);
    canvas.restore();

    return bmp;
相关问题