下面列出了android.view.View#setRotation ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Nullable
@Override
public Animator createAnimator(@NonNull ViewGroup sceneRoot, @Nullable TransitionValues startValues,
@Nullable TransitionValues endValues) {
if (startValues == null || endValues == null) {
return null;
}
final View view = endValues.view;
float startRotation = (Float) startValues.values.get(PROPNAME_ROTATION);
float endRotation = (Float) endValues.values.get(PROPNAME_ROTATION);
if (startRotation != endRotation) {
view.setRotation(startRotation);
return ObjectAnimator.ofFloat(view, View.ROTATION,
startRotation, endRotation);
}
return null;
}
@Override
public void screenScrolled(View v, int i, float scrollProgress) {
float rotation =
(mUp ? TRANSITION_SCREEN_ROTATION : -TRANSITION_SCREEN_ROTATION) * scrollProgress;
float translationX = v.getMeasuredWidth() * scrollProgress;
float rotatePoint =
(v.getMeasuredWidth() * 0.5f) /
(float) Math.tan(Math.toRadians((double) (TRANSITION_SCREEN_ROTATION * 0.5f)));
v.setPivotX(v.getMeasuredWidth() * 0.5f);
if (mUp) {
v.setPivotY(-rotatePoint);
} else {
v.setPivotY(v.getMeasuredHeight() + rotatePoint);
}
v.setRotation(rotation);
v.setTranslationX(translationX);
}
@Override
public void transformItem(View view, float position, boolean isHorizontal, int scrolled) {
float rotation;
float width = view.getWidth();
float height = view.getHeight();
if (isHorizontal) {
rotation = ROT_MOD * position * -1.25f;
view.setPivotX(width * 0.5f);
view.setPivotY(height);
view.setTranslationX(0f);
} else {
rotation = ROT_MOD * position;
view.setPivotY(height * 0.5f);
view.setPivotX(view.getWidth());
view.setTranslationY(0f);
}
view.setRotation(rotation);
}
/**
* Given that the orientation is {@link Orientation#HORIZONTAL}, apply rotation if enabled.
*/
private void setChildRotationHorizontal(@Gravity int gravity, View child, int radius, Point center) {
if (!rotate) {
child.setRotation(0);
return;
}
boolean childPastCenter = (child.getX() + child.getWidth() / 2) > center.x;
float directionMult;
if (gravity == Gravity.END) {
directionMult = childPastCenter ? 1 : -1;
} else {
directionMult = childPastCenter ? -1 : 1;
}
final float opposite = Math.abs(child.getX() + child.getWidth() / 2.0f - center.x);
child.setRotation((float) (directionMult * Math.toDegrees(Math.asin(opposite / radius))));
}
/**
* 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);
}
}
@Override
protected void onTransform(View view, float position) {
final float width = view.getWidth();
final float rotation = ROT_MOD * position;
view.setPivotX(width * 0.5f);
view.setPivotY(0f);
view.setTranslationX(0f);
view.setRotation(rotation);
}
@Override
public void transformPage(View page, float position) {
final float width = page.getWidth();
final float rotation = ROT_MOD * position;
page.setPivotX(width * 0.5f);
page.setPivotY(0f);
page.setTranslationX(0f);
page.setRotation(rotation);
}
protected void returnItemToPoolIfNeeded(FreeFlowItem freeflowItem) {
View v = freeflowItem.view;
v.setTranslationX(0);
v.setTranslationY(0);
v.setRotation(0);
v.setScaleX(1f);
v.setScaleY(1f);
v.setAlpha(1);
viewpool.returnViewToPool(v);
}
private static void move(View view, TransformInfo info) {
computeRenderOffset(view, info.pivotX, info.pivotY);
adjustTranslation(view, info.deltaX, info.deltaY);
float scale = view.getScaleX() * info.deltaScale;
scale = Math.max(info.minimumScale, Math.min(info.maximumScale, scale));
view.setScaleX(scale);
view.setScaleY(scale);
float rotation = adjustAngle(view.getRotation() + info.deltaAngle);
view.setRotation(rotation);
}
protected void returnItemToPoolIfNeeded(FreeFlowItem freeflowItem) {
View v = freeflowItem.view;
v.setTranslationX(0);
v.setTranslationY(0);
v.setRotation(0);
v.setScaleX(1f);
v.setScaleY(1f);
v.setAlpha(1);
viewpool.returnViewToPool(v);
}
@Override
protected void onTransform(View view, float position) {
final float width = view.getWidth();
final float height = view.getHeight();
final float rotation = ROT_MOD * position * -1.25f;
view.setPivotX(width * 0.5f);
view.setPivotY(height);
view.setRotation(rotation);
}
@Override
protected void onTransform(View view, float position) {
final float width = view.getWidth();
final float rotation = ROT_MOD * position;
view.setPivotX(width * 0.5f);
view.setPivotY(0f);
view.setTranslationX(0f);
view.setRotation(rotation);
}
@Override
protected void onTransform(View view, float position) {
final float width = view.getWidth();
final float height = view.getHeight();
final float rotation = ROT_MOD * position * -1.25f;
view.setPivotX(width * 0.5f);
view.setPivotY(height);
view.setRotation(rotation);
}
private void fillChild(RecyclerView.Recycler recycler) {
float proportion = 1;
int paddingLeft = getPaddingLeft();
int paddingRight = getPaddingRight();
int paddingTop = getPaddingTop();
int paddingBottom = getPaddingBottom();
float perHeight = mYInterval / (mCount - 1);
float perWidth = mXInterval / (mCount - 1);
perHeight += perHeight / (mCount - 1);
perWidth += perWidth / (mCount - 1);
for (int i = mTopPosition; i < mTopPosition + mCount && i < getItemCount(); i++) {
View child = mViewCaches.get(i);
if (child == null) {
child = recycler.getViewForPosition(i);
child.setTranslationX(0);
child.setTranslationY(0);
child.setRotation(0);
addView(child, 0);
measureChildWithMargins(child, 0, 0);
int width = getDecoratedMeasurementHorizontal(child);
int height = getDecoratedMeasurementVertical(child);
int left = (getWidth() - width + paddingLeft - paddingRight) / 2;
int right = left + width;
int top = (getHeight() - height + paddingTop - paddingBottom) / 2;
int bottom = top + height;
layoutDecoratedWithMargins(child, left, top, right, bottom);
} else {
attachView(child, 0);
mViewCaches.remove(i);
}
fillChild(child, i, mDx, mDy);
if (i == mTopPosition) {
proportion = layoutTopChild(child);
} else {
int number = i - mTopPosition;
if (i == mTopPosition + mCount - 1) {
proportion = 0;
number -= 1;
}
float origin = 1 - number * SCALE_INTERVAL;
final float target = origin + proportion * SCALE_INTERVAL;
float translationY = (child.getHeight()) * (1 - target) / 2 + (number - proportion) * perHeight;
float translationX = (child.getWidth()) * (1 - target) / 2 + (number - proportion) * perWidth;
child.setScaleX(target);
child.setScaleY(target);
switch (mTranxY) {
case TOP:
translationY *= -1;
case BOTTOM:
child.setTranslationY(translationY);
}
switch (mTranxX) {
case LEFT:
translationX *= -1;
case RIGHT:
child.setTranslationX(translationX);
}
}
}
}
public static void setExpanded(View view, boolean expanded) {
view.setRotation(expanded ? 180.f : 0.f);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
int width = right - left;
int height = bottom - top;
int div = (height - width) / 2;
if (isInLayout) {
RefreshComponent header = mRefreshHeader;
RefreshComponent footer = mRefreshFooter;
final View thisView = this;
int paddingLeft = thisView.getPaddingLeft();
int paddingRight = thisView.getPaddingRight();
int paddingTop = thisView.getPaddingTop();
int paddingBottom = thisView.getPaddingBottom();
for (int i = 0, len = getChildCount(); i < len; i++) {
View child = getChildAt(i);
if (!isRefreshComponent(child) && child.getVisibility() != GONE) {
int t = paddingLeft;
int w = child.getMeasuredWidth();
int h = child.getMeasuredHeight();
int r = width - paddingTop;
//
ViewGroup.LayoutParams params = child.getLayoutParams();
if (params instanceof MarginLayoutParams) {
MarginLayoutParams lp = (MarginLayoutParams) params;
t += lp.leftMargin;
r -= lp.topMargin;
}
//
div = (h - w) / 2;
t -= div;
r -= div;
//
child.setRotation(90);
child.setTag(R.id.srl_tag, "GONE");
child.layout(r - w, t, r, t + h);
}
}
super.onLayout(changed, left, top, right, bottom);
} else {
top -= div;
left += div;
isInLayout = true;
super.layout(left, top, left + width, top + height);
isInLayout = false;
}
}
static void setRotation(View view, float rotation) {
view.setRotation(rotation);
}
@Override
public void setValue(View view, float value) {
view.setRotation(value);
}
static void setRotation(View view, float rotation) {
view.setRotation(rotation);
}
/**
* This method handles setting the property values directly in the View object's fields.
* propertyConstant tells it which property should be set, value is the value to set
* the property to.
*
* @param propertyConstant The property to be set
* @param value The value to set the property to
*/
private void setValue(int propertyConstant, float value) {
//final View.TransformationInfo info = mView.mTransformationInfo;
View v = mView.get();
if (v != null) {
switch (propertyConstant) {
case TRANSLATION_X:
//info.mTranslationX = value;
v.setTranslationX(value);
break;
case TRANSLATION_Y:
//info.mTranslationY = value;
v.setTranslationY(value);
break;
case ROTATION:
//info.mRotation = value;
v.setRotation(value);
break;
case ROTATION_X:
//info.mRotationX = value;
v.setRotationX(value);
break;
case ROTATION_Y:
//info.mRotationY = value;
v.setRotationY(value);
break;
case SCALE_X:
//info.mScaleX = value;
v.setScaleX(value);
break;
case SCALE_Y:
//info.mScaleY = value;
v.setScaleY(value);
break;
case X:
//info.mTranslationX = value - v.mLeft;
v.setX(value);
break;
case Y:
//info.mTranslationY = value - v.mTop;
v.setY(value);
break;
case ALPHA:
//info.mAlpha = value;
v.setAlpha(value);
break;
}
}
}