adrian
adrian

Reputation: 4594

image appears black when trying drawing a text on it using canvas

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!?

Upvotes: 1

Views: 1012

Answers (1)

Tomas
Tomas

Reputation: 1419

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);
}

Upvotes: 1

Related Questions