Johann
Johann

Reputation: 29867

Rotate text when drawing it on a bitmap

I want to rotate my text by 180 degrees when drawing it on a bitmap. The bitmap itself could also be rotated as there is nothing else drawn on it other than the text. It isn't clear to me though what I should be using in the code below to rotate the text: ImageView, canvas, paint, bitmap???

  ImageView ivImage = (ImageView)findViewById(R.id.ivImage);

  DisplayMetrics metrics = getResources().getDisplayMetrics();
  int width = metrics.widthPixels;
  int height = metrics.heightPixels;

  Bitmap.Config conf = Bitmap.Config.ARGB_8888;
  Bitmap bitmap = Bitmap.createBitmap(width, height, conf);

  Canvas canvas = new Canvas(bitmap);
  Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
  paint.setColor(Color.RED);
  paint.setTextSize((int) (200 * 1));

  // draw text to the Canvas center
  Rect bounds = new Rect();
  paint.setTextAlign(Align.CENTER);

  String text = "HELP";

  paint.getTextBounds(text, 0, text.length(), bounds);
  int x = bitmap.getWidth() / 2; //  (bitmap.getWidth() - bounds.width())/2;
  int y = bitmap.getHeight() / 2; // (bitmap.getHeight() + bounds.height())/2; 

  canvas.drawText(text, x * 1, y * 1, paint);

  ivImage.setImageBitmap(bitmap);

Upvotes: 1

Views: 1687

Answers (1)

Flaxie
Flaxie

Reputation: 540

I hope this could help. This is how i did when creating a clock

// Save canvas in current state
canvas.save();

// Rotate canvas around center and draw
canvas.rotate(degrees, canvasWidth/2, canvasHeigth/2);
canvas.drawText(text, x, y, paint)

// Restore canvas, rotates it back to original
canvas.restore();

Upvotes: 4

Related Questions