从布局到StaticLayout:如何使用StaticLayout而不是简单的布局来绘制多行文本?

时间:2018-03-31 21:53:54

标签: android android-layout android-studio

我一直在使用应用程序让用户拍照,然后让他在上面放一个文字。当用户键入一个小短语时,这非常有效。 这是我的代码:

enter code here
private Bitmap ProcessingBitmap(String captionString) {
    Bitmap bm1;
    Bitmap newBitmap = null;
    try {         
        bm1 = BitmapFactory.decodeStream(getContentResolver().openInputStream(pickedImage));

        //create an empty bitmap
        newBitmap = Bitmap.createBitmap(bm1.getWidth(), bm1.getHeight(), Bitmap.Config.ARGB_8888);

        //create a new Canvas object and pass this bitmap to it
        Canvas canvas = new Canvas(newBitmap);

        canvas.drawBitmap(bm1, 0, 0, null);
        Paint paintText = new Paint(Paint.ANTI_ALIAS_FLAG);

        paintText.setColor(Color.RED);
        paintText.setTextSize(convertDpToPixel(50,this));
        paintText.setTextAlign(Paint.Align.CENTER);
        paintText.breakText(captionString,true, canvas.getWidth(),null);
        paintText.setStyle(Paint.Style.FILL);
        paintText.setTypeface(Typeface.create("Sans", Typeface.BOLD));

        Rect textRect = new Rect();
        paintText.getTextBounds(captionString, 0, captionString.length(), textRect);

        if(textRect.width() >= (canvas.getWidth() - 4))
            paintText.setTextSize(convertDpToPixel(25,this));

        int xPos = (canvas.getWidth() / 2);
        int yPos = (int) ((canvas.getHeight() / 2) - ((paintText.descent() + paintText.ascent()) / 2)) ;

        //Draw Canvas
        canvas.drawText(captionString, xPos, yPos, paintText);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return newBitmap;
}

但是,当用户输入长短语时,它会离开照片区域!

我在线查看了很多关于StaticLayout的信息。像这样:http://ivankocijan.xyz/android-drawing-multiline-text-on-canvas/

所以我尝试在我的代码中使用它,但我不知道我做错了什么!当我使用StaticLayout Canvas时,它无法正常工作。

有没有简单的方法将此代码转换为staticLayout?我还缺少另一个考虑因素吗?

我失去了一个星期试图解决它,到目前为止没有任何工作。 THX!

1 个答案:

答案 0 :(得分:0)

我刚刚找到了一个解决方案,它比我想象的要容易!在这里!希望有一天它会帮助别人!

private Bitmap ProcessingBitmap(String captionString) {
    Bitmap bm1;
    Bitmap newBitmap = null;
    try {
        //Decode image
        bm1 = BitmapFactory.decodeStream(getContentResolver().openInputStream(pickedImage));

        //create an empty bitmap
        newBitmap = Bitmap.createBitmap(bm1.getWidth(), bm1.getHeight(), Bitmap.Config.ARGB_8888);

        //create a new Canvas object and pass this bitmap to it
        Canvas canvas = new Canvas(newBitmap);

        //pass bitmap to canvas
        canvas.drawBitmap(bm1, 0, 0, null);

        //Create and configure text
        TextPaint mTextPaint = new TextPaint();
        mTextPaint.setColor(Color.RED);
        mTextPaint.setTextSize(convertDpToPixel(100,this));
        mTextPaint.setTextAlign(Paint.Align.CENTER);
        mTextPaint.setAntiAlias(true);
        mTextPaint.setStyle(Paint.Style.FILL);
        mTextPaint.setTypeface(Typeface.create("Sans", Typeface.BOLD));

        //StaticLayout
        StaticLayout layout = new StaticLayout(captionString, mTextPaint, canvas.getWidth(), Layout.Alignment.ALIGN_NORMAL, 1, 1, true);

        //Draw Canvas
        int xPos = (canvas.getWidth() / 2);
        int yPos = (int) ((canvas.getHeight() / 2) - ((mTextPaint.descent() + mTextPaint.ascent()) / 2)) ;
        canvas.translate(xPos, yPos);

        //Draw Canvas
        layout.draw(canvas);

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return newBitmap;
}