2

In my application I used canvas to draw the text. now I want to set the width of the draw text. I can set only x , y position. I can't set width and height.

My problem

update for example "India is my Country" or if any length text it goes outside of canvas that is outside of the background image. i want to print "India is

my country" if i set width mean i think it goes to next line

 @Override
protected void onDraw(Canvas canvas1)
{
Paint paint = new Paint();
        Bitmap myBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.ban_background);
        Bitmap resizeImage1=Bitmap.createScaledBitmap(myBitmap,590,350,false);
        canvas1.drawColor(Color.BLACK);
        canvas1.drawBitmap(resizeImage1,10,5, null);
        paint.setStyle(Paint.Style.FILL);
        paint.setAntiAlias(true);
        paint.setTextSize(25);
        paint.setColor(Color.BLUE);
        paint.setFakeBoldText(true);
        paint.setTextSize(20);
        paint.setStyle(Paint.Style.FILL_AND_STROKE);
        canvas1.drawText(CameraText, 100,175, paint);
        }
         }
Mercy
  • 1,862
  • 9
  • 39
  • 75
  • when you are setting the text size width and height will be automatically set, i dont think u can explicitly set the width and height. But if u could explain wot is ur real problem is i can help you. You can get the height and width of the text after setting text size like this Rect bounds = new Rect(); paint.getTextBounds(text, 0, text.length(), bounds); – Triode Feb 23 '12 at 06:29
  • update mt problem rajesh – Mercy Feb 23 '12 at 06:50
  • hope this is what you want refer my answer bro if not sorry – Triode Feb 23 '12 at 08:08

2 Answers2

0

i am not sure what you mean but you can try the setStrokeWidth(2.0f)

i hope this helps :D

Mathew Kurian
  • 5,949
  • 5
  • 46
  • 73
  • i am guessing you know the x and y of the location of the text. Just measure the length of the text which you can do by using this link (http://stackoverflow.com/questions/4794484/calculate-text-size-according-to-width-of-text-area) and then see if the x + width is beyond the scope of the canvas, then just cut the string into two and print like that (probably using some sort of recursion) something like (x+textWidth > canvas.width) then go ahead and cut the text – Mathew Kurian Feb 23 '12 at 07:38
0

i have used to create a bitmap as follows and iam able to set the width and height

 Bitmap finalImage = Bitmap.createBitmap(IMAGE_WIDTH, IMAGE_HEIGHT,
            Bitmap.Config.RGB_565);
    Bitmap tempImage = Bitmap.createBitmap(IMAGE_WIDTH / 2, IMAGE_HEIGHT,
            Bitmap.Config.RGB_565);
    Canvas g = new Canvas(finalImage);
    Canvas gtemp = new Canvas(tempImage);
    g.drawColor(Color.WHITE);
    gtemp.drawColor(Color.WHITE);
    Paint pnt = new Paint();
    pnt.setColor(Color.BLACK);

    pnt.setTextAlign(Paint.Align.CENTER);
    pnt.setTextSize(40);
    pnt.setTypeface(font1);

    g.drawText(input, IMAGE_WIDTH / 2, 40, pnt);
    Rect grct = new Rect(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT);
    Rect grctTemp = new Rect(0, 0, IMAGE_WIDTH / 2, IMAGE_HEIGHT);
    gtemp.drawBitmap(finalImage, grct, grctTemp, pnt);

`

Mercy
  • 1,862
  • 9
  • 39
  • 75
user1203673
  • 1,015
  • 7
  • 15