如何使用在RectShape或OvalShape显示Android的画布ShapeDrawable文本? [英] How to display Text in Android Canvas ShapeDrawable with in the RectShape or OvalShape?

查看:260
本文介绍了如何使用在RectShape或OvalShape显示Android的画布ShapeDrawable文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示 文字RectShape 它采用ShapeDrawable在画布显示。

I'm trying to display Text in RectShape which uses ShapeDrawable to display in Canvas.

我能够在这个code显示RectShape,但寻找如何显示字符串上!

I'm able to display RectShape in this code, but looking for how to display the String on that!

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

截图:

推荐答案

对我来说,最好的选择是创建一个自定义的 TextDrawable 处理得当如何显示一块文本。然后在你的 CustomDrawableView 的OnDraw(帆布C)方法,你可以调用它来显示文本和椭圆形。

The best option for me is to create a custom TextDrawable that handles properly how to display a piece of text. Then in your CustomDrawableView in your onDraw(Canvas c) method you can call it to display the text and the oval.

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

Take a look at this answer I wrote recently as it contains how to do it properly.

这篇关于如何使用在RectShape或OvalShape显示Android的画布ShapeDrawable文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆