Convert Canvas to Bitmap

Is it possible to convert a Canvas to a Bitmap before draw? I've override onMeasure of my Custom View to implement scrolling. Canvas has shown correctly, I'd like to convert it in a bitmap for enhance speed of scrolling but using these statements

Bitmap bitmap = Bitmap.createBitmap(this.getMeasuredWidth(),this.getMeasuredHeight(),bitmap.Config.ARGB_8888); 
Canvas canvas = new Canvas(bitmap);

Log cat says

09-15 21:54:36.926: E/AndroidRuntime(28919): FATAL EXCEPTION: main
09-15 21:54:36.926: E/AndroidRuntime(28919): java.lang.RuntimeException: Unable to start activity     ComponentInfo{zardoni.matteo.timeline/zardoni.matteo.AppAndroid.Activities.TimelineActivity}: java.lang.IllegalArgumentException: width and height must be > 0

Is there a way to convert the Canvas into a Bitmap via createBitmap() without know width/height? I know this values only after finish drawing on the Canvas.

Thanks


To convert from canvas to Bitmap, just create an Bitmap and add it to your canvas, then draw on canvas and everything will be appear on your Bitmap

In your case, the log cat say clearly: your function: getMeasuredWidth and getMeasuredHeight return a value that be < 0 Check this.

链接地址: http://www.djcxy.com/p/67624.html

上一篇: 从画布到位图以改善渲染

下一篇: 将画布转换为位图