gpt4 book ai didi

android - 在旧 android 上显示卡片翻转动画

转载 作者:IT老高 更新时间:2023-10-28 22:15:50 25 4
gpt4 key购买 nike

我们都知道 article如何创建"card filp"使用 new api 的动画.但是我怎样才能做到 on apis < 3.0 ?

更新:

只要有像 android-FlipView 这样好的且易于使用的库我不认为你真的需要自己做这样的动画。

最佳答案

找到了答案。如果你想在 ALL ANDROID VERSIONS 上做翻转动画,使用这个:

Activity 布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_activity_root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/transparent" >

<RelativeLayout
android:id="@+id/main_activity_card_face"
android:layout_width="300dp"
android:layout_height="407dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@drawable/front"
android:clickable="true"
android:onClick="onCardClick"
android:padding="5dp" >
</RelativeLayout>

<RelativeLayout
android:id="@+id/main_activity_card_back"
android:layout_width="300dp"
android:layout_height="407dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@drawable/back"
android:clickable="true"
android:onClick="onCardClick"
android:visibility="gone" >
</RelativeLayout>

</RelativeLayout>

当布局文件翻转两个 View 组时,您可以在 View 组中放置任何其他内容,它应该可以工作。现在让我们看看 Activity 中处理调用翻转动画代码的方法:

public void onCardClick(View view)
{
flipCard();
}

private void flipCard()
{
View rootLayout = findViewById(R.id.main_activity_root);
View cardFace = findViewById(R.id.main_activity_card_face);
View cardBack = findViewById(R.id.main_activity_card_back);

FlipAnimation flipAnimation = new FlipAnimation(cardFace, cardBack);

if (cardFace.getVisibility() == View.GONE)
{
flipAnimation.reverse();
}
rootLayout.startAnimation(flipAnimation);
}

最后是 FlipAnimation 类:

public class FlipAnimation extends Animation
{
private Camera camera;

private View fromView;
private View toView;

private float centerX;
private float centerY;

private boolean forward = true;

/**
* Creates a 3D flip animation between two views.
*
* @param fromView First view in the transition.
* @param toView Second view in the transition.
*/
public FlipAnimation(View fromView, View toView)
{
this.fromView = fromView;
this.toView = toView;

setDuration(700);
setFillAfter(false);
setInterpolator(new AccelerateDecelerateInterpolator());
}

public void reverse()
{
forward = false;
View switchView = toView;
toView = fromView;
fromView = switchView;
}

@Override
public void initialize(int width, int height, int parentWidth, int parentHeight)
{
super.initialize(width, height, parentWidth, parentHeight);
centerX = width/2;
centerY = height/2;
camera = new Camera();
}

@Override
protected void applyTransformation(float interpolatedTime, Transformation t)
{
// Angle around the y-axis of the rotation at the given time
// calculated both in radians and degrees.
final double radians = Math.PI * interpolatedTime;
float degrees = (float) (180.0 * radians / Math.PI);

// Once we reach the midpoint in the animation, we need to hide the
// source view and show the destination view. We also need to change
// the angle by 180 degrees so that the destination does not come in
// flipped around
if (interpolatedTime >= 0.5f)
{
degrees -= 180.f;
fromView.setVisibility(View.GONE);
toView.setVisibility(View.VISIBLE);
}

if (forward)
degrees = -degrees; //determines direction of rotation when flip begins

final Matrix matrix = t.getMatrix();
camera.save();
camera.rotateY(degrees);
camera.getMatrix(matrix);
camera.restore();
matrix.preTranslate(-centerX, -centerY);
matrix.postTranslate(centerX, centerY);
}

这里是原始帖子的链接: Displaying card flip animation on old android

更新来自@FMMobileFelipeMenezes。

如果想要平滑缩放的动画翻转,将这部分代码更改为(applyTransformation):

final Matrix matrix = t.getMatrix();
camera.save();
camera.translate(0, 0, Math.abs(degrees)*2);
camera.getMatrix(matrix);
camera.rotateY(degrees);
camera.getMatrix(matrix);
camera.restore();
matrix.preTranslate(-centerX, -centerY);
matrix.postTranslate(centerX, centerY);

更新来自@Hesam我建议阅读它的好教程。尽管它不如基于 Fragment 的 Android 教程好,但如果您想将动画分配给布局和 View 以及在旧 API 上使用它,那么它值得一读并很有用。

Use Android's scale animation to simulate a 3D flip

Improved project on github by @LenaBru

关于android - 在旧 android 上显示卡片翻转动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16030667/

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