gpt4 book ai didi

android - 在 Android 中翻转两个面的硬币

转载 作者:太空狗 更新时间:2023-10-29 12:47:01 25 4
gpt4 key购买 nike

我正在使用这个 Rotate3dAnimation 类创建一个抛硬币动画,它也在移动和缩放。但我只能将它用于一个 ImageView 。只需在该 ImageView 上使用 startAnimation() 方法。

但我想做的是,使用硬币的两个面,这样它看起来就像一个真正的硬币,有两个不同的面正在翻转。有人可以帮助我了解如何做到这一点吗?

谢谢

    package com.example.movingcoin;



import android.view.animation.Animation;
import android.view.animation.Transformation;
import android.graphics.Camera;
import android.graphics.Matrix;

/**
* An animation that rotates the view on the Y axis between two specified angles.
* This animation also adds a translation on the Z axis (depth) to improve the effect.
*/
public class Rotate3dAnimation extends Animation {
private final float mFromDegrees;
private final float mToDegrees;
private final float mCenterX;
private final float mCenterY;
private final float mDepthZ;
private final boolean mReverse;
private Camera mCamera;

/**
* Creates a new 3D rotation on the Y axis. The rotation is defined by its
* start angle and its end angle. Both angles are in degrees. The rotation
* is performed around a center point on the 2D space, definied by a pair
* of X and Y coordinates, called centerX and centerY. When the animation
* starts, a translation on the Z axis (depth) is performed. The length
* of the translation can be specified, as well as whether the translation
* should be reversed in time.
*
* @param fromDegrees the start angle of the 3D rotation
* @param toDegrees the end angle of the 3D rotation
* @param centerX the X center of the 3D rotation
* @param centerY the Y center of the 3D rotation
* @param reverse true if the translation should be reversed, false otherwise
*/
public Rotate3dAnimation(float fromDegrees, float toDegrees,
float centerX, float centerY, float depthZ, boolean reverse) {
mFromDegrees = fromDegrees;
mToDegrees = toDegrees;
mCenterX = centerX;
mCenterY = centerY;
mDepthZ = depthZ;
mReverse = reverse;
}

@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();
if (mReverse) {
camera.translate(0.0f, 0.0f, mDepthZ * interpolatedTime);
} else {
camera.translate(0.0f, 0.0f, mDepthZ * (1.0f - interpolatedTime));
}

// camera.rotateY(degrees);
camera.rotateX(degrees);

camera.getMatrix(matrix);
camera.restore();

matrix.preTranslate(-centerX, -centerY);
matrix.postTranslate(centerX, centerY);
}
}

最佳答案

几天前遇到了同样的问题,在 FlipAnimator 类中找到了解决方案,您可以在这里找到:FlipAnimatorClass

实际上,这非常简单:您只需将硬币的两面传递给 FlipAnimator。我认为该类(class)非常容易理解,并且实际上是在执行 g00dy 在他上面的评论中建议的内容。

关于android - 在 Android 中翻转两个面的硬币,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17213025/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com