如何画一个TextView成位图(而没有beeing显示屏上绘制)位图、显示屏、画一、TextView

2023-09-05 01:13:29 作者:把喜欢都给你

很多帖子都是根据题目中截图一个TextView成位图。

many posts are found according to the topic "screenshot a TextView into a Bitmap".

嗯,差我的问题是,第一个观点是绘制在显示屏上(所有的布点和测量已经完成的工作),然后绘制成连接到一个位图一个画布。

Well, the difference to my problem is, that first the view is drawn on the display (with all layouting and measuring work already done) and then drawn into a Canvas connected to a Bitmap.

我只是想从头开始创建一个TextView而没有beeing被渲染成位图显示在画面上。

I just want to create a TextView from scratch without ever beeing shown on the display which is rendered into a Bitmap.

这一个是基础配置,它已经干活。在TextView的一次点击自己绘制成位图,将其设置为ImageView的。

This one is the basis configuration which is already workin. A click on the TextView draws itself into a Bitmap and sets it to an 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,我想这一个被抽直入位图。在此之后,我将其设置为ImageView的。我从来没有这样运行:(

Now comes the problematic part. I will create a TextView in Java and I want this one to be drawn straight into a Bitmap. After this I will set this to an ImageView. I never got this running :(

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(),测量()和requestLayout()进行实验也不能工作。

This doesn't work neither in onCreate nor in a OnClickListener. Experimenting with setDrawingCacheEnabled(), measure() and requestLayout() didn't work either.

你能帮助我,请在此?

推荐答案

下面两种方法如何绘制一个TextView到画布上属于一个视图或它是由一个位图来源:

Here are two methods how to draw a TextView into canvas which belongs to a view or it is derived from a bitmap:

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