美文网首页
2019-03-04

2019-03-04

作者: 灵长风 | 来源:发表于2019-03-04 10:00 被阅读0次

    @@ -0,0 +1,85 @@

    skipFrameCount监听

    import android.util.Log;

    import android.view.Choreographer;

    import com.ta.utdid2.android.utils.SystemProperties;

    import java.lang.reflect.Field;

    public class SMFrameCallback implements Choreographer.FrameCallback {

        public static  SMFrameCallback sInstance;

        private String TAG="SMFrameCallback";

        public static final float deviceRefreshRateMs=16.6f;

        public static  long lastFrameTimeNanos=0;//纳秒为单位

        public static  long currentFrameTimeNanos=0;

        public void start() {

            Choreographer.getInstance().postFrameCallback(SMFrameCallback.getInstance());

        }

        public static SMFrameCallback getInstance() {

            if (sInstance == null) {

                sInstance = new SMFrameCallback();

            }

            return sInstance;

        }

        @Override

        public void doFrame(long frameTimeNanos) {

            if(lastFrameTimeNanos==0){

                lastFrameTimeNanos=frameTimeNanos;

                Choreographer.getInstance().postFrameCallback(this);

                return;

            }

            currentFrameTimeNanos=frameTimeNanos;

            float value=(currentFrameTimeNanos-lastFrameTimeNanos)/1000000.0f;

            final int skipFrameCount = skipFrameCount(lastFrameTimeNanos, currentFrameTimeNanos, deviceRefreshRateMs);

            if(skipFrameCount > 30){

                Log.e(TAG,"两次绘制时间间隔value="+value+"  frameTimeNanos="+frameTimeNanos+"  currentFrameTimeNanos="+currentFrameTimeNanos+"  skipFrameCount="+skipFrameCount+"");

            }

            lastFrameTimeNanos=currentFrameTimeNanos;

            Choreographer.getInstance().postFrameCallback(this);

        }

        private int getLimitCount(){

            try {

                Field field = Choreographer.class.getDeclaredField("SKIPPED_FRAME_WARNING_LIMIT");

                return field.getInt(Choreographer.getInstance());

            } catch (Throwable e) {

                e.printStackTrace();

            }

            return 0;

        }

        /**

        *

        *计算跳过多少帧

        * @param start

        * @param end

        * @param devicefreshRate

        * @return

        */

        private  int skipFrameCount(long start,long end,float devicefreshRate){

            int count =0;

            long diffNs=end-start;

            long diffMs = Math.round(diffNs / 1000000.0f);

            long dev=Math.round(devicefreshRate);

            if(diffMs>dev){

                long skipCount=diffMs/dev;

                count=(int)skipCount;

            }

            return  count;

        }

    }

    相关文章

      网友评论

          本文标题:2019-03-04

          本文链接:https://www.haomeiwen.com/subject/ikrbuqtx.html