将全可滚动的TextView位图 [英] convert full scrollable textview to bitmap

查看:140
本文介绍了将全可滚动的TextView位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个TextView转换为位图
这里我的XML code:

I'm trying to convert a textview to a Bitmap here my Xml code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@string/Rlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".ProductListAcivity" >
<TextView
       android:id="@+id/textView1"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignParentTop="true"
       android:layout_above="@+id/textView2"
       android:layout_toLeftOf="@+id/bntLinLay"
       android:layout_toRightOf="@+id/scrollView1"
       android:gravity="center_horizontal"
       android:scrollbars="vertical"
       android:text="****MIMOSA**** \n Articles :\n"
       android:typeface="monospace" />
</RelativeLayout>

然后

TextView tv = (TextView) findViewById(R.id.textView1);
tv.setMovementMethod(new ScrollingMovementMethod());
tv.setDrawingCacheEnabled(true);
tv.buildDrawingCache();
Bitmap testB = Bitmap.createBitmap(tv.getDrawingCache());

但位图的大小是的TextView的初始大小和需要我的TextView只所示的一部分,所以,我想这code与scrallable的LinearLayout和它的作品!
 我无法找到解决方案。
任何建议请!

but the bitmap size is the initial size of the TextView and takes only the shown part of my Textview ,So I tried this code with a scrallable linearLayout and it works ! I can't find the solution . Any suggestion please !

推荐答案

我已经找到你的解决方案。首先,我打算从TextView的文本获取,计算行,然后绘制它的位图。它可能会工作,但我已经尝试别的东西,效果不错。

I have found solution for you. First I was going to get text from TextView, calculate lines and then draw it on a bitmap. It would probably work but I have tried something else and it worked well.

要启用滚动TextView的人通常会做你做了什么,集滚动条:垂直并设置 ScrollingMovementMethod()

To enable scrollable TextView people usually do what you did, set scrollbars:"vertical" and set ScrollingMovementMethod().

作为一种替代方法可以简单地包裹里面滚动型例如文本视图。

As an alternative you can simply wrap your text view inside ScrollView e.g.

<ScrollView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/scrollView"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" >

    <TextView
        android:id="@+id/textView"
        android:text="@string/hello_world"
        android:gravity="center_horizontal"
        android:textSize="20sp"
        android:typeface="monospace"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</ScrollView>

那么这个code将工作

then this code will work

tv.setDrawingCacheEnabled(true);
Bitmap testB = Bitmap.createBitmap(tv.getDrawingCache());

删除行:

tv.setMovementMethod(new ScrollingMovementMethod());
tv.buildDrawingCache();

现在,让我来解释为什么这个工程。当您请求绘制缓存您根据视图边界和与这些范围内绘制的一切得到缓存。因为你的TextView匹配的RelativeLayout和画隐形文字出界,你不能在你的位图中的所有文本。

Now, let me explain why this works. When you request drawing cache you get cache based on the view bounds and with everything drawn within these bounds. Because your TextView match RelativeLayout and "draw" invisible text out of the bounds, you can't get all text in your bitmap.

在另一方面,当你把它包在滚动型的,所以缓存范围涵盖整个文本,并为你的作品不错TextView的大小相匹配的文本。

On the other hand, when you wrap it in ScrollView, TextView size is matching the text so the caching bounds cover entire text and works nice for you.

这篇关于将全可滚动的TextView位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆