Android翻转动画(卡片翻转效果)

需求

屏幕右滑事件触发卡片的翻转效果 ,为了方便,在例子中将右滑事件改成按钮点击事件

老规矩最后有源码

NO.1

先介绍三个插值器

  • LinearInterpolator() 其变化速率恒定
  • AccelerateInterpolator() 其变化开始速率较慢,后面加速
  • DecelerateInterpolator() 其变化开始速率较快,后面减速

NO.2

实现步骤

1.效果图

图片

2.布局

一个按钮,两个TextView(布局文件在源码部分)

3.逻辑判断(是否隐藏)

        final TextView visibletext;        final TextView invisibletext;        //逻辑判断        if (textview1.getVisibility() == View.GONE) {            visibletext = textview2;            invisibletext = textview1;        } else {            invisibletext = textview2;            visibletext = textview1;        }

4.翻转动画

 //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();

5.bug出现

发现实现后,第一次点击出现bug

图片

6.bug解决

在找到控件后,默认设置y方向角度

textview2.setRotationY(-90f);

NO.3

源码

MainActivity.java

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(); }

}

activity_main.xml

<?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>

声明:文中观点不代表本站立场。本文传送门:https://eyangzhen.com/221939.html

联系我们
联系我们
分享本页
返回顶部