OnDraw的自定义视图无限循环的android [英] Ondraw for a custom view is looping infinitely android

查看:565
本文介绍了OnDraw的自定义视图无限循环的android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

OnDraw中为我的自定义View功能被无限调用,是循环!可能是什么原因可能??

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

此观点被放置在XML文件的相对视图中,相对的观点是横向滚动视图的孩子。

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

推荐答案

这里没有一个无限循环。这是怎么回事是OS是尽可能快地重新绘制你的活动。当你的活动被重绘重绘其所有的孩子们的意见。当你的code具有非常小的计算,从我所看到的,它的运行速度非常快,可能是在率> 30 FPS重绘。你的日志消息使得它看起来好象是一个无限循环的时候没有。事实上,竟然没有你的的onDraw 方法内循环​​。

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.

要说明什么是在尝试这个打算。添加保护成员 drawCount 你的类,并将其设置为0:

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;

然后追加 drawCount 你的的onDraw 日志信息的结束

 public void onDraw(Canvas canvas){
       drawCount++;
       Log.w(this.getClass().getName(),"onDraw of Balls called. Total draws:" + Integer.toString(drawCount));
  ...
  }

你应该看到的是每个日志消息将显示不同的drawCount。

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

如果你想获得幻想,并计算你的应用程序的帧率可以测量时间,因为第一次抽签,然后除以 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.

这篇关于OnDraw的自定义视图无限循环的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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