2

我正在尝试在使用 ShapeDrawable 在 Canvas 中显示的RectShape中显示文本。

我能够在此代码中显示 RectShape,但正在寻找如何在其上显示字符串!

  public class CustomDrawableView extends View {
  private ShapeDrawable mDrawable;

  public CustomDrawableView(Context context) {
  super(context);

  int x = 10;
  int y = 10;
  int width = 300;
  int height = 50;

  mDrawable = new ShapeDrawable(new OvalShape());
  mDrawable.getPaint().setColor(0xff74AC23);
  mDrawable.setBounds(x, y, x + width, y + height);
  }

  protected void onDraw(Canvas canvas) {
  mDrawable.draw(canvas);
  }
  }

或者

public class ShapeDrawableTest extends View {

    ShapeDrawable shapes = new ShapeDrawable();

    public ShapeDrawableTest(Context context) {
        super(context);
    }

    public ShapeDrawableTest(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        shapes.draw(canvas);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            int x = (int) event.getX(); // Use getX(int) for multi-finger
                                        // gestures
            int y = (int) event.getY();

            makeShapeDrawable(x, y);
            invalidate();
            return (true); // Handled touch event
        } else {
            return (false); // Did not handle touch event
        }
    }

    private void makeShapeDrawable(int x, int y) {
        int maxWidth = getWidth() / 10;
        int maxHeight = getHeight() / 10;
        Shape shape;

        shape = new RectShape();

        shapes = new ShapeDrawable(shape);
        int width = RandomUtils.randomInt(maxWidth) + 5;
        int height = RandomUtils.randomInt(maxHeight) + 5;
        shapes.setBounds(x - width / 2, y - height / 2, x + width / 2, y
                + height / 2);
        shapes.getPaint().setColor(Color.BLUE);

    }
}

我尝试的是:

public class CustomDrawableView extends View {
    public ShapeDrawable mDrawable;

      public CustomDrawableView(Context context) {
      super(context);

      }


        public CustomDrawableView(Context context, AttributeSet attrs) {
            super(context, attrs);
            int x = 10;
          int y = 10;
          int width = 300;
          int height = 50;

          mDrawable = new ShapeDrawable(new OvalShape());
          //mDrawable.getPaint().setColor(0xff74AC23);
          final String s = "Hello";
          Paint p = new Paint();
            Rect bounds = new Rect();

            bounds.set(x, y, x + width, y + height);
            p.setTextSize(20);
            p.setColor(Color.YELLOW);
           p.getTextBounds(s, 0, s.length(), bounds);
          mDrawable.getPaint().getTextBounds(s, 0, s.length(), bounds);
          mDrawable.setBounds(x, y, x + width, y + height);



        }
//      public CustomDrawableView(Context context, AttributeSet attrs, int defStyle) {
//          super(context, attrs, defStyle);
//
//      }

        public void onDraw(Canvas canvas) {
      mDrawable.draw(canvas);
      }
      }

截屏:

在此处输入图像描述

4

1 回答 1

1

对我来说最好的选择是创建一个自定义TextDrawable来正确处理如何显示一段文本。然后在你CustomDrawableViewonDraw(Canvas c)方法中你可以调用它来显示文本和椭圆。

看看我最近写的这个答案,因为它包含如何正确地做到这一点。

于 2014-08-21T09:06:18.630 回答