我的应用里面有一个需求,将一个画面分享出去,这个画面底层是一个View,所以首先要把这个View转换成Bitmap,然后在分享这个bitmap即可。话不多说,直接上代码。

有个地方需要注意一下:就是//Draw background的代码不能省略,否则生成的图片背景就是黑色的了。

==

public static Bitmap getBitmapFromView(View v) {
    Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.RGB_565);
    Canvas c = new Canvas(b);
    v.layout(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
    // Draw background
    Drawable bgDrawable = v.getBackground();
    if (bgDrawable != null)
        bgDrawable.draw(c);
    else
        c.drawColor(Color.WHITE);
    // Draw view to canvas
    v.draw(c);
    return b;
}

==

相关文章:

  • 2022-12-23
  • 2021-09-22
  • 2021-04-27
  • 2022-12-23
  • 2021-07-28
  • 2021-12-21
  • 2022-12-23
  • 2021-08-17
猜你喜欢
  • 2022-12-23
  • 2022-02-01
  • 2022-12-23
  • 2021-09-07
  • 2021-10-12
  • 2021-06-20
  • 2022-12-23
相关资源
相似解决方案