在android中将画布转换为位图图像
我试图在画布上开发一个应用程序,我正在画布上绘制位图。在绘图之后,我试图将其转换为位图图像在android中将画布转换为位图图像,android,bitmap,android-canvas,Android,Bitmap,Android Canvas,我试图在画布上开发一个应用程序,我正在画布上绘制位图。在绘图之后,我试图将其转换为位图图像 谁能给我一个建议吗?建议取决于你想做什么 如果您担心控件的绘制需要很长时间,并且希望绘制到位图,这样您就可以blit位图,而不是通过画布重新绘制,那么您不希望对平台进行双重猜测-控件会自动将其绘制缓存到临时位图,甚至可以使用 如果要使用画布绘制位图,通常的方法是: 使用创建正确大小的位图 使用构造函数创建指向此位图的画布实例 画到画布上 使用位图 因此,您可以创建一个新的位图,例如: Bitmap myB
谁能给我一个建议吗?建议取决于你想做什么 如果您担心控件的绘制需要很长时间,并且希望绘制到位图,这样您就可以blit位图,而不是通过画布重新绘制,那么您不希望对平台进行双重猜测-控件会自动将其绘制缓存到临时位图,甚至可以使用 如果要使用画布绘制位图,通常的方法是:
因此,您可以创建一个新的
位图
,例如:
Bitmap myBitmap=新位图((int)宽度,(int)高度,Config.RGB_565)
宽度和高度与画布相同
接下来,使用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 );
其他例子:
public Bitmap getBitmapNews(int item , boolean selected, int numbernews){
Bitmap bitmap;
if(selected)
bitmap=mBitmapDown[item].copy(Config.ARGB_8888, true);
else
bitmap=mBitmapUp[item].copy(Config.ARGB_8888, true);
Canvas canvas = new Canvas(bitmap);
if(numbernews<10){
canvas.drawBitmap(mNotiNews[numbernews],0,0,null);
}else{
canvas.drawBitmap(mNotiNews[0],0,0,null);
}
return bitmap;
}
公共位图getBitmapNews(int项,已选择布尔值,int numbernews){
位图;
如果(选定)
位图=mBitmapDown[item]。复制(Config.ARGB_8888,true);
其他的
bitmap=mbitmapp[item]。复制(Config.ARGB_8888,true);
画布=新画布(位图);
if(numbernews以下是将画布转换为位图并将其存储到库或特定文件夹的步骤。
注意:请确保您已授予
活动\u main.xml
<LinearLayout
android:id="@+id/linearLayout"
android:orientation="horizontal"
android:layout_margin="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<DrawingView
android:id="@+id/drawingView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
将其存储到库中
final String imagename = UUID.randomUUID().toString() + ".png";
MediaStore.Images.Media.insertImage(getContentResolver(), linearLayout .getDrawingCache(), imagename, "drawing");
转换为位图
linearLayout.setDrawingCacheEnabled(true);
linearLayout.buildDrawingCache();
Bitmap bitmap = Bitmap.createBitmap(linearLayout.getDrawingCache());
你已经得到一个位图对象,或者你想把这个画布保存到一个位图文件中?哦,太好了。最后,我可以从你的回答中了解它是如何工作的。那么,我可以兑现我的绘制位图的一些部分,并重新绘制存储在某个数组中的已准备好的位图,通过你的描述进行绘制优化吗?这将是一个好消息
linearLayout.setDrawingCacheEnabled(true);
linearLayout.buildDrawingCache();
Bitmap bitmap = Bitmap.createBitmap(linearLayout.getDrawingCache());