在位图上绘制文本时旋转文本

时间:2013-07-31 07:39:58

标签: android bitmap paint

我想在位图上绘制时将文本旋转180度。位图本身也可以旋转,因为除了文本之外没有其他任何东西绘制。我不清楚我在下面的代码中应该使用什么来旋转文本: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);

1 个答案:

答案 0 :(得分:3)

我希望这可以提供帮助。这就是我在创建时钟时的表现

// 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();