Android从底部向上弹出的动画效果

时间:2024-05-20 16:04:47

 

下面介绍两种弹出方式,一种是利用TranslateAnimation动画,另一种是PopupWindow

Android从底部向上弹出的动画效果Android从底部向上弹出的动画效果

左边是动画,右边是popupwindow。

第一种方式:

public class MainActivity extends AppCompatActivity {

    private LinearLayout mPopupLayout;
    private Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mPopupLayout = (LinearLayout) findViewById(R.id.start_ctrl);
        btn = findViewById(R.id.start_btn);

        startAnim();
    }

    private void startAnim() {
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //设置动画,从自身位置的最下端向上滑动了自身的高度,持续时间为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(400l);     //设置动画的过渡时间
                mPopupLayout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mPopupLayout.setVisibility(View.VISIBLE);
                        mPopupLayout.startAnimation(ctrlAnimation);
                    }
                }, 500);
            }
        });
    }
}
<?xml version="1.0" encoding="utf-8"?>
<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">

    <Button
        android:id="@+id/start_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="从底部弹出"/>

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

 

第二种方式:

先创建一个popupwindow:

private PopupWindow mPopupWindow;
mPopupWindow = createPopupWindow(contentView);

 将需要的布局文件传入即可:

private PopupWindow createPopupWindow(View contentView) {
        contentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        PopupWindow popupWindow = new PopupWindow(contentView, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT, true);
        popupWindow.setTouchable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setFocusable(true);
        popupWindow.setAnimationStyle(R.style.PopupAnimation);
        popupWindow.setClippingEnabled(true);
        popupWindow.setSoftInputMode(PopupWindow.INPUT_METHOD_NEEDED);
        popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        popupWindow.showAtLocation(mRootLayout, Gravity.NO_GRAVITY, 0, DimenUtils.getWindowHeight() - contentView.getMeasuredHeight());

        popupWindow.update();
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                setWindowBackgroundAlpha(1.0f);//当PopupWindow消失的时候Window完全透明
            }
        });
        return popupWindow;
    }