将位图转换为imageview

时间:2011-03-05 14:35:52

标签: android android-layout

我已经编写了自己的自定义视图,在自定义视图中我使用画布进行了自由手绘图。之后我将自定义视图添加到线性布局..如何将自定义视图添加到图像视图..请帮帮我...提前谢谢......

2 个答案:

答案 0 :(得分:7)

这个问题不是很清楚,但是如果你有一个Bitmap并希望在ImageView中绘制它,你只需要调用ImageView.setImageBitmap()

答案 1 :(得分:0)

在这种情况下,您可以使用以下代码段将自定义视图转换为Bitmap图片:

public static Bitmap getBitmapFromView(View view) {
        //Define a bitmap with the same size as the view
        Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
        //Bind a canvas to it
        Canvas canvas = new Canvas(returnedBitmap);
        //Get the view's background
        Drawable bgDrawable =view.getBackground();
        if (bgDrawable!=null) 
            //has background drawable, then draw it on the canvas
            bgDrawable.draw(canvas);
        else 
            //does not have background drawable, then draw white background on the canvas
            canvas.drawColor(Color.WHITE);
        // draw the view on the canvas
        view.draw(canvas);
        //return the bitmap
        return returnedBitmap;
}

然后,您可以使用以下方法将位图添加到图像视图:

imageview.setBitmapImage(returnedBitmap);