Android 如何将文本视图绘制成位图(不在显示器上绘制)

Android 如何将文本视图绘制成位图(不在显示器上绘制),android,canvas,bitmap,textview,Android,Canvas,Bitmap,Textview,许多帖子都是根据主题“截图将文本视图转换成位图”来找到的 我的问题的不同之处在于,首先在显示器上绘制视图(所有布局和测量工作都已完成),然后绘制到连接到位图的画布上 我只想从头开始创建一个TextView,而不必在显示屏幕上显示,该屏幕被渲染成位图 这是已在运行的基本配置。单击TextView会将自身绘制成位图,并将其设置为ImageView <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="

许多帖子都是根据主题“截图将文本视图转换成位图”来找到的

我的问题的不同之处在于,首先在显示器上绘制视图(所有布局和测量工作都已完成),然后绘制到连接到位图的画布上

我只想从头开始创建一个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);
        }
    });
}

这在onCreate和OnClickListener中都不起作用。尝试setDrawingCacheEnabled()、measure()和requestLayout()也不起作用。

以下两种方法可以将文本视图绘制到属于视图或从位图派生的画布中:

//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

textView.setDrawingCacheEnabled(true)
自API 28以来已被弃用。现在首选的方法是像其他类型的视图一样将视图绘制到画布上。
//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