下面列出了android.view.View#setTranslationY ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void transformItem(View view, float position, boolean isHorizontal, int scrolled) {
if (position < 0f) {
view.setTranslationX(0f);
view.setTranslationY(0f);
view.setScaleX(1f);
view.setScaleY(1f);
view.setAlpha(1f);
return;
}
float scaleFactor = mMinScale + (1 - mMinScale) * (1 - Math.abs(position));
view.setAlpha(1 - position);
view.setScaleX(scaleFactor);
view.setScaleY(scaleFactor);
if (isHorizontal) {
view.setTranslationX(view.getWidth() * -position);
} else {
view.setTranslationY(view.getHeight() * -position);
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if (mode == Mode.NORMAL) return;
int height = getMeasuredHeight();
height = drawWrapButton() ? height + showMoreTextHeight : height;
if (getChildCount() <= mWrapCount) {
setMeasuredDimension(widthMeasureSpec, height);
return;
}
int notWrapContentHeight = 0;
for (int i = 0; i < mWrapCount; i++) {
notWrapContentHeight += getChildAt(i).getMeasuredHeight();
}
notWrapContentHeight = height - notWrapContentHeight;
float delay = notWrapContentHeight - Math.round(notWrapContentHeight * curExpandValue);
for (int i = mWrapCount; i < getChildCount(); i++) {
View child = getChildAt(i);
if (child.getVisibility() != View.GONE) {
Log.i(TAG, "onMeasure: " + delay);
child.setTranslationY(-delay);
}
}
setMeasuredDimension(widthMeasureSpec, (int) (height - delay));
}
@Override
public void onDraw(Canvas c, RecyclerView recyclerView, View view, float dX, float dY,
int actionState, boolean isCurrentlyActive) {
if (Build.VERSION.SDK_INT >= 21) {
if (isCurrentlyActive) {
Object originalElevation = view.getTag();
if (originalElevation == null) {
originalElevation = ViewCompat.getElevation(view);
float newElevation = 1f + findMaxElevation(recyclerView, view);
ViewCompat.setElevation(view, newElevation);
view.setTag(originalElevation);
}
}
}
view.setTranslationX(dX);
view.setTranslationY(dY);
}
protected void onPreTransform(View page, float position) {
final float width = page.getWidth();
page.setRotationX(0);
page.setRotationY(0);
page.setRotation(0);
page.setScaleX(1);
page.setScaleY(1);
page.setPivotX(0);
page.setPivotY(0);
page.setTranslationY(0);
page.setTranslationX(isPagingEnabled() ? 0f : -width * position);
if (hideOffscreenPages()) {
page.setAlpha(position <= -1f || position >= 1f ? 0f : 1f);
// page.setEnabled(false);
} else {
// page.setEnabled(true);
page.setAlpha(1f);
}
}
/**
* Called each {@link #transformPage(android.view.View, float)} before {{@link #onTransform(android.view.View, float)} is called.
*
* @param view
* @param position
*/
protected void onPreTransform(View view, float position) {
final float width = view.getWidth();
view.setRotationX(0);
view.setRotationY(0);
view.setRotation(0);
view.setScaleX(1);
view.setScaleY(1);
view.setPivotX(0);
view.setPivotY(0);
view.setTranslationY(0);
view.setTranslationX(isPagingEnabled() ? 0f : -width * position);
if (hideOffscreenPages()) {
view.setAlpha(position <= -1f || position >= 1f ? 0f : 1f);
} else {
view.setAlpha(1f);
}
}
/**
* Lifting view
*
* @param view The animation target
* @param baseRotation initial Rotation X in 3D space
* @param duration aniamtion duration
* @param startDelay start delay before animation begin
*/
@Deprecated
public static void liftingFromBottom(View view, float baseRotation, int duration, int startDelay){
view.setRotationX(baseRotation);
view.setTranslationY(view.getHeight() / 3);
view
.animate()
.setInterpolator(new AccelerateDecelerateInterpolator())
.setDuration(duration)
.setStartDelay(startDelay)
.rotationX(0)
.translationY(0)
.start();
}
protected void runEnterAnimation(View view, final int position) {
if (position > lastAnimatedPosition) {
lastAnimatedPosition = position;
view.setTranslationY(NumericalUtil.getScreenHeight(mContext));
view.animate()
.translationY(0)
.setInterpolator(new DecelerateInterpolator(3.0f))
.setDuration(ANIMATED_ITEMS_DURATION)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
if (position == mAnimateEndCount||position>=getItemCount()-1) {
AppService.getInstance().getBus().post(new NewsAnimatEndEvent());
}
}
})
.start();
}
}
/**
* Animate FAB on snackbar change.
*/
private void updateFabTranslationForSnackbar(CoordinatorLayout parent, View fab,
View snackbar) {
final float translationY = getFabTranslationYForSnackbar(parent, fab);
if (translationY != this.mTranslationY) {
ViewCompat.animate(fab).cancel();
if (Math.abs(translationY - this.mTranslationY) == (float) snackbar.getHeight()) {
ViewCompat.animate(fab).translationY(translationY).setInterpolator(
new FastOutSlowInInterpolator());
} else {
fab.setTranslationY(translationY);
}
this.mTranslationY = translationY;
}
}
@Override
public void set(View view, PointF value) {
int x = Math.round(value.x);
int y = Math.round(value.y);
int startX = Math.round(endPoint.x);
int startY = Math.round(endPoint.y);
int transY = y - startY;
int transX = x - startX;
view.setTranslationX(transX);
view.setTranslationY(transY);
}
public void slidingByDelta(View view ,float delta){
if(view == null){
return;
}
view.clearAnimation();
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
view.setTranslationY(delta);
}else{
// ViewHelper.setTranslationY(view, delta);
}
}
/**
* Lifting view
*
* @param view The animation target
* @param baseRotation initial Rotation X in 3D space
* @param duration aniamtion duration
*/
@Deprecated
public static void liftingFromBottom(View view, float baseRotation, int duration){
view.setRotationX(baseRotation);
view.setTranslationY(view.getHeight() / 3);
view
.animate()
.setInterpolator(new AccelerateDecelerateInterpolator())
.setDuration(duration)
.rotationX(0)
.translationY(0)
.start();
}
/**
* 摆放朝下展开方向的子控件位置
*/
private void onDownDirectionLayout(int l, int t, int r, int b) {
int centerX = (r - l) / 2;
int offsetY = SHADOW_OFFSET;
View rootView = getChildAt(getChildCount() - 1);
rootView.layout(centerX - rootView.getMeasuredWidth() / 2, offsetY, centerX + rootView.getMeasuredWidth() / 2, offsetY + rootView.getMeasuredHeight());
offsetY += rootView.getMeasuredHeight() + buttonInterval;
for (int i = 0; i < getChildCount() - 1; i++) {
View child = getChildAt(i);
if (child.getVisibility() == GONE)
continue;
int width = child.getMeasuredWidth();
int height = child.getMeasuredHeight();
child.layout(centerX - width / 2, offsetY, centerX + width / 2, offsetY + height);
float collapsedTranslation = -offsetY;
float expandedTranslation = 0f;
child.setTranslationY(isExpanded ? expandedTranslation : collapsedTranslation);
child.setAlpha(isExpanded ? 1f : 0f);
MenuLayoutParams params = (MenuLayoutParams) child.getLayoutParams();
params.collapseDirAnim.setFloatValues(expandedTranslation, collapsedTranslation);
params.expandDirAnim.setFloatValues(collapsedTranslation, expandedTranslation);
params.collapseDirAnim.setProperty(View.TRANSLATION_Y);
params.expandDirAnim.setProperty(View.TRANSLATION_Y);
params.setAnimationsTarget(child);
offsetY += height + buttonInterval;
}
}
@Override
protected void onDisplayDistanceChanged(int distanceXToDisplay, int distanceYToDisplay, int dx, int dy) {
if (!mActivityTranslucentUtil.isTranslucent()) {
return;
}
if (mPreviousActivityContentView != null) {
int translation = 0;
switch (mDirection) {
case DIRECTION_LEFT: translation = initTranslation + (int) (mWidth * mProgress * mRelativeMoveFactor); break;
case DIRECTION_RIGHT: translation = initTranslation - (int) (mWidth * mProgress * mRelativeMoveFactor); break;
case DIRECTION_TOP: translation = initTranslation + (int) (mHeight * mProgress * mRelativeMoveFactor); break;
case DIRECTION_BOTTOM: translation = initTranslation - (int) (mHeight * mProgress * mRelativeMoveFactor); break;
default:
}
movePreviousActivityContentView(translation);
}
boolean horizontal = (mDirection & DIRECTION_HORIZONTAL) > 0;
View contentView = mWrapper.getContentView();
if (contentView != null) {
if (horizontal) {
contentView.setTranslationX(distanceXToDisplay);
} else {
contentView.setTranslationY(distanceYToDisplay);
}
}
layoutScrimView();
}
/**
* Positions the thumb and preview widgets.
*
* @param position The position, between 0 and 1, along the track at which
* to place the thumb.
*/
private void setThumbPos(float position) {
final float thumbMiddle = position * mThumbRange + mThumbOffset;
mThumbImage.setTranslationY(thumbMiddle - mThumbImage.getHeight() / 2f);
final View previewImage = mPreviewImage;
final float previewHalfHeight = previewImage.getHeight() / 2f;
final float previewPos;
switch (mOverlayPosition) {
case OVERLAY_AT_THUMB:
previewPos = thumbMiddle;
break;
case OVERLAY_ABOVE_THUMB:
previewPos = thumbMiddle - previewHalfHeight;
break;
case OVERLAY_FLOATING:
default:
previewPos = 0;
break;
}
// Center the preview on the thumb, constrained to the list bounds.
final Rect container = mContainerRect;
final int top = container.top;
final int bottom = container.bottom;
final float minP = top + previewHalfHeight;
final float maxP = bottom - previewHalfHeight;
final float previewMiddle = MathUtils.constrain(previewPos, minP, maxP);
final float previewTop = previewMiddle - previewHalfHeight;
previewImage.setTranslationY(previewTop);
mPrimaryText.setTranslationY(previewTop);
mSecondaryText.setTranslationY(previewTop);
}
private void resetView(View view) {
view.setAlpha(1f);
view.setScaleX(1f);
view.setScaleY(1f);
view.setRotation(0f);
view.setRotationX(0f);
view.setRotationY(0f);
view.setTranslationX(0f);
view.setTranslationY(0f);
}
public void slidingByDelta(View view ,float delta){
if(view == null){
return;
}
view.clearAnimation();
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
view.setTranslationY(delta);
}else{
ViewHelper.setTranslationY(view, delta);
}
}
@Override
public void onNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull View child, @NonNull View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, int type) {
if (!mCouldScroollOpen) {
if (isClosed(child)) {
return;
}
}
if (mIsFling || dyUnconsumed > 0) {
return;
}
logD(TAG, "onNestedScroll: dyConsumed=" + dyConsumed + " dyUnconsumed= " + dyUnconsumed + "mIsFling " + mIsFling);
float translationY = child.getTranslationY() - dyUnconsumed;
int maxHeadTranslateY = 0;
if (translationY > maxHeadTranslateY) {
translationY = maxHeadTranslateY;
}
logD(TAG, "onNestedScroll: translationY=" + translationY);
if (child.getTranslationY() != translationY) {
onScrollChange(type, (int) translationY);
child.setTranslationY(translationY);
}
}
@Override
public boolean onDependentViewChanged(@NonNull CoordinatorLayout parent, View child, @NonNull View dependency) {
float translationY = Math.min(0, dependency.getTranslationY() - dependency.getHeight());
child.setTranslationY(translationY);
return true;
}
private void createTranslationAnimation(
@NonNull View dependency,
@NonNull View child,
boolean expanded,
boolean currentlyAnimating,
@NonNull FabTransformationSpec spec,
@NonNull List<Animator> animations,
List<AnimatorListener> unusedListeners,
@NonNull RectF childBounds) {
float translationX = calculateTranslationX(dependency, child, spec.positioning);
float translationY = calculateTranslationY(dependency, child, spec.positioning);
ValueAnimator translationXAnimator;
ValueAnimator translationYAnimator;
Pair<MotionTiming, MotionTiming> motionTiming =
calculateMotionTiming(translationX, translationY, expanded, spec);
MotionTiming translationXTiming = motionTiming.first;
MotionTiming translationYTiming = motionTiming.second;
if (expanded) {
if (!currentlyAnimating) {
child.setTranslationX(-translationX);
child.setTranslationY(-translationY);
}
translationXAnimator = ObjectAnimator.ofFloat(child, View.TRANSLATION_X, 0f);
translationYAnimator = ObjectAnimator.ofFloat(child, View.TRANSLATION_Y, 0f);
calculateChildVisibleBoundsAtEndOfExpansion(
child,
spec,
translationXTiming,
translationYTiming,
-translationX,
-translationY,
0f,
0f,
childBounds);
} else {
translationXAnimator = ObjectAnimator.ofFloat(child, View.TRANSLATION_X, -translationX);
translationYAnimator = ObjectAnimator.ofFloat(child, View.TRANSLATION_Y, -translationY);
}
translationXTiming.apply(translationXAnimator);
translationYTiming.apply(translationYAnimator);
animations.add(translationXAnimator);
animations.add(translationYAnimator);
}
@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
float translationY = Math.min(0, dependency.getTranslationY() - dependency.getHeight());
child.setTranslationY(translationY);
return true;
}