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

dkqlctbz  于 2024-01-04  发布在  Android
关注(0)|答案(7)|浏览(154)

我有两个视图(TextviewImageView)在FrameLayout,我想保存的图像与文本。为此,我转换为位图视图。
我的xml是:

  1. <FrameLayout
  2. android:id="@+id/framelayout"
  3. android:layout_marginTop="30dip"
  4. android:layout_height="fill_parent"
  5. android:layout_width="fill_parent">
  6. <ImageView
  7. android:id="@+id/ImageView01"
  8. android:layout_height="wrap_content"
  9. android:layout_width="wrap_content"/>
  10. <TextView android:id="@+id/text_view"
  11. android:layout_marginTop="30dip"
  12. android:layout_width="wrap_content"
  13. android:maxLines="20"
  14. android:scrollbars="vertical"
  15. android:layout_height="wrap_content"/>
  16. </FrameLayout>

字符串

qij5mzcb

qij5mzcb1#

如何将视图转换为位图

  1. FrameLayout view = (FrameLayout)findViewById(R.id.framelayout);
  2. view.setDrawingCacheEnabled(true);
  3. view.buildDrawingCache();
  4. Bitmap bm = view.getDrawingCache();

字符串

v1l68za4

v1l68za42#

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

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

字符串

8cdiaqws

8cdiaqws3#

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

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

字符串

qoefvg9y

qoefvg9y4#

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

4zcjmb1e

4zcjmb1e5#

首先,您需要添加依赖项

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

字符串
然后将布局转换为位图

  1. RelativeLayout pdfmain;
  2. Layout_to_Image layout_to_image;
  3. Bitmap mBitmap;
  4. layout_to_image = new Layout_to_Image(AllotmentDoc.this, pdfmain);
  5. mBitmap = layout_to_image.convert_layout();

h5qlskok

h5qlskok6#

  1. FrameLayout v = (FrameLayout)findViewById(R.id.frme1);
  2. v.setDrawingCacheEnabled(true);
  3. // this is the important code :)
  4. // Without it the view will have a dimension of 0,0 and the bitmap will be null
  5. v.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
  6. View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
  7. v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
  8. v.buildDrawingCache(true);
  9. v.post(new Runnable() {
  10. @Override
  11. public void run() {
  12. // Bitmap b = v.getDrawingCache();
  13. Bitmap b = Bitmap.createBitmap(v.getDrawingCache());
  14. v.setDrawingCacheEnabled(false); // clear drawing cache
  15. Log.e("ss","ss"+b.getHeight());
  16. }
  17. });

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

展开查看全部
flmtquvp

flmtquvp7#

  1. public static Bitmap loadBitmapFromView(View view) {
  2. view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
  3. view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
  4. final Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
  5. Canvas canvas = new Canvas(bitmap);
  6. view.draw(canvas);
  7. return bitmap;
  8. }

字符串

相关问题