Java Code Examples for android.view.View#setTranslationX()

The following examples show how to use android.view.View#setTranslationX() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
/**
	 * 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);
		}
	}
 
Example 2
/**
 * 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);
	}
}
 
Example 3
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);
    }
}
 
Example 4
/**
 * 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);
    }
}
 
Example 5
/**
 * 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);
}
 
Example 6
@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);

	}
}
 
Example 7
/**
 * 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);
}
 
Example 8
@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);
    }
}
 
Example 9
@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);
			}
		}
	}
}
 
Example 10
Source Project: ECardFlow   File: ZoomOutPageTransformer.java    License: MIT License 5 votes vote down vote up
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);
    }
}
 
Example 11
@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);
}
 
Example 12
Source Project: weex-uikit   File: TransformItemDecoration.java    License: MIT License 5 votes vote down vote up
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));
  }

}
 
Example 13
@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));
    }
}
 
Example 14
@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);
}
 
Example 15
Source Project: litho   File: DynamicPropsManager.java    License: Apache License 2.0 5 votes vote down vote up
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;
  }
}
 
Example 16
/**
 * 摆放朝左展开方向的子控件位置
 */
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;
    }
}
 
Example 17
Source Project: newsApp   File: DepthPageTransformer.java    License: Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 18
private void setTranslation(View page) {
    ViewPager viewPager = (ViewPager) page.getParent();
    int scroll = viewPager.getScrollX() - page.getLeft();
    page.setTranslationX(scroll);
}
 
Example 19
Source Project: weex-uikit   File: WXSliderNeighbor.java    License: MIT License 4 votes vote down vote up
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);
}
 
Example 20
Source Project: KJFrameForAndroid   File: ViewHelper.java    License: Apache License 2.0 4 votes vote down vote up
static void setTranslationX(View view, float translationX) {
    view.setTranslationX(translationX);
}