在Android中翻转双面硬币
我正在使用这个RotateDaniation类创建一个翻转硬币的动画,它也在移动和缩放。但我只能在一个图像视图中使用它。只需在该图像视图上使用startAnimation()方法 但我想做的是,使用硬币的两面,让它看起来像一个真正的硬币,两面都在翻转。有人能帮我怎么做吗 谢谢在Android中翻转双面硬币,android,animation,rotation,imageview,android-animation,Android,Animation,Rotation,Imageview,Android Animation,我正在使用这个RotateDaniation类创建一个翻转硬币的动画,它也在移动和缩放。但我只能在一个图像视图中使用它。只需在该图像视图上使用startAnimation()方法 但我想做的是,使用硬币的两面,让它看起来像一个真正的硬币,两面都在翻转。有人能帮我怎么做吗 谢谢 package com.example.movingcoin; import android.view.animation.Animation; import android.view.ani
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类中找到了解决方案,您可以在这里找到:
事实上,这很容易:你只需要把硬币的两面传给FlipAnimator。我认为这个类很容易理解,实际上它正在做g00dy在上面评论中建议的事情。诀窍是将视图旋转两次! 从正常位置转到中间位置后,更改视图(例如更改硬币的图像),然后将其从中间旋转回正常位置 您应该在第一个动画的AnimationListener中的onAnimationEnd中执行视图中的所有更改并开始从中间到法线的旋转 像这样:
firstAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
findViewById(R.id.conceptsLay).setVisibility(View.GONE);
findViewById(R.id.factBaseLay).setVisibility(View.VISIBLE);
secondAnimation.startAnimation();
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
在上面的代码中,我首先将conceptsLay旋转到中间,它基本上是不可见的,然后使它消失,并使其他示例视图可见,并从中间开始动画到正常!
因此,用户看到的是视图被翻转!
也就是说,首先将其从0旋转到90,然后在第二个动画中,将其从-90旋转到0
为了使它更平滑,我还添加了一些alpha动画!
希望这将有助于此转换发生了什么-硬币背面看起来如何-是正面的镜像图像吗?是的,它是X轴图像的镜像版本可以这样做:如果将转换应用于第二个图像(尾侧,如果我们考虑当前的头侧)并在不同的位置更改这两个的可见性,这可能会带来恶作剧-嗯?链接不再直接带您到源代码,因为它已存档,您必须下载源代码并在
androidwidgets/trunk/FlipAnimatorExample/src/com/beanie/examples/animation/FlipAnimator/FlipAnimator.java下导航