尝试使用画布在其上绘制文本时图像显示为黑色
我有一个位图,我正在尝试使用画布在其上写入文本。在画布中设置位图并执行必要的操作(在画布上写入文本)后,我在 ImageView 上绘制生成的画布/code>。最大的问题是没有显示图像......屏幕变黑。现在,我知道位图返回正常,因为我在进行画布操作之前显示了位图。
所以,我是这样做的:
image= (ImageView) findViewById(R.id.imageview);
bitmap = android.provider.MediaStore.Images.Media
.getBitmap(cr, selectedImage);
int heightOfOld=bitmap.getHeight();
int widthOfOld=bitmap.getWidth();
android.graphics.Bitmap.Config hasAlpha=bitmap.getConfig();
Bitmap bitmapResult=bitmap.createBitmap(widthOfOld, heightOfOld, hasAlpha);
Canvas c=new Canvas(bitmapResult);
Canvas c1=drawTextImage(c);
image.draw(c1);
这是在画布上绘制文本的方法:
private Canvas drawTextImage(Canvas c){
Paint paint=new Paint();
paint.setColor(Color.BLUE);
paint.setStyle(Paint.Style.FILL);
paint.setAntiAlias(true);
paint.setTextSize(20);
c.drawText("Golden Stag", 30, 200, paint);
return c;
}
有人能告诉我问题出在哪里吗?
I have a bitmap on which I'm trying to write a text using canvas.After setting the bitmap in a canvas and doing the necessary operations(writting a text on the canvas), I draw the resulting canvas on a ImageView
.The big problem is that there is no image displayed...the screen turns black.Now, I know that the bitmap is returned ok because I displayed right before doing the canvas operations.
So,here is how I did it:
image= (ImageView) findViewById(R.id.imageview);
bitmap = android.provider.MediaStore.Images.Media
.getBitmap(cr, selectedImage);
int heightOfOld=bitmap.getHeight();
int widthOfOld=bitmap.getWidth();
android.graphics.Bitmap.Config hasAlpha=bitmap.getConfig();
Bitmap bitmapResult=bitmap.createBitmap(widthOfOld, heightOfOld, hasAlpha);
Canvas c=new Canvas(bitmapResult);
Canvas c1=drawTextImage(c);
image.draw(c1);
And here is the method used for drawing text on the canvas:
private Canvas drawTextImage(Canvas c){
Paint paint=new Paint();
paint.setColor(Color.BLUE);
paint.setStyle(Paint.Style.FILL);
paint.setAntiAlias(true);
paint.setTextSize(20);
c.drawText("Golden Stag", 30, 200, paint);
return c;
}
Could someone tell me where is the problem,please!?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于绘制(画布)的文档说:
也许尝试:
更新:示例
我认为这应该有效(但无法测试):
Since the documentation for draw(Canvas) says:
Maybe try with:
Update: Example
I think this should work (can't test it though):