尝试使用画布在其上绘制文本时图像显示为黑色

发布于 2024-11-26 07:44:48 字数 1333 浏览 4 评论 0原文

我有一个位图,我正在尝试使用画布在其上写入文本。在画布中设置位图并执行必要的操作(在画布上写入文本)后,我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜心 2024-12-03 07:44:49

由于绘制(画布)的文档说:

“手动将此视图(及其所有子视图)渲染到给定的画布。”

也许尝试:

image.setImageBitmap(bitmapResult);

“设置位图作为此 ImageView 的内容。”

更新:示例
我认为这应该有效(但无法测试):

image = (ImageView) findViewById(R.id.imageview);
bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr, selectedImage);

Canvas c = new Canvas(bitmap);
drawTextImage(bitmap);
image.setImageBitmap(bitmap);

private Canvas drawTextImage(Bitmap b){
    Canvas c = new Canvas(b);
    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);
}

Since the documentation for draw(Canvas) says:

"Manually render this view (and all of its children) to the given Canvas."

Maybe try with:

image.setImageBitmap(bitmapResult);

"Sets a Bitmap as the content of this ImageView."

Update: Example
I think this should work (can't test it though):

image = (ImageView) findViewById(R.id.imageview);
bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr, selectedImage);

Canvas c = new Canvas(bitmap);
drawTextImage(bitmap);
image.setImageBitmap(bitmap);

private Canvas drawTextImage(Bitmap b){
    Canvas c = new Canvas(b);
    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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文