Android:在Canvas和View上绘图(不带位图)

发布于 2024-12-03 08:41:49 字数 1097 浏览 0 评论 0原文

我创建了RelativeLayout 的子级。 在它的构造函数中我调用:

    public CanvasHolder(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        layoutInflater.inflate(R.layout.canvas_holder, this);    
        this.draw(new Canvas());
    }

然后重写方法 onDraw():

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        black.setARGB(255, 0, 0, 0);
        black.setAntiAlias(true);
        int halfWidth = this.getWidth() / 2;
        int height = this.getHeight();
        canvas.drawLine(0, 0, halfWidth, height, black);
    }

视图上没有出现任何线条。 另外,我尝试不调用“this.draw()”,但什么也没发生。

我发现halfWidth和height等于0。为什么?

PS即使我静态设置宽度和高度,也不会改变。 如果您不介意,请看一下这个示例应用程序:为什么没有绘制任何内容? http://androidforums.com/attachment.php?attachmentid=21715& ;stc=1&d=1315232946

I've created the child of RelativeLayout.
In its constructor I called:

    public CanvasHolder(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        layoutInflater.inflate(R.layout.canvas_holder, this);    
        this.draw(new Canvas());
    }

And then override method onDraw():

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        black.setARGB(255, 0, 0, 0);
        black.setAntiAlias(true);
        int halfWidth = this.getWidth() / 2;
        int height = this.getHeight();
        canvas.drawLine(0, 0, halfWidth, height, black);
    }

No line has been appeared on the view.
Also, I tried not to call "this.draw()", but nothing happened.

I've discovered that halfWidth and height equals 0. Why?

P.S. even if I set width and height statically, nothing will change.
If you don't mind, take a look on this sample application: why nothing is drawn?
http://androidforums.com/attachment.php?attachmentid=21715&stc=1&d=1315232946

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

缺⑴份安定 2024-12-10 08:41:49

您是否覆盖了onMeasure?这是我的 - 你可能会发现在加载视图时它会被调用几次,有时值为 0:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
    int parentHeight = MeasureSpec.getSize(heightMeasureSpec);

    if (parentHeight > 0) {
        this.setMeasuredDimension(parentWidth, parentHeight);
        // ... other stuff ...
    }
}

Did you overwrite the onMeasure? Here's mine - you may find that this gets called a few times whilst the view is loading, sometimes with 0 values:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
    int parentHeight = MeasureSpec.getSize(heightMeasureSpec);

    if (parentHeight > 0) {
        this.setMeasuredDimension(parentWidth, parentHeight);
        // ... other stuff ...
    }
}
鸠魁 2024-12-10 08:41:49

我找到了不平凡的解决方案。我已经通过带有 1 个参数的构造函数初始化了视图。然后我在布局中插入自定义视图,并在需要时调用 invalidate() 。

I've found non-trivial solution. I've inited the view by the constructor with 1 parameter. And then I inserted the custom view in the layout and called invalidate() when I needed it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文