当前位置:   article > 正文

安卓ondraw刷新视图,OnDraw的自定义视图无限循环的android

android中ondraw绘画完没有刷新界面

OnDraw function for my custom View is being called infinitely and is looping !! What could be possible reason??

Here is my custom view:-

public class Balls extends View{

private static final String TAG = "BallsView";

private int mMode = READY;

public static final int PAUSE = 0;

public static final int READY = 1;

public static final int RUNNING = 2;

public static final int LOSE = 3;

private final Paint mPaint = new Paint();

private double mUx = 0.1;

private double mUy = 2;

private double mVy;

private double mVx;

private double mSx;

private double mSy;

private double mRange;

private float mX1;

private float mY1;

private int mX2;

private int mY2;

private int mDx;

private int mDy;

Time t;

float mAngle;

private final double mGravity = -9.8;

private long mLastTime;

private double mT;

private Canvas mCanvas = null;

public Balls(Context context, AttributeSet attrs, int defStyle){

super(context, attrs, defStyle);

setFocusable(true);

setWillNotDraw(false);

mPaint.setStyle(Paint.Style.FILL_AND_STROKE);

mPaint.setStrokeWidth(10);

mPaint.setAntiAlias(true);

mPaint.setStrokeCap(Cap.ROUND);

//mPaint.setColor(0xff00ffff);

mPaint.setARGB(255, 0, 255, 0);

mLastTime = System.currentTimeMillis();

}

public Balls(Context context, AttributeSet attrs) {

super(context, attrs);

setFocusable(true);

setWillNotDraw(false);

mPaint.setStyle(Paint.Style.FILL_AND_STROKE);

mPaint.setStrokeWidth(10);

mPaint.setAntiAlias(true);

mPaint.setStrokeCap(Cap.ROUND);

mPaint.setColor(0xff00ffff);

}

@Override

public void onDraw(Canvas canvas) {

Log.w(this.getClass().getName(),"onDraw of Balls called");

super.onDraw(canvas);

mCanvas = canvas;

if(mCanvas!= null)

Log.w(this.getClass().getName(),"Canvas is not null");

}

This view is inflated as follows in another activity:-

mBalls = (Balls) findViewById(R.id.balls);

This view is placed in xml file inside a relative view and the relative view is the child of horizontal scroll view.

解决方案

There isn't an infinite loop here. What is going on is the OS is redrawing your activity as fast as possible. When your activity gets redrawn it redraws all of its children views. As your code has very little computation, from what I can see here, it is running very fast and is probably redrawing at a rate >30 FPS. Your log message makes it appear as if there is an infinite loop when there isn't. In fact there isn't even a loop inside your onDraw method.

To illustrate what is going on try this. Add a protected member drawCount to your Balls class and set it to 0:

protect int drawCount = 0;

Then append drawCount to the end of your onDraw log message

public void onDraw(Canvas canvas){

drawCount++;

Log.w(this.getClass().getName(),"onDraw of Balls called. Total draws:" + Integer.toString(drawCount));

...

}

What you should see is each log message will display a different drawCount.

If you want to get fancy and calculate the framerate of your app you could measure the time since the first draw and then divide the drawCount by how much time has passed which would give you a estimate of your activities framerate.

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/673350
推荐阅读
相关标签
  

闽ICP备14008679号