2019-03-04

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

    }

}

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容