当前位置:   article > 正文

Android 自定义Layout转Bitmap并保存成照片至本地_android bitmap layout

android bitmap layout

第一种:1.获取屏幕宽高像素值并添加自定义布局View(可以不再当前UI界面显示直接可生成)

  1. /**
  2. * get Screen Px
  3. */
  4. private void getCurrentScreenPx() {
  5. DisplayMetrics metric = new DisplayMetrics();
  6. getWindowManager().getDefaultDisplay().getMetrics(metric);
  7. int mScreenWidth = metric.widthPixels; // 屏幕宽度(像素)
  8. int mScreenHeight = metric.heightPixels; // 屏幕高度(像素)
  9. //get print view
  10. View mPrintView = LayoutInflater.from(this).inflate(R.layout.print, null, false);
  11. initPrintView(mPrintView);
  12. Log.d("wc_test","mPrintView-getCurrentScreenPx-->"+mScreenWidth+"--->"+mScreenHeight);
  13. Log.d("wc_test","mPrintView--->"+mPrintView.getWidth()+"--->"+mPrintView.getHeight());
  14. layoutView(mPrintView, mScreenWidth, mScreenHeight);//指定打印view大小
  15. }
  16. private void layoutView(View mPrintView, int mPrintWidth, int mPrintHeight) {
  17. //指定整个View的大小 参数是左上角 和右下角的坐标
  18. mPrintView.layout(0, 0, mPrintWidth, mPrintHeight);
  19. int measuredWidth = View.MeasureSpec.makeMeasureSpec(mPrintWidth, View.MeasureSpec.EXACTLY);
  20. int measuredHeight = View.MeasureSpec.makeMeasureSpec(mPrintHeight, View.MeasureSpec.AT_MOST);
  21. //measure完后,并不会实际改变View的尺寸,需要调用View.layout方法去进行布局
  22. //调用layout函数后,View的大小将会变成你想要设置成的大小
  23. mPrintView.measure(measuredWidth, measuredHeight);
  24. mPrintView.layout(0, 0,mPrintView.getMeasuredWidth(), mPrintView.getMeasuredHeight());
  25. }

2.进行View绘制。如果UI界面需要显示,则直接设置此bitmap即可。

  1. private Bitmap loadBitmapFromView(ScrollView mPrintScrollView) {
  2. int w = mPrintScrollView.getWidth();
  3. int h = mPrintScrollView.getHeight();
  4. Bitmap bmp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
  5. Canvas canvas = new Canvas(bmp);
  6. canvas.drawColor(Color.WHITE);
  7. /** 如果不设置canvas画布为白色,则生成透明 */
  8. mPrintScrollView.layout(0, 0, w, h);
  9. mPrintScrollView.draw(canvas);
  10. return bmp;
  11. }

2.保存照片至本地,设置完view之后,记得执行

mPrintScrollView.destroyDrawingCache();
  1. public static void savePhotoToSDCard(Bitmap photoBitmap, String path, String photoName) {
  2. if (checkSDCardAvailable()) {
  3. File dir = new File(path);
  4. if (!dir.exists()) {
  5. dir.mkdirs();
  6. }
  7. File photoFile = new File(path, photoName + ".png");
  8. FileOutputStream fileOutputStream = null;
  9. try {
  10. fileOutputStream = new FileOutputStream(photoFile);
  11. if (photoBitmap != null) {
  12. if (photoBitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream)) {
  13. fileOutputStream.flush();
  14. }
  15. }
  16. } catch (FileNotFoundException e) {
  17. photoFile.delete();
  18. e.printStackTrace();
  19. } catch (IOException e) {
  20. photoFile.delete();
  21. e.printStackTrace();
  22. } finally {
  23. try {
  24. if(fileOutputStream != null){
  25. fileOutputStream.close();
  26. }
  27. } catch (IOException e) {
  28. e.printStackTrace();
  29. }
  30. }
  31. }
  32. }
  33. /**
  34. *check sdCard
  35. */
  36. public static boolean checkSDCardAvailable() {
  37. return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
  38. }

第二种:当前view必须在视图上显示。则可以直接使用以下方式:

1.如果当前View没有超出界面

  1. private Bitmap getViewBitmap(View v) {
  2. v.clearFocus();
  3. v.setPressed(false);
  4. boolean willNotCache = v.willNotCacheDrawing();
  5. v.setWillNotCacheDrawing(false);
  6. int color = v.getDrawingCacheBackgroundColor();
  7. v.setDrawingCacheBackgroundColor(0);
  8. if (color != 0) {
  9. v.destroyDrawingCache();
  10. }
  11. v.buildDrawingCache();
  12. Bitmap cacheBitmap = v.getDrawingCache();
  13. if (cacheBitmap == null) {
  14. return null;
  15. }
  16. Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);
  17. v.destroyDrawingCache();
  18. v.setWillNotCacheDrawing(willNotCache);
  19. v.setDrawingCacheBackgroundColor(color);
  20. return bitmap;
  21. }

2.当前view超出界面

  1. public static Bitmap getBitmapByView(ScrollView scrollView) {
  2. int h = 0;
  3. Bitmap bitmap = null;
  4. for (int i = 0; i < scrollView.getChildCount(); i++) {
  5. h += scrollView.getChildAt(i).getHeight();
  6. }
  7. bitmap = Bitmap.createBitmap(scrollView.getWidth(), h,
  8. Bitmap.Config.RGB_565);
  9. final Canvas canvas = new Canvas(bitmap);
  10. scrollView.draw(canvas);
  11. return bitmap;
  12. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/262125
推荐阅读
相关标签
  

闽ICP备14008679号