首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >android -交叉淡出动画到底部导航视图

android -交叉淡出动画到底部导航视图
EN

Stack Overflow用户
提问于 2017-10-09 07:23:59
回答 1查看 3.3K关注 0票数 0

我是android的初学者。在这个项目中,我有BottomNavigationView。当从一个视图切换到另一个视图时,我想将cross-fade动画添加到这个BottomNavigationView中。下面是我的密码。

bottom_nav.xml

代码语言:javascript
复制
<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

代码语言:javascript
复制
<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

代码语言:javascript
复制
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动画?

EN

回答 1

Stack Overflow用户

发布于 2017-10-09 08:23:08

从机器人的文档..。

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

代码语言:javascript
复制
   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);
                    }
                });
    }
票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46640803

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档