我已经动态创建了布局和 textview,并将 textview 宽度设置为某个值,并将 hieght 设置为 fill_parent。
在做的时候
tv = new TextView(this);
tv.setLayoutParams(new LayoutParams(500, LayoutParams.WRAP_CONTENT));
tv.setText("Hello world how are you whats up whats up hows life is good oh great");
tv.setTextColor(Color.BLACK);
tv.setBackgroundColor(Color.TRANSPARENT);
tv.setTextSize(24);
ll.addView(tv);
this.setContentView(ll);
它很好地显示内容并正确包装
你好,世界,你好吗,怎么样,生活多么美好,哦,太棒了
但是当我尝试提取此文本视图的宽度和高度并尝试创建相同宽度和相同高度的图像时,它会通过部分切割正确单词 y 来生成更小宽度的图像。
bitmap = Bitmap.createBitmap(tv.getWidth(),tv.getHeight(),Config.RGB_565);
canvas=new Canvas(bitmap);
canvas.drawARGB(255,0,255,0);
paint.setStyle(Style.FILL);
paint.setColor(Color.BLUE);
paint.setAntiAlias(true);
paint.setTypeface(Typeface.SERIF);
paint.setTextSize((int) 24);
canvas.drawText(tv.getText().toString().substring(0,44),0,27, paint);
canvas.drawText(tv.getText().toString().substring(tv.getLayout().getLineStart(1),tv.getLayout().getLineStart(2)),0,48, paint);
canvas.drawText(tv.getText().toString().substring(tv.getLayout().getLineStart(2),tv.getLayout().getLineStart(3)),0,48+24, paint);
canvas.save();
有什么问题。
我也试过这个
bitmap = Bitmap.createBitmap(tv.getWidth(),tv.getHeight(),Config.RGB_565);
canvas=new Canvas(bitmap);
canvas.drawARGB(255,0,255,0);
final TextPaint paint = new TextPaint(Color.BLUE);
paint.setStyle(Style.FILL);
paint.setAntiAlias(true);
paint.setTypeface(Typeface.SERIF);
paint.setTextSize((int) 24);
canvas.save();
StaticLayout sl= new StaticLayout(tv.getText().toString(), paint, bitmap.getWidth(), android.text.Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
sl.draw(canvas);
但结果我得到了一条切割文本的线和一个下划线。请指教