如何将TextView绘制到位图中(而不必在显示器上绘制)

9
根据“将TextView截图成位图”主题,找到了很多帖子。然而,我的问题与之不同,首先在显示器上绘制视图(所有布局和测量工作已完成),然后将其绘制到连接到位图的画布中。我只想从头开始创建一个TextView,而不必显示在屏幕上,最后将其渲染到位图中。这是基本配置,已经可以正常工作。点击TextView将其绘制到位图中,并将其设置为ImageView。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:orientation="vertical" android:background="#fff">

    <TextView android:id="@+id/tv" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:text="The Quick Brown Fox Jumps Over The Lazy Dog."
        android:textSize="20dip" android:background="#abcdef"
        android:textColor="#000" android:padding="10dip"
        android:layout_margin="10dip" />

    <ImageView android:id="@+id/iv" android:layout_width="449px"
        android:layout_height="47px" android:background="#56789a"
        android:layout_margin="10dip" />
</LinearLayout>

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    findViewById(R.id.tv).setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Bitmap bmp = Bitmap.createBitmap(449, 47, Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bmp);

            v.draw(canvas);

            ImageView iv = (ImageView) findViewById(R.id.iv);
            iv.setImageBitmap(bmp);
        }
    });
}

现在来到了问题所在。我将在Java中创建一个TextView,希望它能直接绘制到Bitmap中。之后我会将其设置到ImageView中。但是我从未成功过 :(

Bitmap bmp = Bitmap.createBitmap(449, 47, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bmp);

TextView tv = new TextView(this);
tv.setText("THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG");
tv.setTextSize(55f);
tv.setTextColor(this.getResources().getColor(android.R.color.black));
tv.draw(canvas);

ImageView iv = (ImageView) findViewById(R.id.iv);
iv.setImageBitmap(bmp);

无论是在onCreate还是OnClickListener中,这都不起作用。尝试使用setDrawingCacheEnabled(),measure()和requestLayout()也没有效果。

2个回答

20

以下是两种方式,可以将一个TextView绘制到一个视图或从位图派生出来的画布上:

//method 1
TextPaint tp = new TextPaint(TextPaint.ANTI_ALIAS_FLAG); 
tp.setColor(Color.WHITE);
tp.setTextSize(30);
tp.setShadowLayer(5, 2, 2, Color.CYAN);
StaticLayout sl=new StaticLayout("This is the first sample text 
        which will be wrapped within the text box.",tp,300,
      Layout.Alignment.ALIGN_NORMAL, 1f,0f,false); 

canvas.save();
canvas.translate(50, 20); //position text on the canvas
sl.draw(canvas);
canvas.restore();

//method 2
TextView textView = new TextView(StartActivity.this); 
textView.layout(0, 0, 300, 500); //text box size 300px x 500px
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, 30);
textView.setTextColor(Color.WHITE);
textView.setShadowLayer(5, 2, 2, Color.CYAN); //text shadow
textView.setText("This is the second sample 
         text which will be wrapped within the text box."); 
textView.setDrawingCacheEnabled(true); 
canvas.drawBitmap(textView.getDrawingCache(), 50, 200, null); 
    //text box top left position 50,50

自API 28起,textView.setDrawingCacheEnabled(true)已被弃用。现在更推荐使用像其他类型的View一样将视图绘制到画布上的方法。 - omiwrench

0

被接受的答案很好。它可能会引入低质量的细节。为了避免这种情况,请使用:

yourView.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);

setDrawingCacheQuality 在API28中已废弃。 - Abhishek Dutt
尽管它从28版本开始被弃用,但在32版本中仍然可以编译,因此现在甚至未来几年仍然有用,因为向后兼容性很重要,许多旧代码仍然可以运行。所以你是对的,Abhishek,但目前这不是一个大问题。 - Daniel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接