Android實(shí)現(xiàn)全屏截圖或長截屏功能
本文實(shí)例為大家分享了Android實(shí)現(xiàn)全屏截圖或長截屏功能的具體代碼,供大家參考,具體內(nèi)容如下
全屏截圖:
/** * 傳入的activity是要截屏的activity */ public static Bitmap getViewBitmap(Activity activity) { // View是你需要截圖的View View view = activity.getWindow().getDecorView(); //這兩句必須寫,否則getDrawingCache報(bào)空指針 view.setDrawingCacheEnabled(true); view.buildDrawingCache(); Bitmap b1 = view.getDrawingCache(); // 獲取狀態(tài)欄高度 Rect frame = new Rect(); activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame); int statusBarHeight = frame.top; // 獲取屏幕長和高 int width = activity.getResources().getDisplayMetrics().widthPixels; int height = activity.getResources().getDisplayMetrics().heightPixels; // 去掉標(biāo)題欄 Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height - statusBarHeight); view.destroyDrawingCache(); return b; }
ScrollView或者ListView或者LinearLayout等ViewGroup的長截圖:
public static Bitmap getViewGroupBitmap(ViewGroup viewGroup) { //viewGroup的總高度 int h = 0; Bitmap bitmap; // 適用于ListView或RecyclerView等求高度 for (int i = 0; i < viewGroup.getChildCount(); i++) { h += viewGroup.getChildAt(i).getHeight(); } // 若viewGroup是ScrollView,那么他的直接子元素有id的話,如下所示: // h = mLinearLayout.getHeight(); } // 創(chuàng)建對(duì)應(yīng)大小的bitmap(重點(diǎn)) bitmap = Bitmap.createBitmap(scrollView.getWidth(), h, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); scrollView.draw(canvas); return bitmap; }
總結(jié):
1. 布局為ScrollView,ListView,RecyclerView等能滑動(dòng)的,用for循環(huán)遍歷子元素求實(shí)際高度。
ps:ScrollView由于只能有一個(gè)直接子元素,那么我們可以直接用他的子元素來求高度。
2. 布局為LinearLayout等ViewGroup,直接.getHeight()獲取
注意:
1. getHeight(),getWidth()不能直接在avtivity生命周期中調(diào)用,因?yàn)閍ctivity尚未生成完畢之前,控件的長寬尚未測量,故所得皆為0
2. 用該方式實(shí)現(xiàn)長截屏需要注意背景色的問題,如果你的截圖背景色出了問題,仔細(xì)檢查XML文件,看看該背景色是否設(shè)置在你截屏的控件中
補(bǔ)充:
對(duì)于混合布局比如說:根RelativeLayout布局中有ViewGroup+RelativeLayout等子布局,可以分別測量他們的高度并生成bitmap對(duì)象,然后拼接在一起即可。
/** * 上下拼接兩個(gè)Bitmap, * drawBitmap的參數(shù):1.需要畫的bitmap * 2.裁剪矩形,bitmap會(huì)被該矩形裁剪 * 3.放置在canvas的位置矩形,bitmap會(huì)被放置在該矩形的位置上 * 4.畫筆 */ public static Bitmap mergeBitmap_TB_My(Bitmap topBitmap, Bitmap bottomBitmap) { int width = topBitmap.getWidth(); int height = topBitmap.getHeight() + bottomBitmap.getHeight(); Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); Rect rectTop = new Rect(0, 0, width, topBitmap.getHeight()); Rect rectBottomRes = new Rect(0, 0, width, bottomBitmap.getHeight()); RectF rectBottomDes = new RectF(0, topBitmap.getHeight(), width, height); canvas.drawBitmap(topBitmap, rectTop, rectTop, null); canvas.drawBitmap(bottomBitmap, rectBottomRes, rectBottomDes, null); return bitmap; }
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
android studio生成aar包并在其他工程引用aar包的方法
本篇文章主要介紹了android studio生成aar包并在其他工程引用aar包的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-11-11android相冊(cè)選擇圖片的編碼實(shí)現(xiàn)代碼
本篇文章主要介紹了android相冊(cè)選擇圖片的編碼實(shí)現(xiàn)代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-08-08Android LayoutInflater中 Inflate()方法應(yīng)用
本文主要介紹Android 中Inflate 方法的用法, 在開發(fā)Android應(yīng)用過程中,可以在程序中應(yīng)用 Inflate()方法加載新布局,希望能幫助有需要的朋友2016-07-07Android 日歷控件庫,可左右滑動(dòng),顯示公歷,農(nóng)歷,節(jié)假日等功能
這篇文章主要介紹了Android 日歷控件庫,可左右滑動(dòng),顯示公歷,農(nóng)歷,節(jié)假日等功能的相關(guān)資料,需要的朋友可以參考下2016-09-09Android開場動(dòng)畫類完整實(shí)現(xiàn)代碼
這篇文章主要介紹了Android開場動(dòng)畫類完整實(shí)現(xiàn)代碼,是非常實(shí)用的功能,需要的朋友可以參考下2014-07-07Kotlin中常見內(nèi)聯(lián)擴(kuò)展函數(shù)的使用方法教程
在Kotlin中,使用inline修飾符標(biāo)記內(nèi)聯(lián)函數(shù),既會(huì)影響到函數(shù)本身, 也影響到傳遞給它的Lambda表達(dá)式,這兩者都會(huì)被內(nèi)聯(lián)到調(diào)用處。下面這篇文章主要給大家介紹了關(guān)于Kotlin中常見內(nèi)聯(lián)擴(kuò)展函數(shù)的使用方法,需要的朋友可以參考下。2017-12-12