开发者

converting the bitmap to imageview

I have written my own custom view, in the custom view i have done free hand drawing using canvas.. After that i've added the custom view to linear layout.. ho开发者_StackOverflow社区w to add the custom view to image view.. please help me out...Thanks in advance...


This question isn't that clear but if you have a Bitmap and want draw it in an ImageView you just call ImageView.setImageBitmap().


In this case, you can convert your custom view to Bitmap image by using the following snippet:

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;
}

Then you can add bitmap to image view by using the following method:

imageview.setBitmapImage(returnedBitmap);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜