我怎样才能得到一个自定义视图的画布大小的OnDraw方法之外? [英] How can I get the canvas size of a custom view outside of the onDraw method?

查看:172
本文介绍了我怎样才能得到一个自定义视图的画布大小的OnDraw方法之外?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够访问该视图的画布的大小,以执行一些计算。出于某种原因,该视图的大小传递给 onSizeChanged 是不是传给的OnDraw 画布的大小不同。我目前的解决方法使用一个布尔标志,以确定何时我需要做的计算。

理想的解决方案将允许我做这些计算中的 onSizeChanged 的方法,所以我不知道...有什么办法可以得到阿霍德的画布对象(或至少它的尺寸)的的OnDraw 法外?

我的code是如下。它被绘制圆的半径在给定的角度。当我使用 canvas.centerX()来确定起点和终点半径,一切完美的作品。如果我用传递到 onSizeChanged 的参数,它甚至没有远程接近正确。

  @覆盖
保护无效onSizeChanged(INT W,INT小时,INT oldw,诠释oldh){
  super.onSizeChanged(W,H,oldw,oldh);
  mSizeChanged = TRUE;
}

@覆盖
保护无效的OnDraw(帆布油画){
  super.onDraw(画布);

  如果(mSizeChanged){
    RectF边界=新RectF(canvas.getClipBounds());
    浮动的centerX = bounds.centerX();
    浮centerY = bounds.centerY();
    浮动radianAngle =(浮点)Math.toRadians(mStartAngle);

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

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

解决方案

有关绘图的目的,你真的不应该使用画布对象的尺寸。

只是用在 onSizeChanged 方法提供给您的尺寸。您可以存储尺寸在的OnDraw 方法使用或调整/画上的支持位图,你可以与后来的画。

更新:

迅速刮起了一些code,它看起来像这样工作的:

 公共类CustomView扩展视图{
    民营涂料粉刷;
    私人诠释瓦;
    私人诠释H;

    公共CustomView(上下文的背景下,的AttributeSet attr)使用{
        超(背景下,ATTR);
        油漆=新的油漆();
        paint.setTextAlign(Align.CENTER);
    }

    @覆盖
    保护无效onSizeChanged(INT W,INT小时,INT oldw,诠释oldh){
        this.w = W;
        this.h = H;
        super.onSizeChanged(W,H,oldw,oldh);
    }

    @覆盖
    保护无效的OnDraw(帆布油画){
        canvas.drawColor(Color.WHITE);
        canvas.drawText(TEST,瓦特/ 2,H / 2,油漆);
    }
}
 

更新2

继圆code更新。

我们可以做到这一点:

  @覆盖
    保护无效的OnDraw(帆布油画){
        canvas.drawColor(Color.WHITE);
        浮动的centerX =(浮点)W / 2;
        浮动centerY =(浮点)H / 2;
        浮动radianAngle =(浮点)Math.toRadians(由startAngle);

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

        paint.setColor(0xFF330000);
        paint.setStrokeWidth(1);
        canvas.drawLines(半径,油漆);
    }
 

您会看到,现在这适用于任何规模的看法。

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.

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?

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

解决方案

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

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.

Update:

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

Update 2

Following the circle code update.

We can do this:

   @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天全站免登陆