Android 如何制作一个像 Google Play 这样滚动时淡入的 ActionBar
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25424818/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make a ActionBar like Google Play that fades in when scrolling
提问by Cristiana Chavez
回答by CommandSpace
The following is the code I used in the app I am working
以下是我在我正在工作的应用程序中使用的代码
You will have to use the OnScrollChanged
function in your ScrollView
. ActionBar
doesn't let you set the opacity , so set a background drawable on the actionbar and you can change its opacity based on the amount of scroll in the scrollview. I have given an example workflow
您将不得不OnScrollChanged
在您的ScrollView
. ActionBar
不允许您设置 opacity ,因此在操作栏上设置可绘制的背景,您可以根据滚动视图中的滚动量更改其不透明度。我给出了一个示例工作流程
The function sets gives the appropriate alpha for the view locationImage based on its position WRT window .
函数集根据位置 WRT 窗口为视图 locationImage 提供适当的 alpha。
this.getScrollY()
gives you how much the scrollView
has scrolled
this.getScrollY()
给你scrollView
滚动了多少
public void OnScrollChanged(int l, int t, int oldl, int oldt) {
// Code ...
locationImage.setAlpha(getAlphaForView(locationImageInitialLocation- this.getScrollY()));
}
private float getAlphaForView(int position) {
int diff = 0;
float minAlpha = 0.4f, maxAlpha = 1.f;
float alpha = minAlpha; // min alpha
if (position > screenHeight)
alpha = minAlpha;
else if (position + locationImageHeight < screenHeight)
alpha = maxAlpha;
else {
diff = screenHeight - position;
alpha += ((diff * 1f) / locationImageHeight)* (maxAlpha - minAlpha); // 1f and 0.4f are maximum and min
// alpha
// this will return a number betn 0f and 0.6f
}
// System.out.println(alpha+" "+screenHeight +" "+locationImageInitialLocation+" "+position+" "+diff);
return alpha;
}
EDIT : I have added an example working sample at https://github.com/ramanadv/fadingActionBar, you can have a look at it.
编辑:我在https://github.com/ramanadv/fadingActionBar添加了一个示例工作示例,您可以查看它。
回答by shaobin0604
Please check this library https://github.com/ManuelPeinado/FadingActionBarwhich implements the cool fading action bar effect that you want
请检查这个库https://github.com/ManuelPeinado/FadingActionBar,它实现了你想要的很酷的淡入淡出动作条效果
回答by android_developer
This library helps for animation https://github.com/ksoichiro/Android-ObservableScrollView
这个库有助于动画https://github.com/ksoichiro/Android-ObservableScrollView
回答by Cristiana Chavez
Official Google DeveloperDocumentation
action bar in overlay mode.
叠加模式下的操作栏。
Enable Overlay Mode
启用叠加模式
To enable overlay mode for the action bar, you need to create a custom theme that extends an existing action bar theme and set the android:windowActionBarOverlay property to true.
要为操作栏启用覆盖模式,您需要创建一个扩展现有操作栏主题的自定义主题,并将 android:windowActionBarOverlay 属性设置为 true。
For Android 3.0 and higher only
仅适用于 Android 3.0 及更高版本
If your minSdkVersion is set to 11 or higher, your custom theme should use Theme.Holo theme (or one of its descendants) as your parent theme. For example:
如果您的 minSdkVersion 设置为 11 或更高,您的自定义主题应使用 Theme.Holo 主题(或其后代之一)作为您的父主题。例如:
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
parent="@android:style/Theme.Holo">
<item name="android:windowActionBarOverlay">true</item>
</style>
</resources>
For Android 2.1 and higher
对于 Android 2.1 及更高版本
If your app is using the Support Library for compatibility on devices running versions lower than Android 3.0, your custom theme should use Theme.AppCompat theme (or one of its descendants) as your parent theme. For example:
如果您的应用使用支持库在运行版本低于 Android 3.0 的设备上兼容,您的自定义主题应使用 Theme.AppCompat 主题(或其后代之一)作为您的父主题。例如:
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
parent="@android:style/Theme.AppCompat">
<item name="android:windowActionBarOverlay">true</item>
<!-- Support library compatibility -->
<item name="windowActionBarOverlay">true</item>
</style>
</resources>
Specify Layout Top-margin
指定布局上边距
When the action bar is in overlay mode, it might obscure some of your layout that should remain visible. To ensure that such items remain below the action bar at all times, add either margin or padding to the top of the view(s) using the height specified by actionBarSize. For example:
当操作栏处于叠加模式时,它可能会遮住一些应该保持可见的布局。为确保此类项目始终位于操作栏下方,请使用 actionBarSize 指定的高度在视图顶部添加边距或填充。例如:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="?android:attr/actionBarSize">
...
</RelativeLayout>
回答by Fabian Frank
There is official documentation on how to achieve this effect: https://developer.android.com/training/basics/actionbar/overlaying.html
有关于如何实现这种效果的官方文档:https: //developer.android.com/training/basics/actionbar/overlaying.html
Edit: There is an open source library ObservableScrollView with a lot of open source demos with different ActionBar effects, including the ones you mentioned. It's a great resource: https://github.com/ksoichiro/Android-ObservableScrollView.
编辑:有一个开源库 ObservableScrollView,其中包含许多具有不同 ActionBar 效果的开源演示,包括您提到的那些。这是一个很好的资源:https: //github.com/ksoichiro/Android-ObservableScrollView。
回答by MiichaelD
If you are using a CoordinatorLayout in your layout xml you should checkout this articledescribing how to handle scrolls and make other views react to them.
如果您在布局 xml 中使用 CoordinatorLayout,您应该查看这篇文章,描述如何处理滚动并让其他视图对其做出反应。
There is an example accomplishing what you asked for, if you add your imageview as a child of the CollapsingToolbarLayout all the magic is handled automatically and you can even some parameters like the scrim color, minimum height to start collapsing, etc:
有一个示例可以完成您的要求,如果您将 imageview 添加为 CollapsingToolbarLayout 的子项,则所有魔术都会自动处理,您甚至可以使用一些参数,例如稀松布颜色、开始折叠的最小高度等:
回答by sujith s
Using AbsListView scroll listener we can achieve for listview simply without using other complicated library or ScrollView
使用 AbsListView 滚动监听器,我们可以简单地实现列表视图,而无需使用其他复杂的库或 ScrollView
set scroll listener to list view
将滚动侦听器设置为列表视图
public class PagedScrollListener implements AbsListView.OnScrollListener {
private ActionBar mActionBar;
private View mTopHideView; // represent header view
@Override
public void onScrollStateChanged(final AbsListView view, final int scrollState) {
mScrollState = scrollState;
}
@Override
public void onScroll(final AbsListView view, final int firstVisibleItem, final int visibleItemCount, final int totalItemCount) {
if (mActionBar != null && mTopHideView != null && mListView != null) {
Log.i(TAG, getScrollY() + "");
int currentY = getScrollY();
final int headerHeight = mTopHideView.getHeight() - mActionBar.getHeight();
final float ratio = (float) Math.min(Math.max(currentY, 0), headerHeight) / headerHeight;
final int newAlpha = (int) (ratio * 255);
Log.i(TAG, newAlpha + " alpha");
mActionBarDrawaqble.setAlpha(newAlpha);
}}
public void setActionBarRefernce(ActionBar actionBar, View topHideView, float actionBarHeight, ListView listView) {
mActionBar = actionBar;
mActionBarHeight = actionBarHeight;
mTopHideView = topHideView;
mListView = listView;
mActionBarDrawaqble = new ColorDrawable(ContextCompat.getColor(mContext, R.color.google_plus_red));
mActionBar.setBackgroundDrawable(mActionBarDrawaqble);
mActionBarDrawaqble.setAlpha(0);
}
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 = mTopHideView.getHeight();
}
return -top + firstVisiblePosition * c.getHeight() + headerHeight;
}
}
// Note :Don't forget to call **setActionBarRefernce method ** of custom listener
//注意:不要忘记调用自定义监听器的**setActionBarRefernce 方法**
回答by SmallSani
On webView:
在网络视图上:
@JavascriptInterface
public void showToolbar(String scrollY, String imgWidth) {
int int_scrollY = Integer.valueOf(scrollY);
int int_imgWidth = Integer.valueOf(imgWidth);
String clr;
if (int_scrollY<=int_imgWidth-actionBarHeight) {
clr = Integer.toHexString(int_scrollY * 255 / (int_imgWidth-actionBarHeight));
}else{
clr = Integer.toHexString(255);
}
toolbar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#"+clr+"9CCC65")));
}