【问题标题】:Rotate text when drawing it on a bitmap在位图上绘制文本时旋转文本
【发布时间】:2013-07-31 16:04:11
【问题描述】:

我想在位图上绘制文本时将其旋转 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);

【问题讨论】:

    标签: android bitmap paint


    【解决方案1】:

    我希望这会有所帮助。这就是我创建时钟时的做法

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-14
      • 1970-01-01
      • 2011-03-11
      • 1970-01-01
      • 2017-06-13
      • 2011-07-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多