利用ObjectAnimator編寫彈出式二級菜單

當二級菜單彈出時,界面的空間可能會不夠或變得擁擠。
解決:
這時可以將ToolBar和界面其它View上移,
這樣因爲二級菜單的彈出而讓界面擁擠的問題就得到了解決。

程序效果如圖所示:


MainActivity.java
public class MainActivity extends AppCompatActivity {

    private Toolbar mToolbar;
    private ImageView mImageView;
    private FrameLayout firstbar_layout,secondbar_layout;
    private int animationDuration = 300;
    private int animator_height,secondbar_height;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        //Toolbar
        mImageView = (ImageView)findViewById(R.id.image);
        //佔用屏幕空間的視圖,這裏用了圖片來代替
        firstbar_layout = (FrameLayout)findViewById(R.id.firstbar);
        //一級菜單
        secondbar_layout = (FrameLayout)findViewById(R.id.secondbar);
        //二級菜單

        animator_height = ScreenInfoUtil.dip2px(this, 50);
        //界面中Toolbar和圖片需要上移的高度
        secondbar_height = ScreenInfoUtil.dip2px(this, 100);
        //二級菜單的高度

        firstbar_layout.setOnClickListener(new View.OnClickListener() { //一級菜單點擊事件
            @Override
            public void onClick(View view) {
                    moveY(0, -animator_height);
                    moveBar(secondbar_height,0);
            }
        });
        secondbar_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) { //二級菜單點擊事件
                moveY(-animator_height, 0);
                moveBar(0,secondbar_height);
            }
        });
    }


    private void moveY(float startY, float endY) {
        //利用ObjectAnimator實現視圖的Y軸移動操作
        Object[] animComponent = {mToolbar,mImageView};
        //對Toolbar和ImageView進行移動
        for (final Object obj : animComponent) {
            ObjectAnimator animator = ObjectAnimator.ofFloat(obj,
                    "translationY", startY, endY);
            animator.setDuration(animationDuration).setInterpolator(
                    new AccelerateDecelerateInterpolator());
            animator.start();
        }
    }
    private void moveBar(float startY, float endY) {
        //利用ObjectAnimator實現二級菜單的彈出操作
        secondbar_layout.setVisibility(View.VISIBLE);
        Object[] animComponent = {secondbar_layout};
        for (final Object obj : animComponent) {
            ObjectAnimator animator = ObjectAnimator.ofFloat(obj,
                    "translationY", startY, endY);
            animator.setDuration(animationDuration).setInterpolator(
                    new AccelerateDecelerateInterpolator());
            animator.start();
        }
    }
}

activity_main.xml
<?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:id="@+id/root_layout"
    tools:context="com.lla.testviewup.MainActivity">
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="?attr/colorPrimary"/>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="280dp"
        android:id="@+id/image"
        android:layout_marginTop="120dp"
        android:scaleType="centerCrop"
        android:background="@drawable/godness"/>
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:id="@+id/firstbar"
        android:layout_alignParentBottom="true"
        android:background="@color/colorPrimary"
        >
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="我是一級菜單,點我彈出二級菜單"/>
    </FrameLayout>
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:id="@+id/secondbar"
        android:layout_alignParentBottom="true"
        android:background="@color/bottom_bar"
        android:visibility="invisible">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="我是二級菜單,點我返回一級菜單"/>
    </FrameLayout>
</RelativeLayout>



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章