如何将 TextView 绘制到位图中(无需在显示器上绘制)

发布于 2024-12-02 18:01:47 字数 2182 浏览 0 评论 0原文

根据主题“将 TextView 截图为位图”可以找到很多帖子。

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

我只想从头开始创建一个 TextView,而不显示在渲染为位图的显示器上。

这是已经运行的基本配置。单击 TextView 会将其自身绘制为 Bitmap 并将其设置为 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。我从来没有运行过这个:(

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() 也不起作用。

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.

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

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);
        }
    });
}

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);

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

善良天后 2024-12-09 18:01:47

以下是如何将属于视图或从位图派生的 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

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
埖埖迣鎅 2024-12-09 18:01:47

接受的答案效果很好。它可能会绘制低质量的细节。为了避免这种使用:

yourView.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);

The accepted answer works well. It may draw in low quality detail. To avoid this use:

yourView.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文