最近好友问计蒙翻转动画,恰好在大二那年看Android Api Demo时记了笔记,由此写一篇文章。
屏幕右滑事件触发卡片的翻转效果 ,为了方便,在例子中将右滑事件改成按钮点击事件
老规矩,最后有源码
LinearInterpolator() 其变化速率恒定
AccelerateInterpolator() 其变化开始速率较慢,后面加速
DecelerateInterpolator() 其变化开始速率较快,后面减速
一个按钮,两个TextView(布局文件在源码部分)
final TextView visibletext;
final TextView invisibletext;
//逻辑判断
if (textview1.getVisibility() == View.GONE) {
visibletext = textview2;
invisibletext = textview1;
} else {
invisibletext = textview2;
visibletext = textview1;
}
//LinearInterpolator() 其变化速率恒定
ObjectAnimator visToInvis = ObjectAnimator.ofFloat(visibletext, "rotationY", 0f, 90f);
visToInvis.setDuration(500);
//AccelerateInterpolator() 其变化开始速率较慢,后面加速
visToInvis.setInterpolator(new AccelerateInterpolator());
final ObjectAnimator invisToVis = ObjectAnimator.ofFloat(invisibletext, "rotationY",
-90f, 0f);
invisToVis.setDuration(500);
//DecelerateInterpolator() 其变化开始速率较快,后面减速
invisToVis.setInterpolator(new DecelerateInterpolator());
visToInvis.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator anim) {
visibletext.setVisibility(View.GONE);
invisToVis.start();
invisibletext.setVisibility(View.VISIBLE);
}
});
visToInvis.start();
发现实现后,第一次点击出现bug
在找到控件后,默认设置y方向角度
textview2.setRotationY(-90f);
public class MainActivity extends Activity {
private TextView textview1;
private TextView textview2;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
//bug解决
textview2.setRotationY(-90f);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
flipAnimation();
}
});
}
private void initView() {
textview1 = (TextView) findViewById(R.id.textview1);
textview2 = (TextView) findViewById(R.id.textview2);
button = (Button) findViewById(R.id.button);
}
private void flipAnimation() {
final TextView visibletext;
final TextView invisibletext;
//逻辑判断
if (textview1.getVisibility() == View.GONE) {
visibletext = textview2;
invisibletext = textview1;
} else {
invisibletext = textview2;
visibletext = textview1;
}
//LinearInterpolator() 其变化速率恒定
ObjectAnimator visToInvis = ObjectAnimator.ofFloat(visibletext, "rotationY", 0f, 90f);
visToInvis.setDuration(500);
//AccelerateInterpolator() 其变化开始速率较慢,后面加速
visToInvis.setInterpolator(new AccelerateInterpolator());
final ObjectAnimator invisToVis = ObjectAnimator.ofFloat(invisibletext, "rotationY",
-90f, 0f);
invisToVis.setDuration(500);
//DecelerateInterpolator() 其变化开始速率较快,后面减速
invisToVis.setInterpolator(new DecelerateInterpolator());
visToInvis.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator anim) {
visibletext.setVisibility(View.GONE);
invisToVis.start();
invisibletext.setVisibility(View.VISIBLE);
}
});
visToInvis.start();
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="200dp"
android:layout_height="200dp"
android:background="#35B9F4"
android:id="@+id/textview1"
android:layout_centerInParent="true"
/>
<TextView
android:layout_width="200dp"
android:layout_height="200dp"
android:background="#03DAC5"
android:layout_centerInParent="true"
android:id="@+id/textview2"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:id="@+id/button"
android:text="点击翻转"/>
</RelativeLayout>
最后将button的点击事件改成屏幕监听事件即可。
希望对您有所帮助,欢迎留言。如有问题可联系计蒙。