Android對(duì)界面上指定view進(jìn)行截圖

都不喜歡廢話,網(wǎng)上方法也一堆,這里我就記錄一下,emmmmm

/**
 *此方法直接截取屏幕指定view區(qū)域的內(nèi)容
 * @param view 需要截取屏幕的圖片view
 * @return Bitmap 
 */
private Bitmap getScreenshot2Bitmap(View view) throws Exception {
    View screenView = getWindow().getDecorView();
    screenView.setDrawingCacheEnabled(false);
    screenView.buildDrawingCache();
    //獲取屏幕整張圖片
    Bitmap screenBitmap = screenView.getDrawingCache();
    if (screenBitmap != null) {
        //需要截取的長(zhǎng)和寬
        int outWidth = view.getWidth();
        int outHeight = view.getHeight();
        //獲取需要截圖部分的在屏幕上的坐標(biāo)(view的左上角坐標(biāo))
        int[] viewLocationArray = new int[2];
        view.getLocationOnScreen(viewLocationArray);
        //從屏幕截圖中截取指定區(qū)域
        screenBitmap = Bitmap.createBitmap(bitmap, viewLocationArray[0], viewLocationArray[1], outWidth, outHeight);
        Toast.makeText(context, "截圖成功", Toast.LENGTH_SHORT).show();
        view.setDrawingCacheEnabled(false);  
    }
        //記得加上,不然重復(fù)生成時(shí) 返回的還是第一次生成的bitmap截圖
        screenView.destroyDrawingCache();
    return screenBitmap ;
}
/**
*此方法只能截取指定view區(qū)域上的內(nèi)容,如果view上還有其他view,則無(wú)法截取
 * 先測(cè)量和布局,再生成Bitmap
 */
public static Bitmap getBitmap(View view) {
    // 測(cè)量
    int widthSpec = View.MeasureSpec.makeMeasureSpec(screenWidth, View.MeasureSpec.AT_MOST);
    int heightSpec = View.MeasureSpec.makeMeasureSpec(screenHeight, View.MeasureSpec.AT_MOST);
    view.measure(widthSpec, heightSpec);
    // 布局
    int measuredWidth = view.getMeasuredWidth();
    int measuredHeight = view.getMeasuredHeight();
    view.layout(0, 0, measuredWidth, measuredHeight);
    // 繪制
    int width = view.getWidth();
    int height = view.getHeight();
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
    Canvas canvas = new Canvas(bitmap);
    view.draw(canvas);
    return bitmap;
}

下一步操作就是保存圖片到本地或操作圖片.....

//todo 有空可以看下這幾個(gè)方法 控制生成圖片的
setDrawingCacheEnabled(boolean enabled)
buildDrawingCache()
getDrawingCache()
destroyDrawingCache()

WebView 截取長(zhǎng)圖:
直接上代碼了 穩(wěn)穩(wěn)的 屏幕外也可以實(shí)現(xiàn)截取

public static void getViewBitmap(View view) {
    WebView webview = (WebView) view;
    //測(cè)量webview 實(shí)際的大小 包括屏幕外的
    webview.measure(View.MeasureSpec.makeMeasureSpec(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED),
            View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
    webview.layout(0, 0, webview.getMeasuredWidth(), webview.getMeasuredHeight());
    //啟用View的DrawingCache功能
    webview.buildDrawingCache(true);
    webview.setDrawingCacheEnabled(true);
    webview.setVerticalScrollBarEnabled(false);
    webview.buildDrawingCache();
    // 延時(shí)保存圖片 必須延時(shí)不然會(huì)出現(xiàn)截取部分白屏 
    new Handler().postDelayed(() -> {
        Bitmap bitmap =convertViewToBitmap(webview, webview.getMeasuredWidth(), webview.getMeasuredHeight());
        Canvas canvas = new Canvas(bitmap);
        // 畫(huà)布的寬高和 WebView 的網(wǎng)頁(yè)保持一致
        Paint paint = new Paint();
        canvas.drawBitmap(bitmap, 0, webview.getMeasuredHeight(), paint);
        webview.draw(canvas);
        try {
            //保存bitmap 為本地圖片或做其他處理
            saveFile(webview, bitmap);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }, 500);
}

/**
 *  繪制view的bitmap 
 * @param view  要繪制的view 
 * @param bitmapWidth  繪制寬  傳入實(shí)際寬高
 * @param bitmapHeight 繪制高
 * @return
 */
private static Bitmap convertViewToBitmap(View view, int bitmapWidth, int bitmapHeight) {
    Bitmap bitmap = null;
    try {
        bitmap = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Bitmap.Config.ARGB_4444);
        view.draw(new Canvas(bitmap));
    } catch (OutOfMemoryError ooe) {
        System.gc();
        return bitmap;
    }
    return bitmap;
}
注意事項(xiàng) : 
1.webview 在截取完成后 會(huì)失去焦點(diǎn) 不能在滾動(dòng)了 (處理方法:reload  或手動(dòng)滾動(dòng)webview)
2.webview 會(huì)截取不到部分三方框架繪制的圖 (可轉(zhuǎn)換成image 后在進(jìn)行截取)
3. 獲取圖片后 切記 執(zhí)行webview.buildDrawingCache(false);
注:此方向法系轉(zhuǎn)載
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容