首页 > 系统 > Android > 正文

Android实现将View保存成Bitmap的方法

2019-10-23 20:36:45
字体:
来源:转载
供稿:网友

本文实例讲述了Android实现将View保存成Bitmap的方法。分享给大家供大家参考,具体如下:

1、

public Bitmap convertViewToBitmap(View view){    Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),        Bitmap.Config.ARGB_8888);    //利用bitmap生成画布    Canvas canvas = new Canvas(bitmap);    //把view中的内容绘制在画布上    view.draw(canvas);  return bitmap;}

2、

/*** save view as a bitmap*/private Bitmap saveViewBitmap(View view) {// get current view bitmap  view.setDrawingCacheEnabled(true);  view.buildDrawingCache(true);  Bitmap bitmap = view.getDrawingCache(true);  Bitmap bmp = duplicateBitmap(bitmap);  if (bitmap != null && !bitmap.isRecycled()) { bitmap.recycle(); bitmap = null; }  // clear the cache  view.setDrawingCacheEnabled(false);  return bmp;}public static Bitmap duplicateBitmap(Bitmap bmpSrc){  if (null == bmpSrc)    { return null; }  int bmpSrcWidth = bmpSrc.getWidth();  int bmpSrcHeight = bmpSrc.getHeight();  Bitmap bmpDest = Bitmap.createBitmap(bmpSrcWidth, bmpSrcHeight, Config.ARGB_8888); if (null != bmpDest) { Canvas canvas = new Canvas(bmpDest); final Rect rect = new Rect(0, 0, bmpSrcWidth, bmpSrcHeight);  canvas.drawBitmap(bmpSrc, rect, rect, null); }  return bmpDest;}


注:相关教程知识阅读请移步到Android开发频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表