将视图转换为位图(质量问题)

时间:2014-09-03 05:53:17

标签: android image bitmap

我正在开发一个用于创建拼贴的应用程序 - 用户可以选择并加载图片到ImageViews集,然后他可以缩放和移动它们并保存它,例如到专辑。所以,我对保存图像的质量有一个问题 - 它很低!我使用转换视图到位图方法,我缩放视图以适应2000x2000大小,但是,我想,缩放不会提高图像质量 - 它就像是该视图的截图(我转换到位图),我只是缩放这个快照 - 因为它我有低质量的图像输出(

那么,我可以从视图中获取更高质量的图像,然后只是快照吗?

这是我转换视图ti位图的方法:

public static Bitmap loadBitmapFromView(ScalingFrameLayout drawingCanvas, int width, int height, Bitmap.Config config) {
        /**
         * Get previous state
         * */
        ViewGroup.LayoutParams prevParams = drawingCanvas.getLayoutParams();

        /**
         * Scale to
         * */
        drawingCanvas.setScaleX((float) width / (float) prevParams.width);
        drawingCanvas.setScaleY((float) height / (float) prevParams.height);

        /**
         * Create disarable layout size
         * */
        drawingCanvas.setLayoutParams(new RelativeLayout.LayoutParams(width, height));
        drawingCanvas.measure(
                RelativeLayout.MeasureSpec.makeMeasureSpec(drawingCanvas.getLayoutParams().width,
                        RelativeLayout.MeasureSpec.EXACTLY),
                RelativeLayout.MeasureSpec.makeMeasureSpec(drawingCanvas.getLayoutParams().height,
                        RelativeLayout.MeasureSpec.EXACTLY));
        drawingCanvas.layout(0, 0, drawingCanvas.getMeasuredWidth(),
                drawingCanvas.getMeasuredHeight());

        /**
         * Create bitmap according a size
         * */
        Bitmap overAllDrawing = Bitmap.createBitmap(width, height, config);

        /**
         * Draw layout on bitmap
         * */
        Canvas canvas = new Canvas(overAllDrawing);
        drawingCanvas.draw(canvas);

        /**
         * Set previous layout params
         * */
        drawingCanvas.setScale(1);
        drawingCanvas.setLayoutParams(prevParams);

        return overAllDrawing;
    } 

1 个答案:

答案 0 :(得分:1)

我通过缩放所有主视图子代码来解决这个问题:

/**
         * Create disarable layout size
         * */
        drawingCanvas.setLayoutParams(new RelativeLayout.LayoutParams(width, height));
        drawingCanvas.measure(
                RelativeLayout.MeasureSpec.makeMeasureSpec(drawingCanvas.getLayoutParams().width,
                        RelativeLayout.MeasureSpec.EXACTLY),
                RelativeLayout.MeasureSpec.makeMeasureSpec(drawingCanvas.getLayoutParams().height,
                        RelativeLayout.MeasureSpec.EXACTLY));
        drawingCanvas.layout(0, 0, drawingCanvas.getMeasuredWidth(),
                drawingCanvas.getMeasuredHeight());

在拍摄结果视图的快照后,我改变了主布局中所有视图的布局参数。