gpt4 book ai didi

Android:如何在图像的静态布局中限制最多两行

转载 作者:太空狗 更新时间:2023-10-29 14:28:05 24 4
gpt4 key购买 nike

我有一张图片,我必须在上面和底部写上文字,我正在使用静态布局在上面写字。下面是它的代码

TextPaint mTextPaintTop= new TextPaint();
mTextPaintTop.setColor(Color.RED);
StaticLayout layoutTop = new StaticLayout(top_text, mTextPaintTop,
width,Layout.Alignment.ALIGN_CENTER, 1.3f, 0, false);
canvas.translate(0, 20); //position the text
layoutTop.draw(canvas);

我想限制文本最多两行。如果用户输入的文本很长,超过两行,就缩小字体,调整到两行。

最佳答案

下载此文件 EllipsizingTextView

@Override
public void draw(Canvas canvas) {
// TODO Auto-generated method stub

Paint mpaint = new Paint(Paint.ANTI_ALIAS_FLAG);

mpaint.setColor(Color.RED);

mpaint.setTextSize(13);

mpaint.setTextAlign(Align.CENTER);

drawTextOnCanvas(canvas, "text you want 2 line bigger", 0, 20, mpaint);

}

}

private void drawTextOnCanvas(Canvas canvas, String text, int x, int y, Paint mPaint) {
// Setup a textview like you normally would with your activity context
EllipsizingTextView tv = new EllipsizingTextView(mContext);

tv.setGravity(Gravity.CENTER_HORIZONTAL);
tv.setEllipsize(TruncateAt.END);
tv.setMaxLines(2);
// setup text
tv.setText(text);

// maybe set textcolor
tv.setTextColor(Color.WHITE);

// you have to enable setDrawingCacheEnabled, or the getDrawingCache will return null
tv.setDrawingCacheEnabled(true);

// we need to setup how big the view should be..which is exactly as big as the canvas
tv.measure(MeasureSpec.makeMeasureSpec(canvas.getWidth(), MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(canvas.getHeight(), MeasureSpec.EXACTLY));

// assign the layout values to the textview
tv.layout(0, 0, tv.getMeasuredWidth(), tv.getMeasuredHeight());

// draw the bitmap from the drawingcache to the canvas
canvas.drawBitmap(tv.getDrawingCache(), x, y, mPaint);

// disable drawing cache
tv.setDrawingCacheEnabled(false);
}

关于Android:如何在图像的静态布局中限制最多两行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9884875/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com