Implement an ActionBar animation effect that is combined with the content and an actionbar.
Implement an ActionBar animation that is combined with the content to make your actionbar more vivid. Below is:
The advantage of this effect is that actionbar has become a part of the content, and the actual application effect is better than the image display effect, in addition to the effect of actionbar gradually appearing, the background image also has a dynamic effect called Ken Burns effect.
The following describes the implementation process.
Set the actionbar Style, We need the following two points:
1. The actionbar is transparent.
2. Enable overlay mode
<resources> <style name="TransparentTheme" parent="@android:style/Theme.Holo.Light"> <item name="android:windowBackground">@null</item> <item name="android:actionBarStyle">@style/ActionBarStyle.Transparent</item> <item name="android:windowActionBarOverlay">true</item> </style> <style name="ActionBarStyle.Transparent" parent="@android:Widget.ActionBar"> <item name="android:background">@null</item> <item name="android:displayOptions">homeAsUp|showHome|showTitle</item> <item name="android:titleTextStyle">@style/ActionBarStyle.Transparent.TitleTextStyle</item> </style> <style name="ActionBarStyle.Transparent.TitleTextStyle" parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title"> <item name="android:textColor">@android:color/white</item> </style></resources>
Layout
Layout is the key to implementation. FrameLayout contains a ListView and another FrameLayout (header, where the image is displayed, which is calledHeaderThe header contains two imageviews, one for displaying the background image and the other for displaying the logo. The logo will change with the scrolling of the listView and finally go to the actionbar.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/white" /> <FrameLayout android:id="@+id/header" android:layout_width="match_parent" android:layout_height="@dimen/header_height"> <ImageView android:id="@+id/header_picture" android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/picture0" /> <ImageView android:id="@+id/header_logo" android:layout_width="@dimen/header_logo_size" android:layout_height="@dimen/header_logo_size" android:layout_gravity="center" android:src="@drawable/ic_header_logo" /> </FrameLayout></FrameLayout>
The trick here is to add a forged header to the Listview and set its height to true.
HeaderHeight
.Xml code for forging Headers
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="@dimen/header_height" android:orientation="vertical"></LinearLayout>
Add his inflate TO ListView:
mFakeHeader = getLayoutInflater().inflate(R.layout.fake_header, mListView, false);mListView.addHeaderView(mFakeHeader);
Obtain the scroll positionThis code is excerpted from stackOverflow:
public int getScrollY() { View c = mListView.getChildAt(0); if (c == null) { return 0; } int firstVisiblePosition = mListView.getFirstVisiblePosition(); int top = c.getTop(); int headerHeight = 0; if (firstVisiblePosition >= 1) { headerHeight = mPlaceHolderView.getHeight(); } return -top + firstVisiblePosition * c.getHeight() + headerHeight;}
MobileHeaderLocation
When the ListView is rolled, you must moveHeaderAnd the forged ListView header. Note that it is moved to the boundary of the actionbar.
mListView.setOnScrollListener(new AbsListView.OnScrollListener() { @Override public void onScrollStateChanged(AbsListView view, int scrollState) { } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { int scrollY = getScrollY(); //sticky actionbar mHeader.setTranslationY(Math.max(-scrollY, mMinHeaderTranslation)); } });
Title gradient
The title text of the actionbar is gradient, And the TextView control of the title text can be obtained through the getIdentifier method of Resource.
private TextView getActionBarTitleView() { int id = Resources.getSystem().getIdentifier("action_bar_title", "id", "android"); return (TextView) findViewById(id);}
Set its transparency to 0 during initialization:
getActionBarTitleView().setAlpha(0f);
When the ListView is scrolling, the transparency is changed based on the ratio of the header's moving distance:
mListView.setOnScrollListener(new AbsListView.OnScrollListener() { @Override public void onScrollStateChanged(AbsListView view, int scrollState) { } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { float ratio = clamp(mHeader.getTranslationY() / mMinHeaderTranslation, 0.0f, 1.0f); //actionbar title alpha getActionBarTitleView().setAlpha(clamp(5.0F * ratio - 4.0F, 0.0F, 1.0F)); } });
Alpha values: f (x) = 5x-4
Clamp is a basic mathematical formula:
public static float clamp(float value, float max, float min) { return Math.max(Math.min(value, min), max);}
Movement and scaling of application icons and logos
First, you need to obtain the ImageView where the icon is located:
private ImageView getActionBarIconView() { return (ImageView) findViewById(android.R.id.home);}
Set a transparent icon
ActionBar actionBar = getActionBar();actionBar.setIcon(R.drawable.ic_transparent);
Then, when the ListView slides, the app icon and logo are moved and scaled according to the header's moving ratio. In this process, you can obtain the application icon and the logo in the header in the rectangular area of the screen to determine how to move and scale.
mListView.setOnScrollListener(new AbsListView.OnScrollListener() { @Override public void onScrollStateChanged(AbsListView view, int scrollState) { } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { float ratio = clamp(mHeader.getTranslationY() / mMinHeaderTranslation, 0.0f, 1.0f); //move & scale interpolation = mAccelerateDecelerateInterpolator.getInterpolation(ratio); View actionBarIconView = getActionBarIconView(); getOnScreenRect(mRect1, mHeaderLogo); getOnScreenRect(mRect2, actionBarIconView); float scaleX = 1.0F + interpolation (mRect2.width() / mRect1.width() – 1.0F); float scaleY = 1.0F + interpolation (mRect2.height() / mRect1.height() – 1.0F); float translationX = 0.5F (interpolation (mRect2.left + mRect2.right – mRect1.left – mRect1.right)); float translationY = 0.5F (interpolation (mRect2.top + mRect2.bottom – mRect1.top – mRect1.bottom)); mHeaderLogo.setTranslationX(translationX); mHeaderLogo.setTranslationY(translationY – mHeader.getTranslationY()); mHeaderLogo.setScaleX(scaleX); mHeaderLogo.setScaleY(scaleY); } });
Note: my own understanding of the above Code is that the application icon is not displayed until the beginning and end, but its position is occupied by the logo in the header.
Code download: https://github.com/flavienlaurent/NotBoringActionBar
Http://flavienlaurent.com/blog/2013/11/20/making-your-action-bar-not-boring/.
There is already an Actionbar at the top. How can I dynamically add an actionbar above?
------ Best solution -------------------------------------------------------- yes, but you must manually call invalidateOptionsMenu () after defining the partition operation attribute in the configuration file to add the menu () to refresh the menu display method, make sure always ------ other solutions ---------------------------------------------------- the column sharding operation attribute refers to manifest. android: uiOptions = "splitActionBarWhenNarrow" in xml is already set, but it still cannot be implemented .. Can you give me a link to the tutorial... ------ Other solutions else // flag = false; // invalidateOptionsMenu (); @ OverridepublicbooleanonPrepareOptionsMenu (Menumenu) {// TODOAuto-generatedmethodstubSystem.out.println ("invalidateOptionsMenu ()"); menu. findItem (R. id. menu_test ). setVisible (flag); returnsuper. onPrepareOptionsMenu (menu);} ------ other solutions ---------------------------------------------------------- No .... This cannot be set ....
How can I add an Actionbar on the top dynamically?
The Actionbar can be split. If you have set an actionbar on the top, you want to add some menus or
The button is displayed at the bottom, but only when needed. It is hidden at other times, while the top part is always required.
------ Best solution ---------------------------------------------------------- Yes
However, you must define the column operation attribute in the configuration file.
After adding the menu, you must manually call invalidateOptionsMenu () to refresh
You must use always to display menus.
------ Other solutions --------------------------------------------------------
Does the column sharding operation attribute mean android: uiOptions = splitActionBarWhenNarrow in manifest. xml?
This is already set, but it still cannot be implemented ..
Can you give me a link to the tutorial...
------ Other solutions --------------------------------------------------------
// Flag = false;
// InvalidateOptionsMenu (); @ Override
Public boolean onPrepareOptionsMenu (Menu menu ){
// TODO Auto-generated method stub
System. out. println (invalidateOptionsMenu ());
Menu. findItem (R. id. menu_test). setVisible (flag );
Return super. onPrepareOptionsMenu (menu );}
------ Other solutions --------------------------------------------------------
No .... This cannot be set ....