要实现您想要的,您需要在动画师中再做两件事:
- 使用非默认枢轴旋转视图(放置在视图中间)
- 旋转时平移视图
在这两种情况下,您都需要知道视图的大小,因此我建议您创建自定义布局组件以用作片段的根,公开一组属性,您可以在您的内部使用不同的objectanimator 进行动画处理xml。
组件应如下所示:
public class FlippableLayout extends FrameLayout {
private FlipEvaluator flipRightInEvaluator;
private FlipEvaluator flipRightOutEvaluator;
private FlipEvaluator flipLeftInEvaluator;
private FlipEvaluator flipLeftOutEvaluator;
public FlippableLayout(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public FlippableLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
setCameraDistance(getCameraDistance() * 10); // reduces perspective skewing
flipRightInEvaluator = new FlipEvaluator(
1f, .5f, // pivotX/pivotY
-1f, 0f, // translationX start/end
-180, 0, // rotationY start/end
0f, 1f); // alpha start/end
flipRightOutEvaluator = new FlipEvaluator(
0f, .5f,
0f, 1f,
0, 180,
1f, 0f);
flipLeftInEvaluator = new FlipEvaluator(
.0f, .5f,
1f, 0f,
180, 0,
0f, 1f);
flipLeftOutEvaluator = new FlipEvaluator(
1f, .5f,
0f, -1f,
0, -180,
1f, 0f);
}
public void setFlipRightIn(float value) {
evaluateUsing(flipRightInEvaluator, value);
}
public void setFlipRightOut(float value) {
evaluateUsing(flipRightOutEvaluator, value);
}
public void setFlipLeftIn(float value) {
evaluateUsing(flipLeftInEvaluator, value);
}
public void setFlipLeftOut(float value) {
evaluateUsing(flipLeftOutEvaluator, value);
}
private void evaluateUsing(FlipEvaluator evaluator, float value) {
float cappedValue = Math.min(1f, Math.max(0f, value));
setPivotX(getWidth() * evaluator.getPivotX());
setPivotY(getHeight() * evaluator.getPivotY());
setAlpha(evaluator.getAlpha(cappedValue));
setTranslationX(getWidth() * evaluator.getTranslationX(cappedValue));
setRotationY(evaluator.getRotationY(cappedValue));
}
private static class FlipEvaluator {
private final float pivotX;
private final float pivotY;
private final float startTranslationX;
private final float endTranslationY;
private final float startRotationY;
private final float endRotationY;
private final float startAlpha;
private final float endAlpha;
/**
* Simple evaluator holding all the start/end values for a flip animation.
*
* @param pivotX value between 0 and 1, where 0 is the left border and 1 is the right border of the target
* @param pivotY value between 0 and 1, where 0 is the top border and 1 is the bottom border of the target
* @param startTranslationX value between 0 and 1, where 1 is the width of the target
* @param endTranslationY value between 0 and 1, where 1 is the width of the target
* @param startRotationY value between -180 and 180
* @param endRotationY value between -180 and 180
* @param startAlpha initial alpha
* @param endAlpha final alpha
*/
private FlipEvaluator(float pivotX, float pivotY,
float startTranslationX, float endTranslationY,
float startRotationY, float endRotationY,
float startAlpha, float endAlpha) {
this.pivotX = pivotX;
this.pivotY = pivotY;
this.startTranslationX = startTranslationX;
this.endTranslationY = endTranslationY;
this.startRotationY = startRotationY;
this.endRotationY = endRotationY;
this.startAlpha = startAlpha;
this.endAlpha = endAlpha;
}
public float getPivotX() {
return pivotX;
}
public float getPivotY() {
return pivotY;
}
public float getTranslationX(float t) {
return startTranslationX + (endTranslationY - startTranslationX) * t;
}
public float getRotationY(float t) {
return startRotationY + (endRotationY - startRotationY) * t;
}
public float getAlpha(float t) {
return t < .5f ? startAlpha : endAlpha;
}
}
}
您的动画文件将如下所示:
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:valueFrom="0"
android:valueTo="1"
android:propertyName="flipLeftIn"
android:interpolator="@android:interpolator/accelerate_decelerate"
android:duration="1000" />
当然,您可以将flipLeftIn 更改为flipLeftOut、flipRightIn 或flipRightOut,以便将动画师应用于不同的属性。
在您的Activity 中,您可以像往常一样在片段事务中设置自定义动画,指定您之前定义的 XML:
....
getFragmentManager()
.beginTransaction()
.setCustomAnimations(
R.animator.card_flip_right_in, R.animator.card_flip_right_out,
R.animator.card_flip_left_in, R.animator.card_flip_left_out)
....
另一种方法是在 XML 中完成所有操作,但使用通过 XML 定义的维度值设置数据透视/转换的可扩展性不如上面显示的解决方案。
编辑
要减少相机距离,您可以在 API>12 上轻松使用View.setCameraDistance()。我更新了包含此更改的 sn-p。