最佳答案
以下是我在我正在工作的应用程序中使用的代码
您必须在 ScrollView
中使用 OnScrollChanged
函数。 ActionBar
不允许您设置 opacity ,因此在操作栏上设置一个可绘制的背景,您可以根据 ScrollView 中的滚动量来更改其不透明度。我已经给出了一个示例工作流
函数sets根据其位置WRT窗口为 View locationImage提供适当的alpha。
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;
}
编辑:我在 https://github.com/ramanadv/fadingActionBar 添加了一个示例工作示例,你可以看看。
关于android - 如何制作像 Google Play 这样在滚动时淡入的 ActionBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25424818/