如何在 onDraw 方法之外获取自定义视图的画布大小? [英] How can I get the canvas size of a custom view outside of the onDraw method?

查看:17
本文介绍了如何在 onDraw 方法之外获取自定义视图的画布大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够访问视图画布的大小来执行一些计算.出于某种原因,传递给 onSizeChanged 的视图大小与传递给 onDraw 的画布大小不同.我当前的解决方法使用布尔标志来确定何时需要进行计算.

I need to be able to access the size of the view's canvas to perform some calculations. For some reason, the size of the view passed to onSizeChanged is different than the size of the canvas passed to onDraw. My current workaround uses a boolean flag to determine when I need to do the calculations.

理想的解决方案将允许我在 onSizeChanged 方法中进行这些计算,所以我想知道...有什么方法可以获取 Canvas onDraw 方法之外的对象(或至少它的尺寸)?

The ideal solution would allow me to do these calculations in the onSizeChanged method, so I'm wondering... is there any way I can get ahold of the Canvas object (or at least it's dimensions) outside of the onDraw method?

我的代码如下.它以给定的角度绘制圆的半径.当我使用 canvas.centerX() 来确定半径的起点和终点时,一切正常.如果我使用传递给 onSizeChanged 的参数,它甚至远不接近正确.

My code is below. It is draws the radius of a circle at a given angle. When I use canvas.centerX() to determine the start points and end points for the radius, everything works perfectly. If I use the parameters passed into onSizeChanged, it isn't even remotely close to correct.

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
  super.onSizeChanged(w, h, oldw, oldh);
  mSizeChanged = true;
}

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

  if (mSizeChanged) {
    RectF bounds = new RectF(canvas.getClipBounds());
    float centerX = bounds.centerX();
    float centerY = bounds.centerY();
    float radianAngle = (float) Math.toRadians(mStartAngle);

    mRadius[0] = center;
    mRadius[1] = center;
    mRadius[2] = center + center * FloatMath.cos(radianAngle);
    mRadius[3] = center + center * FloatMath.sin(radianAngle);
    mSizeChanged = false;
  }

  mPaint.setColor(0xFF330000);
  mPaint.setStrokeWidth(1);
  canvas.drawLines(mRadius, mPaint);
}

推荐答案

出于绘图目的,您不应该真正使用 Canvas 对象的尺寸.

For drawing purposes, you should not really use the dimensions of the Canvas object.

只需使用在 onSizeChanged 方法中提供给您的尺寸.您可以存储尺寸以在 onDraw 方法中使用,也可以调整大小/绘制到您可以稍后绘制的支持位图.

Just use the dimensions provided to you in the onSizeChanged method. You can either store the dimensions for use in the onDraw method or resize/draw to a backing bitmap that you can draw with later.

更新:

快速编写一些代码,看起来像这样:

Quickly whipped up some code, it looks like this works:

public class CustomView extends View{
    private Paint paint;
    private int w;
    private int h;

    public CustomView(Context context, AttributeSet attr) {
        super(context, attr);
        paint = new Paint();
        paint.setTextAlign(Align.CENTER);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        this.w = w;
        this.h = h;
        super.onSizeChanged(w, h, oldw, oldh);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawColor(Color.WHITE);
        canvas.drawText("TEST", w/2, h/2, paint);   
    }
}

更新 2

关注圈码更新.

我们可以这样做:

   @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawColor(Color.WHITE);
        float centerX = (float) w/2;
        float centerY = (float) h/2;
        float radianAngle = (float) Math.toRadians(startAngle);

        radius[0] = centerX;
        radius[1] = centerY;
        radius[2] = centerX + centerX * FloatMath.cos(radianAngle);
        radius[3] = centerY + centerY * FloatMath.sin(radianAngle);

        paint.setColor(0xFF330000);
        paint.setStrokeWidth(1);
        canvas.drawLines(radius, paint);
    }

您会发现这现在适用于任何尺寸的视图.

You'll see that this now works on any sized view.

这篇关于如何在 onDraw 方法之外获取自定义视图的画布大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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