如何减少位图中的间距(渲染文本)

时间:2015-06-01 07:55:43

标签: android

我已经编码以图像的形式打印文本。然后问题是,当我打印时,间距距离每条线太远。我不知道怎么做。但在我的代码中没有“\ n”。我不知道为什么会这样。无论是我的drawtext还是我的缺点

public void drawText(String text, int textSize) { 

    TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint.LINEAR_TEXT_FLAG); //(Paint.ANTI_ALIAS_FLAG | Paint.LINEAR_TEXT_FLAG)
    textPaint.setStyle(Paint.Style.FILL);
    textPaint.setColor(Color.BLACK);
    textPaint.setTextSize(textSize);

    try
    {
        Typeface tf =Typeface.createFromAsset(getAssets(),"fonts/andalemono.ttf");
        textPaint.setTypeface(tf);
    }
    catch(Exception e)
    {
        Log.d("your tag", "typeface error: ", e);
    }

    StaticLayout mTextLayout = new StaticLayout(text, textPaint, 370, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);

   //draw background
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.LINEAR_TEXT_FLAG);
    paint.setStyle(Paint.Style.FILL);
    paint.setColor(Color.WHITE);
    float baseline = (int) (-paint.ascent() + 0.5f); 
    int height = (int) ((baseline + paint.descent()+ 0.5f + (textSize/2))); 
    Bitmap image = Bitmap.createBitmap(370, height, Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(image);
    c.drawText(text,0, baseline, paint);
    c.drawPaint(paint);


    // Draw text
    c.save();
    c.translate(0,0);
    mTextLayout.draw(c);
    c.restore();
    printBitmap(image);
}

myfunction printBitmap()

public void printBitmap(Bitmap bmp)
{
    if (!myPrinter.printImage(bmp))
        return;

}

0 个答案:

没有答案