首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从布局到StaticLayout:如何使用StaticLayout代替简单的布局来绘制多行文本?

从布局到StaticLayout:如何使用StaticLayout代替简单的布局来绘制多行文本?
EN

Stack Overflow用户
提问于 2018-03-31 21:53:54
回答 1查看 554关注 0票数 0

我一直在开发一个应用程序,让用户拍照,然后让他在上面放一条短信。当用户输入一个小短语时,这是完美的。这是我的密码:

代码语言:javascript
复制
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画布时,它不起作用。

有没有一种简单的方法可以将这些代码转换为staticLayout?还有什么我错过的吗?

为了解决这个问题,我损失了一周时间,但到目前为止都没有效果。谢谢!

EN

回答 1

Stack Overflow用户

发布于 2018-03-31 23:20:50

我刚找到一个解决办法,比我想的要容易!开始了!希望有一天它能帮助到别人!

代码语言:javascript
复制
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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49592784

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档