码迷,mamicode.com
首页 > 其他好文 > 详细

view存成bitmap

时间:2016-10-01 17:06:49      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:

  1. public static String saveView2Bitmap(Context context,View view,String fileName) {
  2. Bitmap bitmap = null;
  3. if(context == null || view == null || TextUtils.isEmpty(fileName)){
  4. return null;
  5. }
  6. view.setDrawingCacheEnabled(true);
  7. // view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
  8. view.buildDrawingCache();
  9. bitmap = view.getDrawingCache();
  10. if(bitmap == null){
  11. Toast.makeText(context,context.getString(R.string.wallpaper_failed_to_crop),Toast.LENGTH_SHORT).show();
  12. return null;
  13. }
  14. String ret = null;
  15. OutputStream fos = null;
  16. try {
  17. Bitmap resizeBitmap = zoomBitmap(bitmap,0.5f,0.5f);
  18. bitmap.recycle();
  19. Uri uri = Uri.fromFile(new File(WallpaperSettingUtils.getTempSaveDir() + fileName));
  20. File file = new File(uri.getPath());
  21. if (!file.exists()) {
  22. try {
  23. file.createNewFile();
  24. } catch (IOException e) {
  25. Toast.makeText(context,context.getString(R.string.wallpaper_failed_to_crop),Toast.LENGTH_SHORT).show();
  26. return null;
  27. }
  28. }
  29. fos = context.getContentResolver().openOutputStream(uri);
  30. // 这里也可以是 Bitmap.CompressFormat.PNG, 可以保持透明背景
  31. resizeBitmap.compress(Bitmap.CompressFormat.JPEG, 80, fos);
  32. ret = WallpaperSettingUtils.getTempSaveDir() + fileName;
  33. } catch (Exception e) {
  34. Toast.makeText(context,context.getString(R.string.wallpaper_failed_to_crop),Toast.LENGTH_SHORT).show();
  35. }finally {
  36. Util.closeSilently(fos);
  37. view.setDrawingCacheEnabled(false);
  38. }
  39. return ret;
  40. }
  41. /**
  42. * 将图片按指定比例缩放
  43. */
  44. public static Bitmap zoomBitmap(Bitmap bitmap, float scaleWidth, float scaleHeight) {
  45. int width = bitmap.getWidth();
  46. int height = bitmap.getHeight();
  47. Matrix matrix = new Matrix();
  48. // float scaleWidth = ((float) w / width);
  49. // float scaleHeight = ((float) h / height);
  50. matrix.postScale(scaleWidth, scaleHeight);
  51. Bitmap newBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
  52. return newBitmap;
  53. }
  54. public static Bitmap convertViewToBitmap(View view){
  55. view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
  56. view.buildDrawingCache();
  57. Bitmap bitmap = view.getDrawingCache();
  58. return bitmap;
  59. }
  60. //---------------------
  61. public static String saveView2BitmapWithoutZoom(Context context,View view,String fileName) {
  62. Bitmap bitmap = null;
  63. if(context == null || view == null || TextUtils.isEmpty(fileName)){
  64. return null;
  65. }
  66. view.setDrawingCacheEnabled(true);
  67. view.buildDrawingCache();
  68. bitmap = view.getDrawingCache();
  69. if(bitmap == null){
  70. Toast.makeText(context,context.getString(R.string.wallpaper_failed_to_crop),Toast.LENGTH_SHORT).show();
  71. return null;
  72. }
  73. String ret = null;
  74. OutputStream fos = null;
  75. try {
  76. Uri uri = Uri.fromFile(new File(WallpaperSettingUtils.getTempSaveDir() + fileName));
  77. File file = new File(uri.getPath());
  78. if (!file.exists()) {
  79. try {
  80. file.createNewFile();
  81. } catch (IOException e) {
  82. Toast.makeText(context,context.getString(R.string.wallpaper_failed_to_crop),Toast.LENGTH_SHORT).show();
  83. return null;
  84. }
  85. }
  86. fos = context.getContentResolver().openOutputStream(uri);
  87. bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
  88. ret = WallpaperSettingUtils.getTempSaveDir() + fileName;
  89. } catch (Exception e) {
  90. Toast.makeText(context,context.getString(R.string.wallpaper_failed_to_crop),Toast.LENGTH_SHORT).show();
  91. }finally {
  92. Util.closeSilently(fos);
  93. view.destroyDrawingCache();
  94. view.setDrawingCacheEnabled(false);
  95. bitmap.recycle();
  96. }
  97. return ret;
  98. }





view存成bitmap

标签:

原文地址:http://www.cnblogs.com/ywq-come/p/5925985.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!