android 如何将视图转换为位图?

dkqlctbz  于 9个月前  发布在  Android
关注(0)|答案(7)|浏览(101)

我有两个视图(TextviewImageView)在FrameLayout,我想保存的图像与文本。为此,我转换为位图视图。
我的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>

字符串

qij5mzcb

qij5mzcb1#

如何将视图转换为位图

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

view.setDrawingCacheEnabled(true);

view.buildDrawingCache();

Bitmap bm = view.getDrawingCache();

字符串

v1l68za4

v1l68za42#

我曾经使用buildDrawingCache()方法来获取布局的位图,但是当视图很大时使用I was having trouble with it。现在我使用以下方法:

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

字符串

8cdiaqws

8cdiaqws3#

你可以使用下面的代码片段得到一个视图的位图

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

字符串

qoefvg9y

qoefvg9y4#

为什么不写一个扩展ImageView的类,重写方法onDraw,把你的图片和文本放在那里,这样更容易

4zcjmb1e

4zcjmb1e5#

首先,您需要添加依赖项

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

h5qlskok

h5qlskok6#

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线程,它确保Bitmap方法只在v.buildDrawingCache(true);. v.buildDrawingCache(true);在某些移动的中需要几毫秒的时间后才执行,这就是它在某些移动的中崩溃的原因。如果您遇到Bitmap对象的空指针异常,请尝试此解决方案。

flmtquvp

flmtquvp7#

public static Bitmap loadBitmapFromView(View view) {
    view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
    view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());

    final Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    view.draw(canvas);

    return bitmap;
}

字符串

相关问题