截屏应注意的细节和尝试(导出的图片一片黑色)

本文讨论了手机截图中文字显示为黑色可能的原因,指出在没有非黑色背景的情况下,黑色字体无法显现。关键在于截图时从顶层View绘制,需要确保有白色背景。解决方案包括给特定View绘制白色背景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.在手机的图片查看器是没有颜色背景的(没有黑色就显示为黑色)。而画笔的颜色是默认黑色的,所以需要一个非黑色的背景才能够看到黑色的字体。

下面是截屏的关键代码:是从顶层View绘制,所以contentView为根的子控件树不加背景也是可以看到黑色字体的,因为顶层View应该是绘制了白色背景的

static Bitmap getListViewScreenShot(Activity a){
        View view = a.getWindow().getDecorView();
        /*view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        Bitmap b1 = view.getDrawingCache();*/
        Canvas canvas = new Canvas();

        Bitmap b1 = Bitmap.createBitmap(720, 1200, Bitmap.Config.ARGB_8888);
        canvas.setBitmap(b1);
        view.draw(canvas);
        Rect frame = new Rect();
        a.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
        int statusBarHeight = frame.top;
        System.out.println(statusBarHeight);       
        int width = a.getWindowManager().getDefaultDisplay().getWidth();
        int height = a.getWindowManager().getDefaultDisplay()
                .getHeight();
        // Bitmap b = Bitmap.createBitmap(b1, 0, 25, 320, 455);
        Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height
                - statusBarHeight);
        view.destroyDrawingCache();
        return b;
    }


    public static void savePic(Bitmap b, String strFileName) {
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(strFileName);
            if (null != fos) {

                final boolean isSuccessCompress = b.compress(Bitmap.CompressFormat.PNG, 90, fos);
                if(isSuccessCompress){
                    Log.e("isSuccessCompress", "yyyyyyyy");
                }else{
                    Log.e("isSuccessCompress", "NNNNNNNN");
                }
                fos.flush();
                fos.close();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static void shootLoacleView(Activity a,String picpath) {
        savePic(getListViewScreenShot(a), picpath);
    }


而不是从顶层View开始绘制的(即不是调用decorView.draw(canvas)),而是从控件树的其他View开始绘制的,就要给该View绘制个背景canvas.drawColor(Color.WHITE),如果要看到绘制的黑色字体的话

 public static Bitmap getListViewBitmap(ListView listView,String picpath) {
        int h = 0;
        int w = 0;
        Bitmap bitmap;
  
        int count = listView.getChildCount();
        for (int i = 0; i < count; i++) {
            h += listView.getChildAt(i).getHeight();
        }

        w = listView.getWidth();
        bitmap = Bitmap.createBitmap(w, h,
                Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas();
        canvas.setBitmap(bitmap);
        canvas.drawColor(Color.WHITE);
        View view = (View)listView;
        view.draw(canvas);
        
      
        return bitmap;
    }



    public static void shootListView(ListView listView, String picpath) {
        savePic(getListViewBitmap(listView,picpath), picpath);
    }


    public static Bitmap getScrollViewBitmap(ScrollView scrollView,String picpath) {
        int h = 0;
        Bitmap bitmap;
        // 获取listView实际高度
        for (int i = 0; i < scrollView.getChildCount(); i++) {
            h += scrollView.getChildAt(i).getHeight();
        }
        Log.d(TAG, "" + h);
        Log.d(TAG, " 高度:" + scrollView.getHeight());
        // 创建对应大小的bitmap
        bitmap = Bitmap.createBitmap(scrollView.getWidth(), h,
                Bitmap.Config.ARGB_8888);
        final Canvas canvas = new Canvas(bitmap);
        canvas.drawColor(Color.WHITE);
        scrollView.draw(canvas);
       /* // 测试输出
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(picpath);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        try {
            if (null != out) {
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
                out.flush();
                out.close();
            }
        } catch (IOException e) {
        }*/
        return bitmap;
    }

    static Bitmap getTextViewBitmap(TextView tv){
      tv.setDrawingCacheEnabled(true); tv.buildDrawingCache(); Bitmap b1 = tv.getDrawingCache(); return b1; } 

static void shootTextView(TextView tv, String picPath){
 savePic(getTextViewBitmap(tv), picPath); } 

private static String TAG = "Listview and ScrollView item 截图:"; 

public static void shootScrollView(ScrollView scrollView, String picpath) { 
savePic(getScrollViewBitmap(scrollView, picpath), picpath); }


/*注意:上面是的ScrollVIew和ListView的截屏的目的是长截屏,就是没有显示出来 的部分都要截取出来。但是ListView做不到,因为ListView为了效率,把没有显示出来的不绘制,所以只能截取到ListView中显示出来的Item。而ScrollVIew没有作这个处理。*/




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值