如何将xml文件转化为Bitmap

一、获取windownwidth

int windowWidth = MyApplication.getWindowWidth();

二、将布局文件转化成Bitmap

public Bitmap getScrollViewBitmap(RelativeLayout relativeLayout) {
    int h = 0;
    Bitmap bitmap;
    h = relativeLayout.getHeight();
    bitmap = Bitmap.createBitmap(windowWidth, h, Bitmap.Config.ARGB_4444);
    final Canvas canvas = new Canvas(bitmap);
    canvas.drawColor(Color.parseColor("#f2f7fa"));
    relativeLayout.draw(canvas);
    return bitmap;
}

三、保存生成的Bitmap图片到本地

 public String saveImageToGallery(Context context, Bitmap bmp) {
    String path = Environment.getExternalStorageDirectory() + "/DAYU2/Screen";
    File fileFolder = new File(path);
    if (!fileFolder.exists()) {
      fileFolder.mkdirs();
    }
    String fileName = "Screen_" + System.currentTimeMillis() + ".jpg";
    File file = new File(fileFolder, fileName);
    try {
      FileOutputStream fos = new FileOutputStream(file);
      bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
      fos.flush();
      fos.close();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
    return file.getAbsolutePath();
}

四、获取本地保存的Bitmap地址

 public String shareBitmap() {
   Bitmap xxBitmap = getScrollViewBitmap(comPictureOutRl);
   if (xxBitmap == null) {
     return null;
   }
  String s = saveImageToGallery(getContext(), xxBitmap);
   return s;
 }

猜你喜欢

转载自blog.csdn.net/qq_40837613/article/details/81979318
今日推荐