本文整理汇总了Java中android.view.View.getViewTreeObserver方法的典型用法代码示例。如果您正苦于以下问题:Java View.getViewTreeObserver方法的具体用法?Java View.getViewTreeObserver怎么用?Java View.getViewTreeObserver使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.view.View
的用法示例。
在下文中一共展示了View.getViewTreeObserver方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: onLaidOut
import android.view.View; //导入方法依赖的package包/类
/**
* Executes the given {@link Runnable} when the view is laid out
**/
public static void onLaidOut(final View view, final Runnable runnable) {
if (isLaidOut(view)) {
runnable.run();
return;
}
final ViewTreeObserver observer = view.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
final ViewTreeObserver trueObserver;
if (observer.isAlive()) {
trueObserver = observer;
} else {
trueObserver = view.getViewTreeObserver();
}
removeGlobalLayoutObserver(trueObserver, this);
runnable.run();
}
});
}
示例2: setOverflowButtonColor
import android.view.View; //导入方法依赖的package包/类
public static void setOverflowButtonColor(@NonNull Activity activity,
@Nullable Toolbar toolbar,
final @ColorInt int color) {
if (toolbar != null && toolbar.getTag() != null && ATE.IGNORE_TAG.equals(toolbar.getTag()))
return; // ignore tag was set, don't update the overflow
final String overflowDescription = activity.getString(R.string.abc_action_menu_overflow_description);
final View target = toolbar != null ? toolbar :
(ViewGroup) activity.getWindow().getDecorView();
final ViewTreeObserver viewTreeObserver = target.getViewTreeObserver();
viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
final ArrayList<View> outViews = new ArrayList<>();
target.findViewsWithText(outViews, overflowDescription,
View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION);
if (outViews.isEmpty()) return;
final AppCompatImageView overflow = (AppCompatImageView) outViews.get(0);
overflow.setImageDrawable(TintHelper.createTintedDrawable(overflow.getDrawable(), color));
removeOnGlobalLayoutListener(target, this);
}
});
}
示例3: onCreate
import android.view.View; //导入方法依赖的package包/类
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final View layout = findViewById(Window.ID_ANDROID_CONTENT);
ViewTreeObserver vto = layout.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
layout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
} else {
layout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
initUIandEvent();
}
});
}
示例4: animateViewAfterMeasured
import android.view.View; //导入方法依赖的package包/类
private void animateViewAfterMeasured(final View view) {
final ViewTreeObserver vto = view.getViewTreeObserver();
if (!vto.isAlive()) {
sharePickerView.setVisibility(GONE);
return;
}
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (view.getHeight() > 0 || view.getWidth() > 0) {
if (vto.isAlive()) {
vto.removeGlobalOnLayoutListener(this);
animateAppear();
}
}
}
});
}
示例5: onMapReady
import android.view.View; //导入方法依赖的package包/类
public void onMapReady(GoogleMap googleMap) {
if (m_gMap == null) {
m_gMap = googleMap;
GoogleMap map = getMap();
if (map == null) {
MFBUtil.Alert(this, getString(R.string.txtError), getString(R.string.errNoGoogleMaps));
finish();
return;
}
map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
UiSettings settings = map.getUiSettings();
settings.setCompassEnabled(false);
settings.setRotateGesturesEnabled(false);
settings.setScrollGesturesEnabled(true);
settings.setZoomControlsEnabled(false);
settings.setZoomGesturesEnabled(true);
View mapView = getFragmentManager().findFragmentById(R.id.mfbMap).getView();
if (mapView != null && mapView.getViewTreeObserver() != null && mapView.getViewTreeObserver().isAlive()) {
mapView.getViewTreeObserver().addOnGlobalLayoutListener(this);
}
map.setOnMarkerClickListener(this);
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
map.setMyLocationEnabled(true);
}
map.setOnMapLongClickListener(this);
updateMapElements();
}
}
示例6: addKeyboardVisibilityChangedListener
import android.view.View; //导入方法依赖的package包/类
public static void addKeyboardVisibilityChangedListener(
@NonNull OnKeyboardVisibilityChangedListener listener, @NonNull Activity activity) {
final View activityRootView = ((ViewGroup) activity.findViewById(android.R.id.content))
.getChildAt(0);
ViewTreeObserver viewTreeObserver = activityRootView.getViewTreeObserver();
viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
private final int ESTIMATED_KEYBOARD_SIZE_DP = 100;
private final Rect r = new Rect();
@Override
public void onGlobalLayout() {
float estimatedKeyboardHeight = DeviceUtils.dpToPx(ESTIMATED_KEYBOARD_SIZE_DP);
activityRootView.getWindowVisibleDisplayFrame(r);
int heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top);
boolean isVisible = (heightDiff >= estimatedKeyboardHeight);
listener.onVisibilityChanged(isVisible);
}
});
}
示例7: addOnGlobalLayoutListener
import android.view.View; //导入方法依赖的package包/类
/**
* Add an OnGlobalLayoutListener for the view.
* This is just a convenience method for using {@code ViewTreeObserver.OnGlobalLayoutListener()}.
* This also handles removing listener when onGlobalLayout is called.
*
* @param view the target view to add global layout listener
* @param runnable runnable to be executed after the view is laid out
*/
public static void addOnGlobalLayoutListener(final View view, final Runnable runnable) {
ViewTreeObserver vto = view.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
} else {
view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
runnable.run();
}
});
}
示例8: getWidgetWH
import android.view.View; //导入方法依赖的package包/类
/**
* 直接获取控件的宽、高
* @param view
* @return int[]
*/
public static int[] getWidgetWH(final View view){
ViewTreeObserver vto2 = view.getViewTreeObserver();
vto2.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
});
return new int[]{view.getWidth(),view.getHeight()};
}
示例9: getViewHeight
import android.view.View; //导入方法依赖的package包/类
/**
* 直接获取控件的宽、高
* @param view
* @return int[]
*/
public static int getViewHeight(final View view){
ViewTreeObserver vto2 = view.getViewTreeObserver();
vto2.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
});
return view.getHeight();
}
示例10: getViewWidth
import android.view.View; //导入方法依赖的package包/类
/**
* 直接获取控件的宽、高
* @param view
* @return int[]
*/
public static int getViewWidth(final View view){
ViewTreeObserver vto2 = view.getViewTreeObserver();
vto2.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
});
return view.getWidth();
}
示例11: registerListener
import android.view.View; //导入方法依赖的package包/类
private void registerListener(View anchor) {
if (mSuperScrollListener != null) {
ViewTreeObserver vto = (anchor.getWindowToken() != null) ? anchor.getViewTreeObserver() : null;
if (vto != mViewTreeObserver) {
if (mViewTreeObserver != null && mViewTreeObserver.isAlive()) {
mViewTreeObserver.removeOnScrollChangedListener(mSuperScrollListener);
}
if ((mViewTreeObserver = vto) != null) {
vto.addOnScrollChangedListener(mSuperScrollListener);
}
}
}
}
示例12: tryShow
import android.view.View; //导入方法依赖的package包/类
public boolean tryShow() {
boolean addGlobalListener = false;
this.mPopup = new ListPopupWindow(this.mContext, null, this.mPopupStyleAttr, this.mPopupStyleRes);
this.mPopup.setOnDismissListener(this);
this.mPopup.setOnItemClickListener(this);
this.mPopup.setAdapter(this.mAdapter);
this.mPopup.setModal(true);
View anchor = this.mAnchorView;
if (anchor == null) {
return false;
}
if (this.mTreeObserver == null) {
addGlobalListener = true;
}
this.mTreeObserver = anchor.getViewTreeObserver();
if (addGlobalListener) {
this.mTreeObserver.addOnGlobalLayoutListener(this);
}
this.mPopup.setAnchorView(anchor);
this.mPopup.setDropDownGravity(this.mDropDownGravity);
if (!this.mHasContentWidth) {
this.mContentWidth = measureContentWidth();
this.mHasContentWidth = true;
}
this.mPopup.setContentWidth(this.mContentWidth);
this.mPopup.setInputMethodMode(2);
this.mPopup.show();
this.mPopup.getListView().setOnKeyListener(this);
return true;
}
示例13: animationReveal
import android.view.View; //导入方法依赖的package包/类
protected void animationReveal(Bundle savedInstanceState) {
cx = getResources().getDisplayMetrics().widthPixels - 120;
cy = getResources().getDisplayMetrics().heightPixels - 180;
final View rootLayout = getWindow().getDecorView().getRootView();
if (savedInstanceState == null) {
rootLayout.setVisibility(View.INVISIBLE);
ViewTreeObserver viewTreeObserver = rootLayout.getViewTreeObserver();
if (viewTreeObserver.isAlive()) {
viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
float finalRadius = Math.max(rootLayout.getWidth(), rootLayout.getHeight());
// create the animator for this view (the start radius is zero)
Animator circularReveal = ViewAnimationUtils.createCircularReveal(rootLayout, cx, cy, 0, finalRadius);
circularReveal.setDuration(revealTime);
// make the view visible and start the animation
rootLayout.setVisibility(View.VISIBLE);
circularReveal.start();
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
rootLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
} else {
rootLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
}
});
}
}
}
示例14: onLaidOut
import android.view.View; //导入方法依赖的package包/类
/**
* Executes the given {@link java.lang.Runnable} when the view is laid out
**/
public static void onLaidOut(final View view, final Runnable runnable) {
if (isLaidOut(view)) {
runnable.run();
return;
}
final ViewTreeObserver observer = view.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
final ViewTreeObserver trueObserver;
if (observer.isAlive()) {
trueObserver = observer;
} else {
trueObserver = view.getViewTreeObserver();
}
if (Build.VERSION.SDK_INT >= 16) {
trueObserver.removeOnGlobalLayoutListener(this);
} else {
//noinspection deprecation
trueObserver.removeGlobalOnLayoutListener(this);
}
runnable.run();
}
});
}
示例15: startAnimation
import android.view.View; //导入方法依赖的package包/类
public static MoveData startAnimation(Context context, final View toView, Bundle transitionBundle, Bundle savedInstanceState, final int duration, final TimeInterpolator interpolator) {
final TransitionData transitionData = new TransitionData(context, transitionBundle);
if (transitionData.imageFilePath != null) {
setImageToView(toView, transitionData.imageFilePath);
}
final MoveData moveData = new MoveData();
moveData.toView = toView;
moveData.duration = duration;
if (savedInstanceState == null) {
ViewTreeObserver observer = toView.getViewTreeObserver();
observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
toView.getViewTreeObserver().removeOnPreDrawListener(this);
int[] screenLocation = new int[2];
toView.getLocationOnScreen(screenLocation);
moveData.leftDelta = transitionData.thumbnailLeft - screenLocation[0];
moveData.topDelta = transitionData.thumbnailTop - screenLocation[1];
moveData.widthScale = (float) transitionData.thumbnailWidth / toView.getWidth();
moveData.heightScale = (float) transitionData.thumbnailHeight / toView.getHeight();
runEnterAnimation(moveData, interpolator);
return true;
}
});
}
return moveData;
}