下面列出了android.view.View#setTranslationX ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Called each {@link #transformPage(View, float)} before {{@link #onTransform(View, float)}.
* <p>
* The default implementation attempts to reset all view properties. This is useful when toggling transforms that do
* not modify the same page properties. For instance changing from a transformation that applies rotation to a
* transformation that fades can inadvertently leave a fragment stuck with a rotation or with some degree of applied
* alpha.
*
* @param page
* Apply the transformation to this page
* @param position
* Position of page relative to the current front-and-center position of the pager. 0 is front and
* center. 1 is one full page position to the right, and -1 is one page position to the left.
*/
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);
}
}
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);
}
}
/**
* Show the the touch interceptor.
*/
private void showTouchInterceptor() {
if (findViewWithTag(TAG_OVERLAY) == null) {
addTouchInterceptor();
}
if (mContentView == null) {
mContentView = findViewWithTag(TAG_CONTENT);
}
float offset = map(mContentView.getLeft(), 0, DuoDrawerLayout.this.getWidth() * mMarginFactor, 0, 1);
float scaleFactorContent = map(offset, 0, 1, mContentScaleClosed, mClickToCloseScale);
View interceptor = findViewWithTag(TAG_OVERLAY);
if (interceptor != null) {
interceptor.setTranslationX(mContentView.getLeft());
interceptor.setTranslationY(mContentView.getTop());
interceptor.setScaleX(scaleFactorContent);
interceptor.setScaleY(scaleFactorContent);
interceptor.setVisibility(VISIBLE);
}
}
/**
* Swipe a view by moving distance
*
* @param child a target view
* @param deltaX x moving distance by x-axis.
* @param deltaY y moving distance by y-axis.
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
protected void swipeView(View child, float deltaX, float deltaY) {
if (isFixedView(child)){
deltaX = deltaX / 4;
}
float deltaXAbs = Math.abs(deltaX);
float fractionCovered = deltaXAbs / (float) child.getWidth();
child.setTranslationX(deltaX);
child.setAlpha(1.f - fractionCovered);
if (deltaX > 0)
child.setRotationY(-15.f * fractionCovered);
else
child.setRotationY(15.f * fractionCovered);
}
@Override
public void transformPage(View view, float position) {
int pageWidth = view.getWidth();
if (position < -1) { // [-Infinity,-1)
// This page is way off-screen to the left.
view.setAlpha(0);
} else if (position <= 0) { // [-1,0]
// Use the default slide transition when
// moving to the left page
view.setAlpha(1);
view.setTranslationX(0);
view.setScaleX(1);
view.setScaleY(1);
} else if (position <= 1) { // (0,1]
// Fade the page out.
view.setAlpha(1 - position);
// Counteract the default slide transition
view.setTranslationX(pageWidth * -position);
// Scale the page down (between MIN_SCALE and 1)
float scaleFactor = MIN_SCALE + (1 - MIN_SCALE)
* (1 - Math.abs(position));
view.setScaleX(scaleFactor);
view.setScaleY(scaleFactor);
} else { // (1,+Infinity]
// This page is way off-screen to the right.
view.setAlpha(0);
}
}
/**
* Removes the sticky view from the container and reset the positioning of the container.
*
* @param stickyView stickyView to reset
*/
private static void resetStickyView(RecyclerView.ViewHolder stickyView) {
final View view = stickyView.itemView;
removeViewFromParent(view);
//Reset transformation on removed stickyView
view.setTranslationX(0);
view.setTranslationY(0);
stickyView.setIsRecyclable(true);
}
@Override
protected void onTransform(View page, float position) {
int pageWidth = page.getWidth();
int pageHeight = page.getHeight();
if (position < -1) { // [-Infinity,-1)
// This page is way off-screen to the left.
page.setAlpha(0);
} else if (position <= 1) { // [-1,1]
// Modify the default slide transition to shrink the page as well
float scaleFactor = Math.max(MIN_SCALE, 1 - Math.abs(position));
float vertMargin = pageHeight * (1 - scaleFactor) / 2;
float horzMargin = pageWidth * (1 - scaleFactor) / 2;
if (position < 0) {
page.setTranslationX(horzMargin - vertMargin / 2);
} else {
page.setTranslationX(-horzMargin + vertMargin / 2);
}
// Scale the page down (between MIN_SCALE and 1)
page.setScaleX(scaleFactor);
page.setScaleY(scaleFactor);
// Fade the page relative to its size.
page.setAlpha(MIN_ALPHA +
(scaleFactor - MIN_SCALE) /
(1 - MIN_SCALE) * (1 - MIN_ALPHA));
} else { // (1,+Infinity]
// This page is way off-screen to the right.
page.setAlpha(0);
}
}
@Override
public void transformPage(final View page, final float position) {
// The status of the transformation
final boolean pageIsSelected = (position == 0f);
final boolean pageIsScrolling = (-1f < position && position < 1f);
if (pageIsSelected) {
page.invalidate(); // Make sure page displays correctly
} else if (pageIsScrolling) {
// For every resource ID which has been nominated for a parallax factor
for (final Integer id : parallaxFactors.keySet()) {
// Check to see if the current page has a View with that resource ID
final View viewToTransform = getViewToTransform(page, id);
// If the parallax factor is applicable, apply the parallax effect
if (viewToTransform != null) {
final float parallaxFactor = parallaxFactors.get(id);
// The displacement which is automatically applied by the transformer superclass
final float nominalDisplacement = (page.getWidth() / 2) * position;
// Subtract 1 from the parallax factor because the View is already moved the
// nominal displacement by the transformer superclass
final float modifiedDisplacement = nominalDisplacement * (parallaxFactor - 1);
// Apply the extra displacement using the X translation method
viewToTransform.setTranslationX(modifiedDisplacement);
}
}
}
}
public void transformPage(View view, float position) {
int pageWidth = view.getWidth();
int pageHeight = view.getHeight();
if (position < -1) { // [-Infinity,-1)
// This page is way off-screen to the left.
view.setAlpha(MIN_ALPHA);
view.setScaleX(MIN_SCALE);
view.setScaleY(MIN_SCALE);
} else if (position <= 1) { // [-1,1]
// Modify the default slide transition to shrink the page as well
float scaleFactor = Math.max(MIN_SCALE, 1 - Math.abs(position));
float vertMargin = pageHeight * (1 - scaleFactor) / 2;
float horzMargin = pageWidth * (1 - scaleFactor) / 2;
if (position < 0) {
view.setTranslationX(horzMargin - vertMargin / 2);
view.setScaleX(1 + 0.15f * position);
view.setScaleY(1 + 0.15f * position);
} else {
view.setTranslationX(-horzMargin + vertMargin / 2);
view.setScaleX(1 - 0.15f * position);
view.setScaleY(1 - 0.15f * position);
}
// Scale the page down (between MIN_SCALE and 1)
// Fade the page relative to its size.
view.setAlpha(MIN_ALPHA + (scaleFactor - MIN_SCALE) / (1 - MIN_SCALE) * (1 - MIN_ALPHA));
} else { // (1,+Infinity]
// This page is way off-screen to the right.
view.setScaleX(MIN_SCALE);
view.setScaleY(MIN_SCALE);
view.setAlpha(MIN_ALPHA);
}
}
@Override
protected void onTransform(View view, float position) {
final float height = view.getHeight();
final float width = view.getWidth();
final float scale = min(position < 0 ? 1f : Math.abs(1f - position), 0.5f);
view.setScaleX(scale);
view.setScaleY(scale);
view.setPivotX(width * 0.5f);
view.setPivotY(height * 0.5f);
view.setTranslationX(position < 0 ? width * position : -width * position * 0.25f);
}
private void updateItem(View child, int width, int height) {
int size,childCenter,containerSize;
if (mIsVertical) {
containerSize = height;
size = child.getHeight();
childCenter = child.getTop() + size / 2;
} else {
containerSize = width;
size = child.getWidth();
childCenter = child.getLeft() + size / 2;
}
final int actionDistance = (containerSize + size) / 2;
final float effectsAmount = Math.min(1.0f, Math.max(-1.0f, (1.0f / actionDistance) * (childCenter - containerSize/2)));
if(mAlpha>0){
child.setAlpha(1-mAlpha*Math.abs(effectsAmount));
}
if(mScaleX>0||mScaleY>0){
child.setScaleX(1-mScaleX*Math.abs(effectsAmount));
child.setScaleY(1-mScaleY*Math.abs(effectsAmount));
}
if(mRotation!=0){
child.setRotation(mRotation * effectsAmount);
}
if(mXTranslate!=0){
child.setTranslationX(mXTranslate * Math.abs( effectsAmount));
}
if(mYTranslate!=0){
child.setTranslationY(mYTranslate * Math.abs( effectsAmount));
}
}
@Override
public void transformPage(View page, float position) {
page.setTranslationX(0);
if (position <= -1.0F || position >= 1.0F) {
page.setAlpha( 0.0F);
} else if (position == 0.0F) {
page.setAlpha( 1.0F);
} else {
// position is between -1.0F & 0.0F OR 0.0F & 1.0F
page.setAlpha( 1.0F - Math.abs(position));
}
}
@Override
protected void onTransform(View view, float position) {
final float height = view.getHeight();
final float width = view.getWidth();
final float scale = min(position > 0 ? 1f : Math.abs(1f + position), 0.5f);
view.setScaleX(scale);
view.setScaleY(scale);
view.setPivotX(width * 0.5f);
view.setPivotY(height * 0.5f);
view.setTranslationX(position > 0 ? width * position : -width * position * 0.25f);
}
private void bindCommonDynamicProp(int key, DynamicValue<?> value, View target) {
switch (key) {
case KEY_ALPHA:
target.setAlpha(DynamicPropsManager.<Float>resolve(value));
break;
case KEY_TRANSLATION_X:
target.setTranslationX(DynamicPropsManager.<Float>resolve(value));
break;
case KEY_TRANSLATION_Y:
target.setTranslationY(DynamicPropsManager.<Float>resolve(value));
break;
case KEY_SCALE_X:
target.setScaleX(DynamicPropsManager.<Float>resolve(value));
break;
case KEY_SCALE_Y:
target.setScaleY(DynamicPropsManager.<Float>resolve(value));
break;
case KEY_ELEVATION:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
target.setElevation(DynamicPropsManager.<Float>resolve(value));
}
break;
case KEY_BACKGROUND_COLOR:
target.setBackgroundColor(DynamicPropsManager.<Integer>resolve(value));
break;
case KEY_ROTATION:
target.setRotation(DynamicPropsManager.<Float>resolve(value));
break;
}
}
/**
* 摆放朝左展开方向的子控件位置
*/
private void onLeftDirectionLayout(int l, int t, int r, int b) {
int centerY = (b - t) / 2;
int offsetX = r - l - SHADOW_OFFSET;
for (int i = getChildCount() - 1; i >= 0; i--) {
View child = getChildAt(i);
if (child.getVisibility() == GONE)
continue;
int width = child.getMeasuredWidth();
int height = child.getMeasuredHeight();
child.layout(offsetX - width, centerY - height / 2, offsetX, centerY + height / 2);
//排除根按钮,添加动画
if (i != getChildCount() - 1) {
float collapsedTranslation = r - l - SHADOW_OFFSET - offsetX;
float expandedTranslation = 0f;
child.setTranslationX(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_X);
params.expandDirAnim.setProperty(View.TRANSLATION_X);
params.setAnimationsTarget(child);
}
offsetX -= width + buttonInterval;
}
}
@Override
public void transformPage(View view, float position) {
int pageWidth = view.getWidth();
int pageHeight = view.getHeight();
float alpha = 0;
if (0 <= position && position <= 1) {
alpha = 1 - position;
} else if (-1 < position && position < 0) {
float scaleFactor = Math.max(MIN_SCALE, 1 - Math.abs(position));
float verticalMargin = pageHeight * (1 - scaleFactor) / 2;
float horizontalMargin = pageWidth * (1 - scaleFactor) / 2;
if (position < 0) {
view.setTranslationX(horizontalMargin - verticalMargin / 2);
} else {
view.setTranslationX(-horizontalMargin + verticalMargin / 2);
}
view.setScaleX(scaleFactor);
view.setScaleY(scaleFactor);
alpha = position + 1;
}
view.setAlpha(alpha);
view.setTranslationX(view.getWidth() * -position);
float yPosition = position * view.getHeight();
view.setTranslationY(yPosition);
}
private void setTranslation(View page) {
ViewPager viewPager = (ViewPager) page.getParent();
int scroll = viewPager.getScrollX() - page.getLeft();
page.setTranslationX(scroll);
}
private void moveLeft(View page, float translation, float alpha, float scale) {
updateScaleAndAlpha(((ViewGroup)page).getChildAt(0), alpha, scale);
page.setTranslationX(translation);
((ViewGroup)page).getChildAt(0).setTranslationX(translation);
}
static void setTranslationX(View view, float translationX) {
view.setTranslationX(translationX);
}