android.view.View#setTranslationX ( )源码实例Demo

下面列出了android.view.View#setTranslationX ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: PageTransformerHelp   文件: ABaseTransformer.java
/**
	 * 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);
	}
}
 
源代码3 项目: ViewPagerTabIndicator   文件: BaseTransformer.java
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);
    }
}
 
源代码4 项目: duo-navigation-drawer   文件: DuoDrawerLayout.java
/**
 * 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);

	}
}
 
源代码7 项目: brickkit-android   文件: StickyViewBehavior.java
/**
 * 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);
			}
		}
	}
}
 
源代码10 项目: ECardFlow   文件: ZoomOutPageTransformer.java
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);
}
 
源代码12 项目: weex-uikit   文件: TransformItemDecoration.java
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));
  }

}
 
源代码13 项目: BaseProject   文件: FadeSlideTransformer.java
@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));
    }
}
 
源代码14 项目: Banner   文件: ForegroundToBackgroundTransformer.java
@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);
}
 
源代码15 项目: litho   文件: DynamicPropsManager.java
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;
  }
}
 
源代码16 项目: FloatingMusicMenu   文件: FloatingMusicMenu.java
/**
 * 摆放朝左展开方向的子控件位置
 */
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;
    }
}
 
源代码17 项目: newsApp   文件: DepthPageTransformer.java
@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);
}
 
源代码19 项目: weex-uikit   文件: WXSliderNeighbor.java
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);
}
 
源代码20 项目: KJFrameForAndroid   文件: ViewHelper.java
static void setTranslationX(View view, float translationX) {
    view.setTranslationX(translationX);
}
 
 方法所在类
 同类方法