android - 淡入淡出动画到底部导航视图
问题描述:
我是初学者到android。在这个项目中,我有BottomNavigationView
。从一个视图切换到另一个视图时,我想将cross-fade
动画添加到此BottomNavigationView
。以下是我的代码。android - 淡入淡出动画到底部导航视图
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
动画?
答
从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);
}
});
}
什么是淡入淡出你想要的看法?你需要更具体然后,如果你需要帮助 –
@MosheEdri:对所有的意见。它的材料设计标准 –
我只看到你的代码中的一个视图我问我的方式 –