Java Code Examples for android.view.ViewTreeObserver#OnDrawListener

The following examples show how to use android.view.ViewTreeObserver#OnDrawListener . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FirstFrameAnimatorHelper.java    From Trebuchet with GNU General Public License v3.0 6 votes vote down vote up
public static void initializeDrawListener(View view) {
    if (sGlobalDrawListener != null) {
        view.getViewTreeObserver().removeOnDrawListener(sGlobalDrawListener);
    }
    sGlobalDrawListener = new ViewTreeObserver.OnDrawListener() {
            private long mTime = System.currentTimeMillis();
            public void onDraw() {
                sGlobalFrameCounter++;
                if (DEBUG) {
                    long newTime = System.currentTimeMillis();
                    Log.d("FirstFrameAnimatorHelper", "TICK " + (newTime - mTime));
                    mTime = newTime;
                }
            }
        };
    view.getViewTreeObserver().addOnDrawListener(sGlobalDrawListener);
    sVisible = true;
}
 
Example 2
Source File: FirstFrameAnimatorHelper.java    From TurboLauncher with Apache License 2.0 6 votes vote down vote up
public static void initializeDrawListener(View view) {
    if (sGlobalDrawListener != null) {
        view.getViewTreeObserver().removeOnDrawListener(sGlobalDrawListener);
    }
    sGlobalDrawListener = new ViewTreeObserver.OnDrawListener() {
            private long mTime = System.currentTimeMillis();
            public void onDraw() {
                sGlobalFrameCounter++;
                if (DEBUG) {
                    long newTime = System.currentTimeMillis();
                    Log.d("FirstFrameAnimatorHelper", "TICK " + (newTime - mTime));
                    mTime = newTime;
                }
            }
        };
    view.getViewTreeObserver().addOnDrawListener(sGlobalDrawListener);
    sVisible = true;
}
 
Example 3
Source File: FirstFrameAnimatorHelper.java    From LB-Launcher with Apache License 2.0 6 votes vote down vote up
public static void initializeDrawListener(View view) {
    if (sGlobalDrawListener != null) {
        view.getViewTreeObserver().removeOnDrawListener(sGlobalDrawListener);
    }
    sGlobalDrawListener = new ViewTreeObserver.OnDrawListener() {
            private long mTime = System.currentTimeMillis();
            public void onDraw() {
                sGlobalFrameCounter++;
                if (DEBUG) {
                    long newTime = System.currentTimeMillis();
                    Log.d("FirstFrameAnimatorHelper", "TICK " + (newTime - mTime));
                    mTime = newTime;
                }
            }
        };
    view.getViewTreeObserver().addOnDrawListener(sGlobalDrawListener);
    sVisible = true;
}
 
Example 4
Source File: FirstFrameAnimatorHelper.java    From LaunchEnr with GNU General Public License v3.0 5 votes vote down vote up
static void initializeDrawListener(View view) {
    if (sGlobalDrawListener != null) {
        view.getViewTreeObserver().removeOnDrawListener(sGlobalDrawListener);
    }
    sGlobalDrawListener = new ViewTreeObserver.OnDrawListener() {
            private long mTime = System.currentTimeMillis();
            public void onDraw() {
                sGlobalFrameCounter++;
            }
        };
    view.getViewTreeObserver().addOnDrawListener(sGlobalDrawListener);
    sVisible = true;
}
 
Example 5
Source File: PageDrawMonitor.java    From AndroidGodEye with Apache License 2.0 5 votes vote down vote up
private void runOnDrawEnd(View view, ViewTreeObserver.OnDrawListener onDrawListener, int maxPostTimes, @NonNull ViewUtil.OnDrawCallback onDrawCallback) {
    if (view == null || onDrawListener == null) {
        return;
    }
    maxPostTimes --;
    int finalMaxPostTimes = maxPostTimes;
    postTraversalFinishCallBack(() -> {
        if (!isDraw && finalMaxPostTimes > 0) {
            runOnDrawEnd(view, onDrawListener, finalMaxPostTimes, onDrawCallback);
        } else {
            view.getViewTreeObserver().removeOnDrawListener(onDrawListener);
            onDrawCallback.didDraw();
        }
    });
}
 
Example 6
Source File: ImageCanaryInternal.java    From AndroidGodEye with Apache License 2.0 5 votes vote down vote up
void start(Application application, ImageCanary imageCanaryEngine) {
    Handler handler = ThreadUtil.createIfNotExistHandler(IMAGE_CANARY_HANDLER);
    callbacks = new SimpleActivityLifecycleCallbacks() {

        private Map<Activity, ViewTreeObserver.OnDrawListener> mOnDrawListenerMap = new HashMap<>();
        private Set<ImageIssue> mImageIssues = new HashSet<>();

        @Override
        public void onActivityResumed(Activity activity) {
            super.onActivityResumed(activity);
            ViewGroup parent = (ViewGroup) activity.getWindow().getDecorView();
            Runnable callback = inspectInner(new WeakReference<>(activity), imageCanaryEngine, mImageIssues);
            ViewTreeObserver.OnDrawListener onDrawListener = () -> {
                if (handler != null) {
                    handler.removeCallbacks(callback);
                    handler.postDelayed(callback, 300);
                }
            };
            mOnDrawListenerMap.put(activity, onDrawListener);
            parent.getViewTreeObserver().addOnDrawListener(onDrawListener);
        }

        @Override
        public void onActivityPaused(Activity activity) {
            super.onActivityPaused(activity);
            ViewTreeObserver.OnDrawListener onDrawListener = mOnDrawListenerMap.remove(activity);
            ViewGroup parent = (ViewGroup) activity.getWindow().getDecorView();
            if (onDrawListener != null) {
                parent.getViewTreeObserver().removeOnDrawListener(onDrawListener);
            }
        }
    };
    application.registerActivityLifecycleCallbacks(callbacks);
}
 
Example 7
Source File: RunReviewOverlay.java    From science-journal with Apache License 2.0 5 votes vote down vote up
public void refreshAfterChartLoad(final boolean backUpdateProgressBar, final int numAttempts) {
  if (!chartController.hasDrawnChart()) {
    // Refresh the Run Review Overlay after the line graph presenter's chart
    // has finished drawing itself.
    final ViewTreeObserver observer = chartController.getChartViewTreeObserver();
    if (observer == null) {
      return;
    }
    observer.removeOnDrawListener(onDrawListener);
    onDrawListener =
        new ViewTreeObserver.OnDrawListener() {
          @Override
          public void onDraw() {
            RunReviewOverlay.this.post(
                new Runnable() {
                  @Override
                  public void run() {
                    if (!observer.isAlive()) {
                      if (numAttempts < MAX_REFRESH_ATTEMPTS) {
                        // Just try again, maybe it will come alive.
                        refreshAfterChartLoad(backUpdateProgressBar, numAttempts + 1);
                      }
                      return;
                    }
                    // The ViewTreeObserver calls its listeners without an iterator,
                    // so we need to remove the listener outside the flow or we risk
                    // an index-out-of-bounds crash in the case of multiple listeners.
                    observer.removeOnDrawListener(onDrawListener);
                    onDrawListener = null;
                    refresh(backUpdateProgressBar);
                  }
                });
          }
        };
    observer.addOnDrawListener(onDrawListener);
  } else {
    refresh(backUpdateProgressBar);
  }
}
 
Example 8
Source File: PEWTextView.java    From ParallaxEverywhere with MIT License 5 votes vote down vote up
@Override
protected void onAttachedToWindow() {
    super.onAttachedToWindow();

    mOnScrollChangedListener = new ViewTreeObserver.OnScrollChangedListener() {
        @Override
        public void onScrollChanged() {
            applyParallax();
        }
    };

    mOnGlobalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            heightView = (float) getHeight();
            widthView = (float) getWidth();

            applyParallax();
        }
    };

    ViewTreeObserver viewTreeObserver = getViewTreeObserver();
    viewTreeObserver.addOnScrollChangedListener(mOnScrollChangedListener);
    viewTreeObserver.addOnGlobalLayoutListener(mOnGlobalLayoutListener);

    if (updateOnDraw
            && android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        onDrawListener = new ViewTreeObserver.OnDrawListener() {
            @Override
            public void onDraw() {
                applyParallax();
            }
        };
        viewTreeObserver.addOnDrawListener(onDrawListener);
    }

    parallaxAnimation();
}
 
Example 9
Source File: PEWImageView.java    From ParallaxEverywhere with MIT License 5 votes vote down vote up
@Override
protected void onAttachedToWindow() {
    super.onAttachedToWindow();

    mOnScrollChangedListener = new ViewTreeObserver.OnScrollChangedListener() {
        @Override
        public void onScrollChanged() {
            applyParallax();
        }
    };

    mOnGlobalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            heightImageView = (float) getHeight();
            widthImageView = (float) getWidth();

            applyParallax();
        }
    };

    ViewTreeObserver viewTreeObserver = getViewTreeObserver();
    viewTreeObserver.addOnScrollChangedListener(mOnScrollChangedListener);
    viewTreeObserver.addOnGlobalLayoutListener(mOnGlobalLayoutListener);

    if (updateOnDraw
            && android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        onDrawListener = new ViewTreeObserver.OnDrawListener() {
            @Override
            public void onDraw() {
                applyParallax();
            }
        };
        viewTreeObserver.addOnDrawListener(onDrawListener);
    }

    parallaxAnimation();
}
 
Example 10
Source File: PageDrawMonitor.java    From AndroidGodEye with Apache License 2.0 4 votes vote down vote up
public void listen() {
    ViewTreeObserver.OnDrawListener onDrawListener = () -> isDraw = true;
    view.getViewTreeObserver().addOnDrawListener(onDrawListener);
    runOnDrawEnd(view, onDrawListener, 3, onDrawCallback);
}