このページのチュートリアルに従い、コードを少し変更することで、(ほぼ)機能させたいものを得ることができました。
私がやりたいのは、テキストビューの中心からy軸を中心に0度から90度までテキストビューを反転することです。テキストビューが画面上の垂直線になったら、テキストビューにテキストを設定し、-90から0に回転して戻します。基本的に、テキストビューを裏返し、その背後にあるテキストを表示したような効果を与えます。
これを実行しようとしているのは、x度からy度に回転するフリップアニメーションを定義することです。これを初めて呼び出すときは、0から90に設定し、アニメーションリスナーを設定します。アニメーションが完了すると、アニメーションリスナーはテキストビューにテキストを設定し、-90から0までの新しいアニメーションを開始します。
2番目のアニメーションは意図したとおりに機能しているように見えますが、最初のアニメーションはテキストビューの左端を中心に回転しているように見えます。以下の図(すべてトップダウンビュー):
太い黒い線は、テキストビューの開始位置を表します(トップダウンビュー)。赤い矢印の線はテキストビューが回転している方向を示し、点線はアニメーション化後のテキストビューの位置を示しています。
私が使用しているコードは次のとおりです。
FlipAnimation(チュートリアルから直接取得):
public class FlipAnimation extends Animation {
private final float mFromDegrees;
private final float mToDegrees;
private final float mCenterX;
private final float mCenterY;
private Camera mCamera;
public FlipAnimation(float fromDegrees, float toDegrees, float centerX, float centerY) {
mFromDegrees = fromDegrees;
mToDegrees = toDegrees;
mCenterX = centerX;
mCenterY = centerY;
}
@Override
public void initialize(int width, int height, int parentWidth, int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
mCamera = new Camera();
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
final float fromDegrees = mFromDegrees;
float degrees = fromDegrees + ((mToDegrees - fromDegrees) * interpolatedTime);
final float centerX = mCenterX;
final float centerY = mCenterY;
final Camera camera = mCamera;
final Matrix matrix = t.getMatrix();
camera.save();
camera.rotateY(degrees);
camera.getMatrix(matrix);
camera.restore();
matrix.preTranslate(-centerX, -centerY);
matrix.postTranslate(centerX, centerY);
}
フリップアニメーションリスナー:
public final class FlipAnimationListener implements AnimationListener {
private TextView flippingView;
private String newText;
public FlipAnimationListener(TextView flippingView, String newText) {
this.flippingView = flippingView;
this.newText = newText;
}
public void onAnimationEnd(Animation animation) {
flippingView.setText(newText);
final float centerX = flippingView.getWidth() / 2.0f;
final float centerY = flippingView.getHeight() / 2.0f;
final FlipAnimation rotation = new FlipAnimation(-90, 0, centerX, centerY);
rotation.setDuration(2000);
rotation.setFillAfter(true);
rotation.setInterpolator(new DecelerateInterpolator());
flippingView.startAnimation(rotation);
}
public void onAnimationRepeat(Animation animation) {}
public void onAnimationStart(Animation animation) {}
}
アクティビティのフリッピングコード:
private void startFlips() {
TextView viewToChange = (TextView) findViewById(R.id.viewToChange);
final float centerX = viewToChange.getWidth() / 2.0f;
final float centerY = viewToChange.getHeight() / 2.0f;
final FlipAnimation rotation = new FlipAnimation(0, 90, centerX, centerY);
rotation.setDuration(2000);
rotation.setFillAfter(true);
rotation.setInterpolator(new AccelerateInterpolator());
rotation.setAnimationListener(new FlipAnimationListener(viewToChange, "test"));
viewToChange.startAnimation(rotation);
}
長い投稿について申し訳ありませんが、うまくいけば私は何も見逃していませんでした!見てくれてありがとう:)
編集:このタイプのアニメーションを処理するためのより良い方法がある場合は、私にも知らせてください!よろしく。