0

我是安卓的初学者。在这个项目中,我有BottomNavigationView. 从一个视图切换到另一个视图时,我想为此添加cross-fade动画。BottomNavigationView下面是我的代码。

bottom_nav.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- Content Container -->

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        app:itemBackground="@color/colorWhite"
        app:itemIconTint="@drawable/item_checked_color"
        app:itemTextColor="@drawable/item_checked_color"
        app:menu="@menu/bottom_navigation_main" />

</RelativeLayout>

我将上面的 xml 文件包含到 activity_main.xml

activity_main.xml

<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:id="@+id/content_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="10dp"/>

// other content

<include
            layout="@layout/bottom_nav"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

</RelativeLayout>

MainActivity.java

public class MyDashboardActivity extends BaseActivity implements NavigationView.OnNavigationItemSelectedListener {

private BottomNavigationView bottomNavigationView;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

bottomNavigationView = (BottomNavigationView)findViewById(R.id.bottom_navigation);
        BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);


 loadBottomNavigation();
}

public void loadBottomNavigation(){


        Menu bottomMenu = bottomNavigationView.getMenu();
        if (bottomMenu != null){
            for (int i=0; i< bottomMenu.size(); i++){
                MenuItem bottomMenuItem = bottomMenu.getItem(i);
                applyFontToMenuItem(bottomMenuItem);

                MenuItem dashboard = bottomMenu.getItem(0);
                dashboard.setChecked(true);
                MenuItem expert = bottomMenu.getItem(2);
                expert.setChecked(false);
            }
        }

        bottomNavigationView.setOnNavigationItemSelectedListener(
                new BottomNavigationView.OnNavigationItemSelectedListener() {
                    @Override
                    public boolean onNavigationItemSelected(MenuItem item) {
                        switch (item.getItemId()){
                            case R.id.action_1:
                                Intent intent = new Intent(getApplicationContext(), NewActivity.class);
                                intent.putExtra("idPatient", idPatient);
                                startActivity(intent);

                                break;

                            case R.id.action_2:
                                Intent intent1 = new Intent(MainActivity.this, FirstActivity.class);
                                intent1.putExtra("idPatient", idPatient);
                                startActivity(intent1);

                                break;

                            case R.id.action_3:
                                Intent intent2 = new Intent(MainActivity.this, SecondActivity.class);
                                String url = "https:rrrrrrr";
                                String activityName = "MainActivity";
                                intent2.putExtra("activity", activityName);
                                intent2.putExtra("linkUrl", url);
                                startActivity(intent2);

                                break;


                        }
                        return true;
                    }
                });
    }
}

cross-fade在视图之间切换时如何添加动画?

4

1 回答 1

-2

从android文档...

https://developer.android.com/training/animation/crossfade.html

   private View mContentView;
    private View mLoadingView;
    private int mShortAnimationDuration;

    ...

    private void crossfade() {

        // Set the content view to 0% opacity but visible, so that it is visible
        // (but fully transparent) during the animation.
        mContentView.setAlpha(0f);
        mContentView.setVisibility(View.VISIBLE);

        // Animate the content view to 100% opacity, and clear any animation
        // listener set on the view.
        mContentView.animate()
                .alpha(1f)
                .setDuration(mShortAnimationDuration)
                .setListener(null);

        // Animate the loading view to 0% opacity. After the animation ends,
        // set its visibility to GONE as an optimization step (it won't
        // participate in layout passes, etc.)
        mLoadingView.animate()
                .alpha(0f)
                .setDuration(mShortAnimationDuration)
                .setListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        mLoadingView.setVisibility(View.GONE);
                    }
                });
    }
于 2017-10-09T08:23:08.593 回答