记录一下Android 长截屏功能

Posted 隔壁小王66

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了记录一下Android 长截屏功能相关的知识,希望对你有一定的参考价值。

需求对webview进行截屏,可以大于一屏

代码:

在setContentView之前调用

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) 
            WebView.enableSlowWholeDocumentDraw();
        

对大于5.0的版本处理,防止截屏不全。

public static Bitmap capture(WebView webView) 
        Picture picture = webView.capturePicture();
        int width = picture.getWidth();
        int height = picture.getHeight();
        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        picture.draw(canvas);
        return bitmap;
    

扩展:截屏listview,scrollview

 /**
     * 截取scrollview的屏幕
     * **/
    public static Bitmap getScrollViewBitmap(ScrollView scrollView) 
        int h = 0;
        Bitmap bitmap;
        for (int i = 0; i < scrollView.getChildCount(); i++) 
            h += scrollView.getChildAt(i).getHeight();
        
        // 创建对应大小的bitmap
        bitmap = Bitmap.createBitmap(scrollView.getWidth(), h,
                Bitmap.Config.ARGB_8888);
        final Canvas canvas = new Canvas(bitmap);
        scrollView.draw(canvas);
        return bitmap;
    
//截取超过一屏的listview
    public static Bitmap shotListView(ListView listview) 
 
        ListAdapter adapter = listview.getAdapter();
        int itemscount = adapter.getCount();
        int allitemsheight = 0;
        List<Bitmap> bmps = new ArrayList<Bitmap>();
 
        for (int i = 0; i < itemscount; i++) 
            View childView = adapter.getView(i, null, listview);
            
            childView.measure(
                    View.MeasureSpec.makeMeasureSpec(listview.getWidth(), View.MeasureSpec.EXACTLY),
                    View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
            childView.layout(0, 0, childView.getMeasuredWidth(), childView.getMeasuredHeight());
            childView.setDrawingCacheEnabled(true);
            childView.buildDrawingCache();
 
            bmps.add(childView.getDrawingCache());
            allitemsheight += childView.getMeasuredHeight();
        
        int w = listview.getMeasuredWidth();
        Bitmap bigbitmap = Bitmap.createBitmap(w, allitemsheight, Bitmap.Config.ARGB_8888);
        Canvas bigcanvas = new Canvas(bigbitmap);
 
        Paint paint = new Paint();
        int iHeight = 0;
 
        for (int i = 0; i < bmps.size(); i++) 
            Bitmap bmp = bmps.get(i);
            bigcanvas.drawBitmap(bmp, 0, iHeight, paint);
            iHeight += bmp.getHeight();
 
            bmp.recycle();
            bmp = null;
        
        return bigbitmap;
    


截取不超过一屏的listview

/**
     *  截图listview
     * **/
    public static Bitmap getListViewBitmap(ListView listView,String picpath) 
        int h = 0;
        Bitmap bitmap;
        // 获取listView实际高度
        for (int i = 0; i < listView.getChildCount(); i++) 
            h += listView.getChildAt(i).getHeight();
        
        Log.d(TAG, "实际高度:" + h);
        Log.d(TAG, "list 高度:" + listView.getHeight());
        // 创建对应大小的bitmap
        bitmap = Bitmap.createBitmap(listView.getWidth(), h,
                Bitmap.Config.ARGB_8888);
        final Canvas canvas = new Canvas(bitmap);
        listView.draw(canvas);
        return bitmap;
    

以上是关于记录一下Android 长截屏功能的主要内容,如果未能解决你的问题,请参考以下文章

android指纹识别拼图游戏仿MIUI长截屏bilibili最美创意等源码

Android长截屏-- ScrollView,ListView及RecyclerView截屏

手机截屏长屏如何截

不可以截屏?

Android自己定义截屏功能,相似QQ截屏

mac如何截屏