如何将视图转换为位图?

36

我有两个视图(TextViewImageView)在 FrameLayout 中,我想保存图像和文本。为此,我将 View 转换为位图。

我的 XML 代码如下:

<FrameLayout 
     android:id="@+id/framelayout"
     android:layout_marginTop="30dip"
     android:layout_height="fill_parent" 
     android:layout_width="fill_parent">

     <ImageView 
          android:id="@+id/ImageView01"
          android:layout_height="wrap_content" 
          android:layout_width="wrap_content"/>

    <TextView android:id="@+id/text_view"
          android:layout_marginTop="30dip"
          android:layout_width="wrap_content" 
          android:maxLines="20"
          android:scrollbars="vertical"
          android:layout_height="wrap_content"/>

</FrameLayout>
6个回答

85

如何将View转换为Bitmap

FrameLayout view = (FrameLayout)findViewById(R.id.framelayout);

view.setDrawingCacheEnabled(true);

view.buildDrawingCache();

Bitmap bm = view.getDrawingCache();

2
@CapDroid 如果我需要获取带有透明背景的位图怎么办? - Fahid Nadeem
我们可以做与之完全相反的操作吗?我的意思是,我们能够再次将位图转换为视图吗? - Sahil Garg
你可以在ImageView或任何视图的背景上设置位图。 - Niranj Patel
1
值得注意的是,这个位图随时可以被处理,因此将位图复制到您自己的位图中更加安全。来源:http://arpitonline.com/2012/07/17/capturing-bitmaps-of-views-in-android/ - Nursultan Talapbekov
@NiranjPatel 我尝试了上面的代码,但它返回的位图大小与屏幕大小相同。 - PriyankaChauhan
1
以前,getDrawingCache() 返回 null。然后我找到了这个答案:https://dev59.com/UHE95IYBdhLWcg3wXsyR#4618030 - Amiraslan

24

我曾经使用buildDrawingCache()方法来获取我的布局的位图,但是当视图很大时,我遇到了一些问题。现在我使用以下方法:

FrameLayout view = findViewById(R.id.framelayout);
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);

1
谢谢!我整整一周都在寻找解决大视图问题的方法,这个解决方案完美地解决了我的问题! - heloisasim
谢谢你!它对于大视图转换为位图非常有效。 - babu
java.lang.IllegalArgumentException: 宽度和高度必须大于0 - Vishal Yadav
@V.Y. 这个答案假设视图已经被布局好了。如果没有,那么你需要通过编程的方式创建它(而不是使用 findViewById)。 - Suragch
如果您不将视图添加到活动中,只需通过引用布局ID进行转换。 - undefined

3

你可以使用以下代码片段获取视图的位图:

mView.setDrawingCacheEnabled(true);
mView.getDrawingCache();

1
为什么不编写一个扩展 ImageView 的类,并覆盖 onDraw 方法,将您的图像和文本放置在那里,这样更容易些。

我尝试了这个,但是得到的图像仍然没有文本。int bw = originalBitmap.getWidth(); int bh = originalBitmap.getHeight(); nb = Bitmap.createScaledBitmap(originalBitmap,bw,bh,true);c = new Canvas(nb);Paint paint = new Paint() c.drawText(my_text, 0, 0, paint); - RajaReddy PolamReddy

0

首先,您需要添加依赖项

implementation 'com.github.vipulasri.layouttoimage:library:1.0.0'

然后将布局转换为位图

RelativeLayout pdfmain;
Layout_to_Image layout_to_image;
Bitmap mBitmap;



    layout_to_image = new Layout_to_Image(AllotmentDoc.this, pdfmain);
    mBitmap = layout_to_image.convert_layout();

0
    FrameLayout v = (FrameLayout)findViewById(R.id.frme1);

    v.setDrawingCacheEnabled(true);

    // this is the important code :)
  // Without it the view will have a dimension of 0,0 and the bitmap will be null
    v.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
            View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
    v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());

    v.buildDrawingCache(true);

    v.post(new Runnable() {
        @Override
        public void run() {
           // Bitmap b = v.getDrawingCache();
            Bitmap b = Bitmap.createBitmap(v.getDrawingCache());
            v.setDrawingCacheEnabled(false); // clear drawing cache
            Log.e("ss","ss"+b.getHeight());
        }
    });

在这里,我添加了一个post Runnable线程,它确保createBitmap方法仅在v.buildDrawingCache(true);执行后执行。在某些手机上,v.buildDrawingCache(true);需要几毫秒的时间,这就是它在某些手机上崩溃的原因。如果您遇到Bitmap对象的空指针异常,请尝试此解决方案。

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