最近业务需要把自定义的View(布局视图)转换成Bitmap输出,请各位高手支招!谢谢
最好有代码哦!!!

解决方案 »

  1.   

    View view = new CustomView();
    View.buildDrawingCache();
    Bitmap b = View.getDrawingCache();
      

  2.   


    需要先确保view已经走过了onDraw或者draw函数。下面是一段没有问题的代码。    private Bitmap getViewBitmap(View v) {
            v.clearFocus();
            v.setPressed(false);        boolean willNotCache = v.willNotCacheDrawing();
            v.setWillNotCacheDrawing(false);        // Reset the drawing cache background color to fully transparent
            // for the duration of this operation
            int color = v.getDrawingCacheBackgroundColor();
            v.setDrawingCacheBackgroundColor(0);        if (color != 0) {
                v.destroyDrawingCache();
            }
            v.buildDrawingCache();
            Bitmap cacheBitmap = v.getDrawingCache();        Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);        // Restore the view
            v.destroyDrawingCache();
            v.setWillNotCacheDrawing(willNotCache);
            v.setDrawingCacheBackgroundColor(color);        return bitmap;
        }