进入APP后登陆注册按钮从底部弹出动画效果的例子:
运行效果图:
代码实现:
首先是我们的布局文件:activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#DDE2E3" tools:context=".MainActivity"> <LinearLayout android:id="@+id/start_ctrl" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:orientation="vertical" android:visibility="gone"> <Button android:id="@+id/start_login" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#F26968" android:gravity="center" android:paddingBottom="15dp" android:paddingTop="15dp" android:text="登陆" android:textColor="#FFFFFF" android:textSize="18sp" /> <Button android:id="@+id/start_register" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#323339" android:gravity="center" android:paddingBottom="15dp" android:paddingTop="15dp" android:text="注册" android:textColor="#FFFFFF" android:textSize="18sp" /> </LinearLayout> </RelativeLayout>
接着是MainActivity.java:
public class MainActivity extends AppCompatActivity { private LinearLayout start_ctrl; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); start_ctrl = (LinearLayout) findViewById(R.id.start_ctrl); //设置动画,从自身位置的最下端向上滑动了自身的高度,持续时间为500ms final TranslateAnimation ctrlAnimation = new TranslateAnimation( TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, 1, TranslateAnimation.RELATIVE_TO_SELF, 0); ctrlAnimation.setDuration(500l); //设置动画的过渡时间 start_ctrl.postDelayed(new Runnable() { @Override public void run() { start_ctrl.setVisibility(View.VISIBLE); start_ctrl.startAnimation(ctrlAnimation); } }, 2000); } }
注释写得很清楚了,这里就不BB解释了,如果你对TranslateAnimation.RELATIVE_TO_SELF这个有疑惑, 请自己谷歌或者百度,限于篇幅(我懒),这里就不写了,蛮简单的~