模仿天天果园登录界面的效果。
要点:将要实现动画的TextView被EditText覆盖,
做出出水面,进水面的效果,只要在水面之下看不见,出水面就看得见。
布局:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:text="Hello World!"
android:textColor="@color/colorAccent"
android:textSize="25sp" />
<EditText
android:id="@+id/et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:background="@color/colorPrimaryDark"
android:hint="请输入你想输入的"
android:padding="8dp" />
</FrameLayout>
android:layout_marginTop保证textview的移动空间,
动画:
anim.dismiss.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">
<translate
android:duration="100"
android:fromYDelta="0%"
android:toYDelta="-100%" />
</set>
anim.show.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">
<translate
android:duration="100"
android:fromYDelta="0%"
android:toYDelta="-100%" />
</set>
MainActivity:
public class MainActivity extends AppCompatActivity {
private TextView mTv;
private boolean isShow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTv = ((TextView) this.findViewById(R.id.tv));
EditText mEt = ((EditText) this.findViewById(R.id.et));
mEt.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
String string = s.toString();
if (string.length() > 0 && !isShow) {
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.show);
mTv.startAnimation(animation); //启动动画
animation.setInterpolator(new AccelerateDecelerateInterpolator());
isShow = true;
} else if (string.length() == 0) {
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.dismiss);
animation.setInterpolator(new AccelerateDecelerateInterpolator());
mTv.startAnimation(animation); //启动动画
isShow = false;
}
}
});
}
}