美文网首页
区域计算

区域计算

作者: Ford_ | 来源:发表于2022-01-02 20:07 被阅读0次


    package com.vivo.circle;

    import android.annotation.TargetApi;
    import android.app.KeyguardManager;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.graphics.Rect;
    import android.os.Build;
    import android.text.TextUtils;
    import android.util.Log;
    import android.view.View;
    import android.view.ViewTreeObserver;

    import java.util.Map;
    import java.util.concurrent.ConcurrentHashMap;

    @TargetApi(Build.VERSION_CODES.CUPCAKE)
    public class IViweMonitor implements ViewTreeObserver.OnGlobalLayoutListener, ViewTreeObserver.OnScrollChangedListener {
    private static final String TAG = IViweMonitor.class.getSimpleName();
    private static final Map<View, IViweMonitor> MONITOR_MAP = new ConcurrentHashMap<View, IViweMonitor>();
    private View view;
    private boolean viewHasShown;
    private long showStartTime;
    private int maxShowAreaPercentage;
    private Rect visibleRect = new Rect();
    private boolean screenAvailable = true;
    private boolean needObserver = true;
    private BroadcastReceiver screenReceiverToRegister;
    private BroadcastReceiver screenStateReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
    if (null == intent) {
    return;
    }
    Log.e(TAG, "---333---");
    String action = intent.getAction();
    if (TextUtils.equals(Intent.ACTION_SCREEN_ON, action) || TextUtils.equals(Intent.ACTION_SCREEN_OFF, action) ||
    TextUtils.equals(Intent.ACTION_USER_PRESENT, action)) {
    Log.e(TAG, "---444---");
    checkScreenState();
    checkViewVisibilityAndShowArea();
    }
    }
    };

    public IViweMonitor(View view, boolean needObserver) {
        this.view = view;
        this.needObserver = needObserver;
    }
    
    public void registerObservers() {
        if (null == view) {
            return;
        }
        Log.e(TAG, "---222---");
        ViewTreeObserver viewTreeObserver = this.view.getViewTreeObserver();
        IViweMonitor oldMonitor = MONITOR_MAP.get(view);
        if (oldMonitor != null && viewTreeObserver.isAlive()) {
            viewTreeObserver.removeOnScrollChangedListener(this);
            viewTreeObserver.removeOnGlobalLayoutListener(this);
        }
        MONITOR_MAP.put(view, this);
        if (needObserver && viewTreeObserver.isAlive()) {
            viewTreeObserver.addOnScrollChangedListener(this);
            viewTreeObserver.addOnGlobalLayoutListener(this);
        }
        IntentFilter screenIntentFilter = new IntentFilter();
        screenIntentFilter.addAction(Intent.ACTION_SCREEN_ON);
        screenIntentFilter.addAction(Intent.ACTION_SCREEN_OFF);
        screenIntentFilter.addAction(Intent.ACTION_USER_PRESENT);
        screenReceiverToRegister = screenStateReceiver;
        view.getContext().registerReceiver(screenReceiverToRegister, screenIntentFilter);
        screenAvailable = true;
    }
    
    private void checkScreenState() {
        Context context = view.getContext();
        boolean screenInteractive = false;
        KeyguardManager keyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
        boolean screenLocked = keyguardManager.inKeyguardRestrictedInputMode();
        screenAvailable = screenInteractive && !screenLocked;
    }
    
    private void unregisterObservers() {
    }
    
    public void onViewAttachedToWindow() {
        registerObservers();
        checkViewVisibilityAndShowArea();
    }
    
    public void onViewDetachedFromWindow() {
        unregisterObservers();
        onViewHidden();
    }
    
    private void checkViewVisibilityAndShowArea() {
        boolean visible = screenAvailable && view.isShown() && view.getLocalVisibleRect(visibleRect);
        int viewArea = view.getWidth() * view.getHeight();
        if (visible && viewArea > 0) {
            int visibleArea = visibleRect.width() * visibleRect.height();
            int areaPercentage = visibleArea * 100 / viewArea;
            if (areaPercentage > maxShowAreaPercentage) {
                maxShowAreaPercentage = areaPercentage;
            }
            onUpdateViewShowArea(areaPercentage);
            if (areaPercentage <= 0) {
                visible = false;
            }
        }
        if (visible) {
            onViweShown();
        } else {
            onViewHidden();
        }
    }
    
    public void onViweShown() {
        Log.e(TAG, "show:"+viewHasShown);
        if (viewHasShown) {
            return;
        }
        viewHasShown = true;
        showStartTime = System.currentTimeMillis();
        onViewShowStart();
    }
    
    public void onViewHidden() {
        Log.e(TAG, "hidden:"+viewHasShown);
        if (!viewHasShown) {
            return;
        }
        viewHasShown = false;
        long showEndTime = System.currentTimeMillis();
        long duration = showEndTime - showStartTime;
        onViewShowEnd(duration, maxShowAreaPercentage);
        maxShowAreaPercentage = 0;
    }
    
    private void onViewShowEnd(long duration, int maxShowAreaPercentage) {
        Log.e(TAG, "结束:" + duration + "毫秒  " + maxShowAreaPercentage);
    }
    
    public void onUpdateViewShowArea(int percentage) {
        Log.e(TAG, "更新:" + percentage);
    }
    
    public void onViewShowStart() {
    
    }
    
    @Override
    public void onGlobalLayout() {
        checkViewVisibilityAndShowArea();
    }
    
    @Override
    public void onScrollChanged() {
        checkViewVisibilityAndShowArea();
    }
    

    }

    相关文章

      网友评论

          本文标题:区域计算

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