Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/192.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Android画布中以矩形或椭圆形显示文本?_Android_Android Canvas_Android View_Android Shape - Fatal编程技术网

如何在Android画布中以矩形或椭圆形显示文本?

如何在Android画布中以矩形或椭圆形显示文本?,android,android-canvas,android-view,android-shape,Android,Android Canvas,Android View,Android Shape,我正在尝试显示文本,其中使用ShapeDrawable在画布中显示 我可以在这段代码中显示RectShape,但是我想知道如何在上面显示字符串 public class CustomDrawableView extends View { private ShapeDrawable mDrawable; public CustomDrawableView(Context context) { super(context); int x = 10; int y = 10;

我正在尝试显示文本,其中使用ShapeDrawable在画布中显示

我可以在这段代码中显示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);
      }
      }
屏幕截图:

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);
      }
      }

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


请看一看,因为它包含如何正确执行此操作。

感谢您指出解决方案!但不幸的是,像setImageDrawable这样的setShapeDrawable不能用于Shape,包括ShapeDrawable在内的OvalShape!在上述代码mDrawable reference中,没有可用于设置可绘制内容的方法!很抱歉我没有更好的解释!但是这个想法是在您的自定义视图(CustomDrawableView)中,将LevelListDrawable与TextDrawable和ShapeDrawable结合使用!我现在正在工作,但如果你需要更多帮助,我可以稍后在家准备一个例子;)我的错我说了LevelListDrawable,我想说LayerDrawable()!闷闷不乐!我的错,自从你上次评论我就出去了!让我准备一个Android应用程序来展示它(当我有东西时我会更新答案)!在
mDrawable.draw(画布)之后的
onDraw
方法中
,添加
canvas.drawText(…)
。你想这样做吗?为什么你想“合并”ShapeDrawable中的文本,而不只是在上面画图(相对容易)?如果你需要一个可绘制的界面,你可以改为在位图上绘制ShapeDrawable和文本,然后在BitmapDrawable中转换位图。你想要的是很容易实现的,只需要使用框架默认提供的东西。为什么您希望/需要推出自己的定制可绘制/视图?