下面列出了怎么用android.view.ViewGroupOverlay的API类实例代码及写法,或者点击链接到github查看源代码。
protected void moveSharedElementsFromOverlay() {
int numListeners = mGhostViewListeners.size();
for (int i = 0; i < numListeners; i++) {
GhostViewListeners listener = mGhostViewListeners.get(i);
listener.removeListener();
}
mGhostViewListeners.clear();
if (mWindow == null || !mWindow.getSharedElementsUseOverlay()) {
return;
}
ViewGroup decor = getDecor();
if (decor != null) {
ViewGroupOverlay overlay = decor.getOverlay();
int count = mSharedElements.size();
for (int i = 0; i < count; i++) {
View sharedElement = mSharedElements.get(i);
GhostView.removeGhost(sharedElement);
}
}
}
public FastScroller(@NonNull ViewGroup view, @NonNull ViewHelper viewHelper,
@Nullable Rect padding, @NonNull Drawable trackDrawable,
@NonNull Drawable thumbDrawable, @NonNull Consumer<TextView> popupStyle,
@NonNull AnimationHelper animationHelper) {
mMinTouchTargetSize = view.getResources().getDimensionPixelSize(
R.dimen.afs_min_touch_target_size);
Context context = view.getContext();
mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
mView = view;
mViewHelper = viewHelper;
mUserPadding = padding;
mAnimationHelper = animationHelper;
mTrackWidth = trackDrawable.getIntrinsicWidth();
mThumbWidth = thumbDrawable.getIntrinsicWidth();
mThumbHeight = thumbDrawable.getIntrinsicHeight();
mTrackView = new View(context);
mTrackView.setBackground(trackDrawable);
mThumbView = new View(context);
mThumbView.setBackground(thumbDrawable);
mPopupView = new AppCompatTextView(context);
mPopupView.setLayoutParams(new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
popupStyle.accept(mPopupView);
ViewGroupOverlay overlay = mView.getOverlay();
overlay.add(mTrackView);
overlay.add(mThumbView);
overlay.add(mPopupView);
postAutoHideScrollbar();
mPopupView.setAlpha(0);
mViewHelper.addOnPreDrawListener(this::onPreDraw);
mViewHelper.addOnScrollChangedListener(this::onScrollChanged);
mViewHelper.addOnTouchEventListener(this::onTouchEvent);
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
private void applyDim(Activity activity) {
ViewGroup parent = (ViewGroup) activity.getWindow().getDecorView().getRootView();
//activity跟布局
// ViewGroup parent = (ViewGroup) parent1.getChildAt(0);
Drawable dimDrawable = new ColorDrawable(mDimColor);
dimDrawable.setBounds(0, 0, parent.getWidth(), parent.getHeight());
dimDrawable.setAlpha((int) (255 * mDimValue));
ViewGroupOverlay overlay = parent.getOverlay();
overlay.add(dimDrawable);
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
private void applyDim(ViewGroup dimView) {
Drawable dimDrawable = new ColorDrawable(mDimColor);
dimDrawable.setBounds(0, 0, dimView.getWidth(), dimView.getHeight());
dimDrawable.setAlpha((int) (255 * mDimValue));
ViewGroupOverlay overlay = dimView.getOverlay();
overlay.add(dimDrawable);
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
private void clearDim(Activity activity) {
ViewGroup parent = (ViewGroup) activity.getWindow().getDecorView().getRootView();
//activity跟布局
// ViewGroup parent = (ViewGroup) parent1.getChildAt(0);
ViewGroupOverlay overlay = parent.getOverlay();
overlay.clear();
}
static void applyDim(@NonNull ViewGroup parent){
Drawable dim = new ColorDrawable(Color.BLACK);
dim.setBounds(0, 0, parent.getWidth(), parent.getHeight());
dim.setAlpha((int) (255 * 0.5));
ViewGroupOverlay overlay = parent.getOverlay();
overlay.add(dim);
}
public FastScroller(AbsListView listView, int styleResId) {
mList = listView;
mOldItemCount = listView.getCount();
mOldChildCount = listView.getChildCount();
final Context context = listView.getContext();
mScaledTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
mScrollBarStyle = listView.getScrollBarStyle();
mScrollCompleted = true;
mState = STATE_VISIBLE;
mMatchDragPosition =
context.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.HONEYCOMB;
mTrackImage = new ImageView(context);
mTrackImage.setScaleType(ScaleType.FIT_XY);
mThumbImage = new ImageView(context);
mThumbImage.setScaleType(ScaleType.FIT_XY);
mPreviewImage = new View(context);
mPreviewImage.setAlpha(0f);
mPrimaryText = createPreviewTextView(context);
mSecondaryText = createPreviewTextView(context);
mMinimumTouchTarget = listView.getResources().getDimensionPixelSize(
com.android.internal.R.dimen.fast_scroller_minimum_touch_target);
setStyle(styleResId);
final ViewGroupOverlay overlay = listView.getOverlay();
mOverlay = overlay;
overlay.add(mTrackImage);
overlay.add(mThumbImage);
overlay.add(mPreviewImage);
overlay.add(mPrimaryText);
overlay.add(mSecondaryText);
getSectionsFromIndexer();
updateLongList(mOldChildCount, mOldItemCount);
setScrollbarPosition(listView.getVerticalScrollbarPosition());
postAutoHide();
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
private void clearDim(ViewGroup dimView) {
ViewGroupOverlay overlay = dimView.getOverlay();
overlay.clear();
}
static void clearDim(@NonNull ViewGroup parent) {
ViewGroupOverlay overlay = parent.getOverlay();
overlay.clear();
}
public static void reveal(Activity activity, View view, View sourceView, int colorRes) {
if (activity == null || view == null || sourceView == null) return;
if (isLollipop()) {
final ViewGroupOverlay groupOverlay =
(ViewGroupOverlay) activity.getWindow().getDecorView().getOverlay();
final Rect displayRect = new Rect();
view.getGlobalVisibleRect(displayRect);
// Make reveal cover the display and status bar.
final View revealView = new View(activity);
revealView.setTop(displayRect.top);
revealView.setBottom(displayRect.bottom);
revealView.setLeft(displayRect.left);
revealView.setRight(displayRect.right);
revealView.setBackgroundColor(ContextCompat.getColor(activity, colorRes));
groupOverlay.add(revealView);
final int[] clearLocation = new int[2];
sourceView.getLocationInWindow(clearLocation);
clearLocation[0] += sourceView.getWidth() / 2;
clearLocation[1] += sourceView.getHeight() / 2;
final int revealCenterX = clearLocation[0] - revealView.getLeft();
final int revealCenterY = clearLocation[1] - revealView.getTop();
final double x1_2 = Math.pow(revealView.getLeft() - revealCenterX, 2);
final double x2_2 = Math.pow(revealView.getRight() - revealCenterX, 2);
final double y_2 = Math.pow(revealView.getTop() - revealCenterY, 2);
final float revealRadius = (float) Math.max(Math.sqrt(x1_2 + y_2), Math.sqrt(x2_2 + y_2));
try {
final Animator revealAnimator =
ViewAnimationUtils.createCircularReveal(revealView,
revealCenterX, revealCenterY, 0.0f, revealRadius);
revealAnimator.setDuration(
activity.getResources().getInteger(android.R.integer.config_mediumAnimTime));
final Animator alphaAnimator = ObjectAnimator.ofFloat(revealView, View.ALPHA, 0.0f);
alphaAnimator.setDuration(
activity.getResources().getInteger(android.R.integer.config_shortAnimTime));
alphaAnimator.addListener(new AnimatorListenerAdapter() {
@Override public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
view.startAnimation(AnimationUtils.loadAnimation(activity, R.anim.abc_fade_in));
view.setVisibility(View.VISIBLE);
}
});
final AnimatorSet animatorSet = new AnimatorSet();
animatorSet.play(revealAnimator).before(alphaAnimator);
animatorSet.setInterpolator(new AccelerateDecelerateInterpolator());
animatorSet.addListener(new AnimatorListenerAdapter() {
@Override public void onAnimationEnd(Animator animator) {
groupOverlay.remove(revealView);
}
});
animatorSet.start();
} catch (IllegalStateException e) {
Timber.i("View is detached - not animating");
}
} else {
view.setVisibility(View.VISIBLE);
}
}