網(wǎng)站首頁 編程語言 正文
本文實例為大家分享了Android將View轉化為圖片并保存到本地的具體代碼,供大家參考,具體內容如下
一、概述
app中有需求需要將View轉化為圖片并保存到本地,這里分兩種情況:
1.View本身已經(jīng)顯示在界面上
2.View還沒有添加到界面上或者沒有顯示(繪制)過
二、實現(xiàn)方法
對于上述的第一種情況我使用下面代碼即可:
private void viewSaveToImage(View view) { ? ? ? ? view.setDrawingCacheEnabled(true); ? ? ? ? view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH); ? ? ? ? view.setDrawingCacheBackgroundColor(Color.WHITE); ? ? ? ? // 把一個View轉換成圖片 ? ? ? ? Bitmap cachebmp = loadBitmapFromView(view); ? ? ? ? FileOutputStream fos; ? ? ? ? String imagePath = ""; ? ? ? ? try { ? ? ? ? ? ? // 判斷手機設備是否有SD卡 ? ? ? ? ? ? boolean isHasSDCard = Environment.getExternalStorageState().equals( ? ? ? ? ? ? ? ? ? ? android.os.Environment.MEDIA_MOUNTED); ? ? ? ? ? ? if (isHasSDCard) { ? ? ? ? ? ? ? ? // SD卡根目錄 ? ? ? ? ? ? ? ? File sdRoot = Environment.getExternalStorageDirectory(); ? ? ? ? ? ? ? ? File file = new File(sdRoot, Calendar.getInstance().getTimeInMillis()+".png"); ? ? ? ? ? ? ? ? fos = new FileOutputStream(file); ? ? ? ? ? ? ? ? imagePath = file.getAbsolutePath(); ? ? ? ? ? ? } else ? ? ? ? ? ? ? ? throw new Exception("創(chuàng)建文件失敗!"); ? ? ? ? ? ? cachebmp.compress(Bitmap.CompressFormat.PNG, 90, fos); ? ? ? ? ? ? fos.flush(); ? ? ? ? ? ? fos.close(); ? ? ? ? } catch (Exception e) { ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? } ? ? ? ? LogUtil.e("imagePath="+imagePath); ? ? ? ? view.destroyDrawingCache(); ? ? } ? ? private Bitmap loadBitmapFromView(View v) { ? ? ? ? int w = v.getWidth(); ? ? ? ? int h = v.getHeight(); ? ? ? ? Bitmap bmp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); ? ? ? ? Canvas c = new Canvas(bmp); ? ? ? ? c.drawColor(Color.WHITE); ? ? ? ? /** 如果不設置canvas畫布為白色,則生成透明 */ ? ? ? ? v.layout(0, 0, w, h); ? ? ? ? v.draw(c); ? ? ? ? return bmp; ?}
如果是第二種,還這樣使用的話,就會報錯了,因為View在添加到容器中之前并沒有得到實際的大小,所以首先需要指定View的大小:
DisplayMetrics metric = new DisplayMetrics(); ? ? ? ? ? ? ? ? ? ? getWindowManager().getDefaultDisplay().getMetrics(metric); ? ? ? ? ? ? ? ? ? ? int width = metric.widthPixels; ? ? // 屏幕寬度(像素) ? ? ? ? ? ? ? ? ? ? int height = metric.heightPixels; ? // 屏幕高度(像素) ? ? ? ? ? ? ? ? ? ? View mingpianView = LayoutInflater.from(this).inflate(R.layout.view_team_mingpian, null, false); ? ? ? ? ? ? ? ? ? ? layoutView(mingpianView, width, height);
//然后View和其內部的子View都具有了實際大小,也就是完成了布局,相當與添加到了界面上。接著就可以創(chuàng)建位圖并在上面繪制了: ? ? private void layoutView(View v, int width, int height) { ? ? ? ? // 整個View的大小 參數(shù)是左上角 和右下角的坐標 ? ? ? ? v.layout(0, 0, width, height); ? ? ? ? int measuredWidth = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY); ? ? ? ? int measuredHeight = View.MeasureSpec.makeMeasureSpec(10000, View.MeasureSpec.AT_MOST); ? ? ? ? /** 當然,measure完后,并不會實際改變View的尺寸,需要調用View.layout方法去進行布局。 ? ? ? ? ?* 按示例調用layout函數(shù)后,View的大小將會變成你想要設置成的大小。 ? ? ? ? ?*/ ? ? ? ? v.measure(measuredWidth, measuredHeight); ? ? ? ? v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight()); ? ? }
然后再調用viewSaveToImage(mingpianView);方法即可。
原文鏈接:https://blog.csdn.net/chenzheng8975/article/details/76206290
相關推薦
- 2022-11-24 Python文件簡單操作及openpyxl操作excel文件詳解_python
- 2022-06-19 C++簡明講解類型轉換的使用與作用_C 語言
- 2021-12-19 C/C++?Qt?TabWidget?實現(xiàn)多窗體創(chuàng)建詳解_C 語言
- 2022-05-19 redis擊穿?雪崩?穿透超詳細解決方案梳理_Redis
- 2023-01-18 Android?Admob接入原理及完整教程示例_Android
- 2022-04-15 Android開發(fā)Jetpack組件Room用例講解_Android
- 2022-06-29 徹底掌握C語言strcat函數(shù)的用法_C 語言
- 2022-11-19 C#?使用?Filestream?修改大文件指定位置數(shù)據(jù)_C#教程
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支