Java Code Examples for android.view.ViewTreeObserver#addOnDrawListener()

The following examples show how to use android.view.ViewTreeObserver#addOnDrawListener() . 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: ViewActivity.java    From android-open-source-project-analysis with Apache License 2.0 6 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view);

    view = (CustomView) findViewById(R.id.view);

    ViewTreeObserver viewTreeObserver = view.getViewTreeObserver();
    viewTreeObserver.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
        @Override
        public void onDraw() {

        }
    });

    viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {

        }
    });
}
 
Example 2
Source File: Launcher.java    From LaunchEnr with GNU General Public License v3.0 5 votes vote down vote up
public void onWindowVisibilityChanged(int visibility) {
    // The following code used to be in onResume, but it turns out onResume is called when
    // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
    // is a more appropriate event to handle
    if (visibility == View.VISIBLE) {
        if (!mWorkspaceLoading) {
            final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
            // We want to let Launcher draw itself at least once before we force it to build
            // layers on all the workspace pages, so that transitioning to Launcher from other
            // apps is nice and speedy.
            observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
                private boolean mStarted = false;
                public void onDraw() {
                    if (mStarted) return;
                    mStarted = true;
                    // We delay the layer building a bit in order to give
                    // other message processing a time to run.  In particular
                    // this avoids a delay in hiding the IME if it was
                    // currently shown, because doing that may involve
                    // some communication back with the app.
                    mWorkspace.postDelayed(mBuildLayersRunnable, 500);
                    final ViewTreeObserver.OnDrawListener listener = this;
                    mWorkspace.post(new Runnable() {
                        public void run() {
                            if (mWorkspace != null &&
                                    mWorkspace.getViewTreeObserver() != null) {
                                mWorkspace.getViewTreeObserver().
                                        removeOnDrawListener(listener);
                            }
                        }
                    });
                    return;
                }
            });
        }
        clearTypedText();
    }
}
 
Example 3
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 4
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 5
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 6
Source File: Launcher.java    From TurboLauncher with Apache License 2.0 4 votes vote down vote up
public void onWindowVisibilityChanged(int visibility) {
	mVisible = visibility == View.VISIBLE;
	updateRunning();
	// The following code used to be in onResume, but it turns out onResume
	// is called when
	// you're in All Apps and click home to go to the workspace.
	// onWindowVisibilityChanged
	// is a more appropriate event to handle
	if (mVisible) {
		mAppsCustomizeLayout.onWindowVisible();
		if (!mWorkspaceLoading) {
			final ViewTreeObserver observer = mWorkspace
					.getViewTreeObserver();
			// We want to let Launcher draw itself at least once before we
			// force it to build
			// layers on all the workspace pages, so that transitioning to
			// Launcher from other
			// apps is nice and speedy.
			observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
				private boolean mStarted = false;

				public void onDraw() {
					if (mStarted)
						return;
					mStarted = true;
					// We delay the layer building a bit in order to give
					// other message processing a time to run. In particular
					// this avoids a delay in hiding the IME if it was
					// currently shown, because doing that may involve
					// some communication back with the app.
					mWorkspace.postDelayed(mBuildLayersRunnable, 500);
					final ViewTreeObserver.OnDrawListener listener = this;
					mWorkspace.post(new Runnable() {
						public void run() {
							if (mWorkspace != null
									&& mWorkspace.getViewTreeObserver() != null) {
								mWorkspace.getViewTreeObserver()
										.removeOnDrawListener(listener);
							}
						}
					});
					return;
				}
			});
		}
		clearTypedText();
	}
}