将画布转换为android中的位图图像

我正在尝试在画布上开发一个应用程序,我在画布上绘制一个位图。绘制后,我试图转换为位图图像。

任何人都可以给我一个建议。

先感谢您。


HUH函数
浏览 439回答 3
3回答

慕森卡

建议取决于你想要做什么。如果您担心控件需要很长时间才能绘制,并且您想要绘制位图以便您可以通过blit重新绘制位图而不是通过画布重新绘制,那么您不希望双重猜测平台 - 控件自动将其绘图缓存到临时位图,甚至可以使用控件从控件中获取这些位图getDrawingCache()如果要使用画布绘制位图,通常的配方是:使用创建正确大小的位图 Bitmap.createBitmap()使用Canvas(Bitmap)构造函数创建指向此位图的画布实例画到画布上使用位图

元芳怎么了

所以你创建一个新的Bitmap,例如:Bitmap myBitmap = new Bitmap( (int)Width, (int)Height, Config.RGB_565 )同width和height是一样的画布。接下来,使用canvas.setBitmap(myBitmap),但不是drawBitmap()。在你打电话之后setBitmap,你在画布上绘制的所有内容实际上都是myBitmap通过我所说明的示例代码来实现的。编辑:您无法直接创建位图,例如:Bitmap myBitmap = new Bitmap( (int)Width, (int)Height, Config.RGB_565 );你必须使用:Bitmap myBitmap = Bitmap.createBitmap( (int)Width, (int)Height, Config.RGB_565 );

慕雪6442864

其他例子:public Bitmap getBitmapNews(int item , boolean selected, int numbernews){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; Bitmap bitmap;&nbsp; &nbsp; &nbsp; &nbsp; if(selected)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bitmap=mBitmapDown[item].copy(Config.ARGB_8888, true);&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bitmap=mBitmapUp[item].copy(Config.ARGB_8888, true);&nbsp; &nbsp; &nbsp; &nbsp; Canvas canvas = new Canvas(bitmap);&nbsp; &nbsp; &nbsp; &nbsp; if(numbernews<10){&nbsp; &nbsp; &nbsp; &nbsp; canvas.drawBitmap(mNotiNews[numbernews],0,0,null);&nbsp; &nbsp; &nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; canvas.drawBitmap(mNotiNews[0],0,0,null);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;return bitmap;&nbsp;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android