Android 屏幕截图黑屏问题解决方法

以下两种截屏方法可能由于尺寸没有设置默认过大系统不予处理。

 public static Bitmap convertViewToBitmap(View view, int bitmapWidth, int bitmapHeight){
        Bitmap bitmap =   Bitmap.createBitmap(bitmapWidth, bitmapHeight, Bitmap.Config.ARGB_8888);
        view.draw(new Canvas(bitmap));

        return bitmap;
    }
public static Bitmap convertViewToBitmap(View view){
       view.buildDrawingCache();
    Bitmap bitmap = view.getDrawingCache();
    return bitmap; 
}

其实只需设置下尺寸就行了。

public static Bitmap convertViewToBitmap(View view){
     //根据父容器计算该view适合大小
      view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
        view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
        view.buildDrawingCache();
        Bitmap bitmap = view.getDrawingCache();

     return bitmap;
}

你可能感兴趣的:(Android开发)