下面列出了android.view.View#isAttachedToWindow ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Guarantee order: Parent -> Child
* Make sure that Parent will not overwrite Child when adding Overlay
*/
public static List<NonNullPair<String, View>> sortSharedElementList(ArrayMap<String, View> sharedElements) {
List<NonNullPair<String, View>> list = new ArrayList<>();
boolean isFirstRun = true;
while (!sharedElements.isEmpty()) {
final int numSharedElements = sharedElements.size();
for (int i = numSharedElements - 1; i >= 0; i--) {
final View view = sharedElements.valueAt(i);
final String name = sharedElements.keyAt(i);
if (isFirstRun && (view == null || !view.isAttachedToWindow() || name == null)) {
sharedElements.removeAt(i);
} else if (!isNested(view, sharedElements)) {
list.add(NonNullPair.create(name, view));
sharedElements.removeAt(i);
}
}
isFirstRun = false;
}
return list;
}
/**
* Iterates over the shared elements and adds them to the members in order.
* Shared elements that are nested in other shared elements are placed after the
* elements that they are nested in. This means that layout ordering can be done
* from first to last.
*
* @param sharedElements The map of transition names to shared elements to set into
* the member fields.
*/
private void setSharedElements(ArrayMap<String, View> sharedElements) {
boolean isFirstRun = true;
while (!sharedElements.isEmpty()) {
final int numSharedElements = sharedElements.size();
for (int i = numSharedElements - 1; i >= 0; i--) {
final View view = sharedElements.valueAt(i);
final String name = sharedElements.keyAt(i);
if (isFirstRun && (view == null || !view.isAttachedToWindow() || name == null)) {
sharedElements.removeAt(i);
} else if (!isNested(view, sharedElements)) {
mSharedElementNames.add(name);
mSharedElements.add(view);
sharedElements.removeAt(i);
}
}
isFirstRun = false;
}
}
public void viewInstancesReady(ArrayList<String> accepted, ArrayList<String> localNames,
ArrayList<View> localViews) {
boolean remap = false;
for (int i = 0; i < localViews.size(); i++) {
View view = localViews.get(i);
if (!TextUtils.equals(view.getTransitionName(), localNames.get(i))
|| !view.isAttachedToWindow()) {
remap = true;
break;
}
}
if (remap) {
triggerViewsReady(mapNamedElements(accepted, localNames));
} else {
triggerViewsReady(mapSharedElements(accepted, localViews));
}
}
/** @hide */
protected void attachToAnchor(View anchor, int xoff, int yoff, int gravity) {
detachFromAnchor();
final ViewTreeObserver vto = anchor.getViewTreeObserver();
if (vto != null) {
vto.addOnScrollChangedListener(mOnScrollChangedListener);
}
anchor.addOnAttachStateChangeListener(mOnAnchorDetachedListener);
final View anchorRoot = anchor.getRootView();
anchorRoot.addOnAttachStateChangeListener(mOnAnchorRootDetachedListener);
anchorRoot.addOnLayoutChangeListener(mOnLayoutChangeListener);
mAnchor = new WeakReference<>(anchor);
mAnchorRoot = new WeakReference<>(anchorRoot);
mIsAnchorRootAttached = anchorRoot.isAttachedToWindow();
mParentRootView = mAnchorRoot;
mAnchorXoff = xoff;
mAnchorYoff = yoff;
mAnchoredGravity = gravity;
}
protected void moveSharedElementsToOverlay() {
if (mWindow == null || !mWindow.getSharedElementsUseOverlay()) {
return;
}
setSharedElementMatrices();
int numSharedElements = mSharedElements.size();
ViewGroup decor = getDecor();
if (decor != null) {
boolean moveWithParent = moveSharedElementWithParent();
Matrix tempMatrix = new Matrix();
for (int i = 0; i < numSharedElements; i++) {
View view = mSharedElements.get(i);
if (view.isAttachedToWindow()) {
tempMatrix.reset();
mSharedElementParentMatrices.get(i).invert(tempMatrix);
GhostView.addGhost(view, decor, tempMatrix);
ViewGroup parent = (ViewGroup) view.getParent();
if (moveWithParent && !isInTransitionGroup(parent, decor)) {
GhostViewListeners listener = new GhostViewListeners(view, parent, decor);
parent.getViewTreeObserver().addOnPreDrawListener(listener);
parent.addOnAttachStateChangeListener(listener);
mGhostViewListeners.add(listener);
}
}
}
}
}
private void alignToAnchor() {
final View anchor = mAnchor != null ? mAnchor.get() : null;
if (anchor != null && anchor.isAttachedToWindow() && hasDecorView()) {
final WindowManager.LayoutParams p = getDecorViewLayoutParams();
updateAboveAnchor(findDropDownPosition(anchor, p, mAnchorXoff, mAnchorYoff,
p.width, p.height, mAnchoredGravity, false));
update(p.x, p.y, -1, -1, true);
}
}
private boolean isAttachedToHierarchy(View v) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
return v.isAttachedToWindow();
} else {
return v.getParent() != null; // Best-effort fallback
}
}
@TargetApi(Build.VERSION_CODES.KITKAT)
public static boolean isAttachedToWindow(View view) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
return view.isAttachedToWindow();
} else {
return view.getWindowToken() != null;
}
}
private boolean isAttachedToHierarchy(View v) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
return v.isAttachedToWindow();
} else {
return v.getParent() != null; // Best-effort fallback
}
}
public void animate(View action, View view) {
view.bringToFront();
view.setVisibility(View.VISIBLE);
if (action == null)
return;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
if (!action.isAttachedToWindow() || !view.isAttachedToWindow())
return;
// finding X and Y co-ordinates
int[] coordinateAction = {0, 0};
int[] coordinateView = {0, 0};
action.getLocationInWindow(coordinateAction);
view.getLocationInWindow(coordinateView);
int cx = (coordinateAction[0] + action.getWidth() / 2);
int cy = (0 - coordinateView[1] + coordinateAction[1] + action.getHeight() / 2);
// to find radius when icon is tapped for showing layout
int startRadius = 0;
int endRadius = Math.max(view.getWidth() + cx, view.getHeight() + cy);
// performing circular reveal when icon will be tapped
Animator animator = ViewAnimationUtils.createCircularReveal(view, cx, cy, startRadius, endRadius);
animator.setInterpolator(new AccelerateDecelerateInterpolator());
animator.setDuration(350);
// to show the layout when icon is tapped
animator.start();
}
}
private static boolean isAttachedToWindow(View view) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
return view.getWindowToken() != null;
} else {
return view.isAttachedToWindow();
}
}
@TargetApi(Build.VERSION_CODES.KITKAT)
public static boolean isViewAttachedToWindow(View v) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
return v.isAttachedToWindow();
} else {
// A proxy call which returns null, if the view is not attached to the window.
return v.getKeyDispatcherState() != null;
}
}
private boolean isAttachedToHierarchy(View v) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
return v.isAttachedToWindow();
} else {
return v.getParent() != null; // Best-effort fallback
}
}
private void checkCameraViewPosition() {
if (!deviceHasGoodCamera) {
return;
}
int count = attachPhotoRecyclerView.getChildCount();
for (int a = 0; a < count; a++) {
View child = attachPhotoRecyclerView.getChildAt(a);
if (child instanceof PhotoAttachCameraCell) {
if (Build.VERSION.SDK_INT >= 19) {
if (!child.isAttachedToWindow()) {
break;
}
}
child.getLocationInWindow(cameraViewLocation);
cameraViewLocation[0] -= getLeftInset();
float listViewX = listView.getX() + backgroundPaddingLeft - getLeftInset();
if (cameraViewLocation[0] < listViewX) {
cameraViewOffsetX = (int) (listViewX - cameraViewLocation[0]);
if (cameraViewOffsetX >= AndroidUtilities.dp(80)) {
cameraViewOffsetX = 0;
cameraViewLocation[0] = AndroidUtilities.dp(-150);
cameraViewLocation[1] = 0;
} else {
cameraViewLocation[0] += cameraViewOffsetX;
}
} else {
cameraViewOffsetX = 0;
}
if (Build.VERSION.SDK_INT >= 21 && cameraViewLocation[1] < AndroidUtilities.statusBarHeight) {
cameraViewOffsetY = AndroidUtilities.statusBarHeight - cameraViewLocation[1];
if (cameraViewOffsetY >= AndroidUtilities.dp(80)) {
cameraViewOffsetY = 0;
cameraViewLocation[0] = AndroidUtilities.dp(-150);
cameraViewLocation[1] = 0;
} else {
cameraViewLocation[1] += cameraViewOffsetY;
}
} else {
cameraViewOffsetY = 0;
}
applyCameraViewPosition();
return;
}
}
cameraViewOffsetX = 0;
cameraViewOffsetY = 0;
cameraViewLocation[0] = AndroidUtilities.dp(-150);
cameraViewLocation[1] = 0;
applyCameraViewPosition();
}
private void checkCameraViewPosition() {
if (Build.VERSION.SDK_INT >= 21) {
if (cameraView != null) {
cameraView.invalidateOutline();
}
RecyclerView.ViewHolder holder = gridView.findViewHolderForAdapterPosition(itemsPerRow - 1);
if (holder != null) {
holder.itemView.invalidateOutline();
}
if (!adapter.needCamera || !deviceHasGoodCamera || selectedAlbumEntry != galleryAlbumEntry) {
holder = gridView.findViewHolderForAdapterPosition(0);
if (holder != null) {
holder.itemView.invalidateOutline();
}
}
}
if (!deviceHasGoodCamera) {
return;
}
int count = gridView.getChildCount();
for (int a = 0; a < count; a++) {
View child = gridView.getChildAt(a);
if (child instanceof PhotoAttachCameraCell) {
if (Build.VERSION.SDK_INT >= 19) {
if (!child.isAttachedToWindow()) {
break;
}
}
child.getLocationInWindow(cameraViewLocation);
cameraViewLocation[0] -= parentAlert.getLeftInset();
float listViewX = gridView.getX() - parentAlert.getLeftInset();
if (cameraViewLocation[0] < listViewX) {
cameraViewOffsetX = (int) (listViewX - cameraViewLocation[0]);
if (cameraViewOffsetX >= itemSize) {
cameraViewOffsetX = 0;
cameraViewLocation[0] = AndroidUtilities.dp(-400);
cameraViewLocation[1] = 0;
} else {
cameraViewLocation[0] += cameraViewOffsetX;
}
} else {
cameraViewOffsetX = 0;
}
int maxY = (Build.VERSION.SDK_INT >= 21 ? AndroidUtilities.statusBarHeight : 0) + ActionBar.getCurrentActionBarHeight();
if (cameraViewLocation[1] < maxY) {
cameraViewOffsetY = maxY - cameraViewLocation[1];
if (cameraViewOffsetY >= itemSize) {
cameraViewOffsetY = 0;
cameraViewLocation[0] = AndroidUtilities.dp(-400);
cameraViewLocation[1] = 0;
} else {
cameraViewLocation[1] += cameraViewOffsetY;
}
} else {
cameraViewOffsetY = 0;
}
int containerHeight = parentAlert.getSheetContainer().getMeasuredHeight();
int keyboardSize = parentAlert.sizeNotifierFrameLayout.getKeyboardHeight();
if (!AndroidUtilities.isInMultiwindow && keyboardSize <= AndroidUtilities.dp(20)) {
containerHeight -= parentAlert.commentTextView.getEmojiPadding();
}
maxY = (int) (containerHeight - parentAlert.buttonsRecyclerView.getMeasuredHeight() + parentAlert.buttonsRecyclerView.getTranslationY() + parentAlert.getSheetContainer().getTranslationY());
if (cameraViewLocation[1] + itemSize > maxY) {
cameraViewOffsetBottomY = cameraViewLocation[1] + itemSize - maxY;
if (cameraViewOffsetBottomY >= itemSize) {
cameraViewOffsetBottomY = 0;
cameraViewLocation[0] = AndroidUtilities.dp(-400);
cameraViewLocation[1] = 0;
}
} else {
cameraViewOffsetBottomY = 0;
}
applyCameraViewPosition();
return;
}
}
cameraViewOffsetX = 0;
cameraViewOffsetY = 0;
cameraViewLocation[0] = AndroidUtilities.dp(-400);
cameraViewLocation[1] = 0;
applyCameraViewPosition();
}
private static boolean isAttachedToHierarchy(View v) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
return v.isAttachedToWindow();
}
return v.getParent() != null; // Best-effort fallback (without adding support-v4 just for this...)
}
private static boolean isAttachedToHierarchy(View v) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
return v.isAttachedToWindow();
}
return v.getParent() != null; // Best-effort fallback
}
/**
* Default constructor.
*
* @param promptOptions The options used to create the prompt.
*/
MaterialTapTargetPrompt(final PromptOptions promptOptions)
{
final ResourceFinder resourceFinder = promptOptions.getResourceFinder();
mView = new PromptView(resourceFinder.getContext());
mView.mPrompt = this;
mView.mPromptOptions = promptOptions;
mView.mPromptTouchedListener = new PromptView.PromptTouchedListener()
{
@Override
public void onFocalPressed()
{
if (!isDismissing())
{
onPromptStateChanged(STATE_FOCAL_PRESSED);
if (mView.mPromptOptions.getAutoFinish())
{
finish();
}
}
}
@Override
public void onNonFocalPressed()
{
if (!isDismissing())
{
onPromptStateChanged(STATE_NON_FOCAL_PRESSED);
if (mView.mPromptOptions.getAutoDismiss())
{
dismiss();
}
}
}
@Override
public void onBackButtonPressed()
{
if (!isDismissing())
{
onPromptStateChanged(STATE_BACK_BUTTON_PRESSED);
onPromptStateChanged(STATE_NON_FOCAL_PRESSED);
if (mView.mPromptOptions.getAutoDismiss())
{
dismiss();
}
}
}
};
Rect rect = new Rect();
resourceFinder.getPromptParentView().getWindowVisibleDisplayFrame(rect);
mStatusBarHeight = rect.top;
mGlobalLayoutListener = () -> {
final View targetView = mView.mPromptOptions.getTargetView();
if (targetView != null)
{
final boolean isTargetAttachedToWindow;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
{
isTargetAttachedToWindow = targetView.isAttachedToWindow();
}
else
{
isTargetAttachedToWindow = targetView.getWindowToken() != null;
}
if (!isTargetAttachedToWindow)
{
return;
}
}
prepare();
if (mAnimationCurrent == null)
{
// Force a relayout to update the view's location
updateAnimation(1, 1);
}
};
}
private void checkCameraViewPosition() {
if (Build.VERSION.SDK_INT >= 21) {
if (cameraView != null) {
cameraView.invalidateOutline();
}
RecyclerView.ViewHolder holder = gridView.findViewHolderForAdapterPosition(itemsPerRow - 1);
if (holder != null) {
holder.itemView.invalidateOutline();
}
if (!adapter.needCamera || !deviceHasGoodCamera || selectedAlbumEntry != galleryAlbumEntry) {
holder = gridView.findViewHolderForAdapterPosition(0);
if (holder != null) {
holder.itemView.invalidateOutline();
}
}
}
if (!deviceHasGoodCamera) {
return;
}
int count = gridView.getChildCount();
for (int a = 0; a < count; a++) {
View child = gridView.getChildAt(a);
if (child instanceof PhotoAttachCameraCell) {
if (Build.VERSION.SDK_INT >= 19) {
if (!child.isAttachedToWindow()) {
break;
}
}
child.getLocationInWindow(cameraViewLocation);
cameraViewLocation[0] -= parentAlert.getLeftInset();
float listViewX = gridView.getX() - parentAlert.getLeftInset();
if (cameraViewLocation[0] < listViewX) {
cameraViewOffsetX = (int) (listViewX - cameraViewLocation[0]);
if (cameraViewOffsetX >= itemSize) {
cameraViewOffsetX = 0;
cameraViewLocation[0] = AndroidUtilities.dp(-400);
cameraViewLocation[1] = 0;
} else {
cameraViewLocation[0] += cameraViewOffsetX;
}
} else {
cameraViewOffsetX = 0;
}
int maxY = (Build.VERSION.SDK_INT >= 21 ? AndroidUtilities.statusBarHeight : 0) + ActionBar.getCurrentActionBarHeight();
if (cameraViewLocation[1] < maxY) {
cameraViewOffsetY = maxY - cameraViewLocation[1];
if (cameraViewOffsetY >= itemSize) {
cameraViewOffsetY = 0;
cameraViewLocation[0] = AndroidUtilities.dp(-400);
cameraViewLocation[1] = 0;
} else {
cameraViewLocation[1] += cameraViewOffsetY;
}
} else {
cameraViewOffsetY = 0;
}
int containerHeight = parentAlert.getSheetContainer().getMeasuredHeight();
int keyboardSize = parentAlert.sizeNotifierFrameLayout.getKeyboardHeight();
if (!AndroidUtilities.isInMultiwindow && keyboardSize <= AndroidUtilities.dp(20)) {
containerHeight -= parentAlert.commentTextView.getEmojiPadding();
}
maxY = (int) (containerHeight - parentAlert.buttonsRecyclerView.getMeasuredHeight() + parentAlert.buttonsRecyclerView.getTranslationY() + parentAlert.getSheetContainer().getTranslationY());
if (cameraViewLocation[1] + itemSize > maxY) {
cameraViewOffsetBottomY = cameraViewLocation[1] + itemSize - maxY;
if (cameraViewOffsetBottomY >= itemSize) {
cameraViewOffsetBottomY = 0;
cameraViewLocation[0] = AndroidUtilities.dp(-400);
cameraViewLocation[1] = 0;
}
} else {
cameraViewOffsetBottomY = 0;
}
applyCameraViewPosition();
return;
}
}
cameraViewOffsetX = 0;
cameraViewOffsetY = 0;
cameraViewLocation[0] = AndroidUtilities.dp(-400);
cameraViewLocation[1] = 0;
applyCameraViewPosition();
}
/**
* @param view The view whose requests need preparation. It must be attached to a
* window. This object will retain a weak reference to this view, and will unregister itself
* from AccessibilityManager if the view is detached from a window. It will not re-register
* itself.
* @param requestTypes The types of requests that require preparation. Different types may
* be ORed together.
*
* @throws IllegalStateException if the view is not attached to a window.
*/
public AccessibilityRequestPreparer(View view, @RequestTypes int requestTypes) {
if (!view.isAttachedToWindow()) {
throw new IllegalStateException("View must be attached to a window");
}
mViewRef = new WeakReference<>(view);
mRequestTypes = requestTypes;
view.addOnAttachStateChangeListener(new ViewAttachStateListener());
}