下面列出了android.support.v4.view.ViewCompat#postInvalidateOnAnimation ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Smoothly animate mDraggingPane to the target X position within its range.
*
* @param slideOffset position to animate to
* @param velocity initial velocity in case of fling, or 0.
*/
boolean smoothSlideTo(float slideOffset, int velocity) {
if (!isEnabled() || mSlideableView == null) {
// Nothing to do.
return false;
}
int panelTop = computePanelTopPosition(slideOffset);
if (mDragHelper.smoothSlideViewTo(mSlideableView, mSlideableView.getLeft(), panelTop)) {
setAllChildrenVisible();
ViewCompat.postInvalidateOnAnimation(this);
return true;
}
return false;
}
/**
* Smoothly animate mDraggingPane to the target X position within its range.
*
* @param slideOffset position to animate to
* @param velocity initial velocity in case of fling, or 0.
*/
boolean smoothSlideTo(float slideOffset, int velocity) {
if (!mCanSlide) {
// Nothing to do.
return false;
}
final int topBound = getSlidingTop();
int y = mIsSlidingUp
? (int) (topBound + slideOffset * mSlideRange)
: (int) (topBound - slideOffset * mSlideRange);
if (mDragHelper.smoothSlideViewTo(mSlideableView, mSlideableView.getLeft(), y)) {
setAllChildrenVisible();
ViewCompat.postInvalidateOnAnimation(this);
return true;
}
return false;
}
private void calculateOffsets(final float fraction) {
interpolateBounds(fraction);
mCurrentDrawX = lerp(mExpandedDrawX, mCollapsedDrawX, fraction,
mPositionInterpolator);
mCurrentDrawY = lerp(mExpandedDrawY, mCollapsedDrawY, fraction,
mPositionInterpolator);
setInterpolatedTextSize(lerp(mExpandedTextSize, mCollapsedTextSize,
fraction, mTextSizeInterpolator));
if (mCollapsedTextColor != mExpandedTextColor) {
// If the collapsed and expanded text colors are different, blend them based on the
// fraction
mTextPaint.setColor(blendColors(mExpandedTextColor, mCollapsedTextColor, fraction));
} else {
mTextPaint.setColor(mCollapsedTextColor);
}
mTextPaint.setShadowLayer(
lerp(mExpandedShadowRadius, mCollapsedShadowRadius, fraction, null),
lerp(mExpandedShadowDx, mCollapsedShadowDx, fraction, null),
lerp(mExpandedShadowDy, mCollapsedShadowDy, fraction, null),
blendColors(mExpandedShadowColor, mCollapsedShadowColor, fraction));
ViewCompat.postInvalidateOnAnimation(mView);
}
@Override
public void computeScroll() {
mScrimOpacity = 1 - mScrollPercent;
if (mScrimOpacity >= 0) {
if (mDragHelper.continueSettling(true)) {
ViewCompat.postInvalidateOnAnimation(this);
}
computeScrollPreView();
}
}
@Override
public void computeScroll() {
super.computeScroll();
if (mDragHelper.continueSettling(true)) {
ViewCompat.postInvalidateOnAnimation(this);
}
}
@Override
public void computeScroll() {
if (mDragHelper != null && mDragHelper.continueSettling(true)) {
if (!isEnabled()) {
mDragHelper.abort();
return;
}
ViewCompat.postInvalidateOnAnimation(this);
}
}
@Override
public void computeScroll() {
mScrimOpacity = 1 - mScrollPercent;
if (mDragHelper.continueSettling(true)) {
ViewCompat.postInvalidateOnAnimation(this);
}
}
/**
* 关闭
*
* @param isSmooth 是否平滑的关闭
*/
public void close(boolean isSmooth) {
int finalLeft = 0;
if (isSmooth) {
/**
* public boolean smoothSlideViewTo(View child, int finalLeft, int finalTop)方法的解释
*
* Animate the view <code>child</code> to the given (left, top) position.
* If this method returns true, the caller should invoke {@link #continueSettling(boolean)}
* on each subsequent frame to continue the motion until it returns false. If this method
* returns false there is no further work to do to complete the movement.
*
* 返回true 代表还没有移动到指定的位置,需要刷新界面,继续移动
* 返回false 就停止工作哈
*/
//1、触发动画
if (mDragHelper.smoothSlideViewTo(mMainContent, finalLeft, 0)) {
//参数传this,也就是child所在的viewgroup
ViewCompat.postInvalidateOnAnimation(this);
}
} else {
//调用layout方法,摆放主布局
/**
* @param l Left position, relative to parent
* @param t Top position, relative to parent
* @param r Right position, relative to parent
* @param b Bottom position, relative to parent
*/
mMainContent.layout(finalLeft, 0, finalLeft + mWidth, finalLeft + mHeight);
}
}
private void setJoiningFraction(int leftDot, float fraction) {
if (joiningFractions != null) {
if (leftDot < joiningFractions.length) {
joiningFractions[leftDot] = fraction;
ViewCompat.postInvalidateOnAnimation(this);
}
}
}
@Override
public void computeScroll() {
if (mDragHelper.continueSettling(true)) {
if (!mCanSlide) {
mDragHelper.abort();
return;
}
ViewCompat.postInvalidateOnAnimation(this);
}
}
@Override
protected void drawableStateChanged() {
super.drawableStateChanged();
if (mBorderDrawable != null && mBorderDrawable.isStateful()) {
mBorderDrawable.setState(getDrawableState());
}
if (mMaskDrawable != null && mMaskDrawable.isStateful()) {
mMaskDrawable.setState(getDrawableState());
}
if (isDuplicateParentStateEnabled()) {
ViewCompat.postInvalidateOnAnimation(this);
}
}
/**
* Like {@link View#scrollBy}, but scroll smoothly instead of immediately.
*
* @param dx the number of pixels to scroll by on the X axis
* @param dy the number of pixels to scroll by on the Y axis
*/
public final void smoothScrollBy(int dx, int dy) {
if (getChildCount() == 0) {
// Nothing to do.
return;
}
long duration = AnimationUtils.currentAnimationTimeMillis() - mLastScroll;
if (duration > ANIMATED_SCROLL_GAP) {
final int width = getWidth() - getPaddingRight() - getPaddingLeft();
final int height = getHeight() - getPaddingBottom() - getPaddingTop();
final int right = getChildAt(0).getWidth();
final int bottom = getChildAt(0).getHeight();
final int maxX = Math.max(0, right - width);
final int maxY = Math.max(0, bottom - height);
final int scrollX = getScrollX();
final int scrollY = getScrollY();
dx = Math.max(0, Math.min(scrollX + dx, maxX)) - scrollX;
dy = Math.max(0, Math.min(scrollY + dy, maxY)) - scrollY;
mScroller.startScroll(scrollX, scrollY, dx, dy);
ViewCompat.postInvalidateOnAnimation(this);
} else {
if (!mScroller.isFinished()) {
mScroller.abortAnimation();
}
scrollBy(dx, dy);
}
mLastScroll = AnimationUtils.currentAnimationTimeMillis();
}
@Override
public void computeScroll() {
if (mDragHelper.continueSettling(true)) {
ViewCompat.postInvalidateOnAnimation(this);
}
}
void smoothScrollToVertical(int x, int y, int velocity) {
if (getChildCount() == 0) {
// Nothing to do.
setScrollingCacheEnabled(false);
return;
}
int sy;
boolean wasScrolling = (mScroller != null) && !mScroller.isFinished();
if (wasScrolling) {
// We're in the middle of a previously initiated scrolling. Check to see
// whether that scrolling has actually started (if we always call getStartX
// we can get a stale value from the scroller if it hadn't yet had its first
// computeScrollOffset call) to decide what is the current scrolling position.
sy = mIsScrollStarted ? mScroller.getCurrY() : mScroller.getStartY();
// And abort the current scrolling.
mScroller.abortAnimation();
setScrollingCacheEnabled(false);
} else {
//getScrollX() http://www.bubuko.com/infodetail-916594.html
sy = getScrollY();
}
int sx = getScrollX();
int dx = x - sx;
int dy = y - sy;
if (dx == 0 && dy == 0) {
// TODO: 2017/2/11 completeScroll
completeScrollVertical(false);
populateVertical();
setScrollState(SCROLL_STATE_IDLE);
return;
}
setScrollingCacheEnabled(true);
setScrollState(SCROLL_STATE_SETTLING);
final int height = getClientHeight();
final int halfHeight = height / 2;
final float distanceRatio = Math.min(1f, 1.0f * Math.abs(dy) / height);
final float distance = halfHeight + halfHeight
* distanceInfluenceForSnapDuration(distanceRatio);
int duration;
velocity = Math.abs(velocity);
if (velocity > 0) {
duration = 4 * Math.round(1000 * Math.abs(distance / velocity));
} else {
final float pageHeight = height * mAdapter.getPageWidth(mCurItem);
final float pageDelta = (float) Math.abs(dy) / (pageHeight + mPageMargin);
duration = (int) ((pageDelta + 1) * 100);
}
duration = Math.min(duration, MAX_SETTLE_DURATION);
// Reset the "scroll started" flag. It will be flipped to true in all places
// where we call computeScrollOffset().
mIsScrollStarted = false;
mScroller.startScroll(sx, sy, dx, dy, duration);
ViewCompat.postInvalidateOnAnimation(this);
}
private void smoothScrollToX(int finalLeft) {
if (viewDragHelper.settleCapturedViewAt(finalLeft, 0)) {
ViewCompat.postInvalidateOnAnimation(SwipeBackLayout.this);
}
}
public void invalidateChildRegion(SlidingLayout parent, View child) {
ViewCompat.postInvalidateOnAnimation(parent, child.getLeft(), child.getTop(),
child.getRight(), child.getBottom());
}
private void updateVisualizerWave(byte[] data) {
waveformBytes = data;
ViewCompat.postInvalidateOnAnimation(this);
}
@Override
public void setMaxZoom(float maxZoom) {
chartComputator.setMaxZoom(maxZoom);
ViewCompat.postInvalidateOnAnimation(this);
}
@Override
public void set(DrawShadowFrameLayout dsfl, Float value) {
dsfl.mAlpha = value;
ViewCompat.postInvalidateOnAnimation(dsfl);
}
void setTriggerPercentage(float triggerPercentage) {
this.mTriggerPercentage = triggerPercentage;
this.mStartTime = 0;
ViewCompat.postInvalidateOnAnimation(this.mParent, this.mBounds.left, this.mBounds.top, this.mBounds.right, this.mBounds.bottom);
}