我找到了以下代码,用于翻转包含图表的两个CardViews (我使用MPAndroidCharts库),从而创建了3D卡翻转效果:
public class FlipAnimation extends Animation {
...
@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();
if(horizontal){
camera.rotateX(degrees);
} else {
camera.rotateY(degrees);
}
camera.getMatrix(matrix);
camera.restore();
matrix.preTranslate(-centerX, -centerY);
matrix.postTranslate(centerX, centerY);
}
原始代码只能沿垂直中心轴翻转,所以我在设置布尔值时添加了小if
,然后沿水平中心轴制作动画。现在,有时在动画过程中,以下奇怪的错误会使我的应用程序崩溃:
03-20 16:37:26.369 551-579/c.f.q.android.demo A/OpenGLRenderer: Error: Spot pair overflow!!! used 30, total 22
03-20 16:37:26.370 551-579/c.f.q.android.demo A/libc: Fatal signal 6 (SIGABRT), code -6 in tid 579 (hwuiTask1)
这个错误似乎只发生在沿水平轴旋转时,但即使在这种情况下,错误也不会持续发生(例如,它有时工作无数次,有时在第二次翻转时崩溃)。
发布于 2016-05-16 00:02:37
你的卡视图需要阴影吗?我能够通过将CardView的高度设置为0dp来修复我的错误。所以在xml中:-
<android.support.v7.widget.CardView xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:cardElevation="0dp"
app:cardUseCompatPadding="true">
其他一些开源项目也看到了这个错误:- https://github.com/AnderWeb/discreteSeekBar/issues/73。
https://stackoverflow.com/questions/36115934
复制相似问题