It's been a long time since I blogged. Go directly to the picture 3 to see the effect!!!! The mobile phone is oppo r7 android system 4.4
Not much to say, the simple code is as follows. . . .
Core api:!! Only systems above 4.4 have this feature, so you have to judge! !
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
// Now we start to process the logic. . In general. If we don't use actionBar, we will customize a height as the title bar. I do the same.
We made a judgment >=19 earlier. So you just write normally according to the situation. By dynamically changing the height of the control, the display of the content inside has been controlled. .
Get 60% done! ! ! ! ! Next, one needs to listen to the sliding event. . . I am custom ScrollView. This ScrollView is special. Can also do rebound effect. Will bounce back later. . Here is the custom ScrollView
public class MyScrollView extends ScrollView {
private OnScroll onScroll = null;
private ScrollBottomListener scrollBottomListener;
private ImageView mHeaderImg;
private int mHeaderImgMaxHeight;
private int mHeaderImgOriginalHeight;
public MyScrollView(Context context) {
super(context);
}
public MyScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
if (t + getHeight() >= computeVerticalScrollRange()) {
if (scrollBottomListener != null) {
scrollBottomListener.scrollBottom();
}
}
/**
* 滑动监听
*/
if (onScroll != null) {
onScroll.onScrollChanged(l, t, oldl, oldt);
}
}
public void setScrollBottomListener(ScrollBottomListener scrollBottomListener) {
this.scrollBottomListener = scrollBottomListener;
}
/**
* 是否滚动到底部
*/
public interface ScrollBottomListener {
public void scrollBottom();
}
public void setOnScrollStatus(OnScroll onScroll) {
this.onScroll = onScroll;
}
public interface OnScroll {
void onScrollChanged(int x, int y, int oldx, int oldy);
}
@Override
/**
* deltaY: 竖向滑动到头的值: -值就是拉到头了。+值就是拉到尾巴了
* maxOverScrollY:结束后最大超过的值
* isTouchEvent:是否按住拖动到头的值 true 一直为放开拖到到头,false 惯性滑动
*/
protected boolean overScrollBy(int deltaX, int deltaY, int scrollX, int scrollY, int scrollRangeX, int scrollRangeY, int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent) {
// Log.e("masai", "deltaY:" + deltaY + " maxOverScrollY:" + maxOverScrollY + " " + "isTouchEvent:" + isTouchEvent);
if (deltaY < 0 && isTouchEvent) {
//按住拖到到头的。。动态的改变高度!!
//--得正。。。当改变以后。必须得重新布局一下。重新测量一下
if (mHeaderImg != null) {
int tempHeader = mHeaderImg.getHeight() - deltaY / 3;
if (tempHeader < mHeaderImgMaxHeight) {
mHeaderImg.getLayoutParams().height = tempHeader;
mHeaderImg.requestLayout();
}
}
}
return super.overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX, scrollRangeY, maxOverScrollX, maxOverScrollY, isTouchEvent);
}
/**
* addHeadr
* @param i ImageView 的容器必须是 wrap_content
*/
public void addHeader(final ImageView i) {
this.mHeaderImg = i;
this.mHeaderImgMaxHeight = (int) (i.getDrawable().getIntrinsicHeight() * 1.4);
//测量出最初的高度。用于回弹
i.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
mHeaderImgOriginalHeight = mHeaderImg.getHeight();
i.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_UP:
if (mHeaderImg != null) {
ResetAnimation resetAnimation = new ResetAnimation(mHeaderImg, mHeaderImgOriginalHeight);
startAnimation(resetAnimation);
}
break;
}
return super.onTouchEvent(ev);
}
private class ResetAnimation extends Animation {
private View v;
private int startHeight;
private int endHeight;
public ResetAnimation(View v, int endHeight) {
super();
this.v = v;
this.startHeight = v.getHeight();
this.endHeight = endHeight;
//动画时间
setDuration(400);
setInterpolator(new OvershootInterpolator());
}
/**
* 会循环调用该方法,表示动画执行的进度
*
* @param interpolatedTime 0 - 1!!!动画执行的百分比,如果加了差时器的话可能到1.1,1.2
* @param t
*/
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
super.applyTransformation(interpolatedTime, t);
// 04-19 15:38:05.226 2151-2151/? E/TAG: 457新的高度 日志得知! 开始高度480
// 04-19 15:38:05.241 2151-2151/? E/TAG: 462新的高度
// 04-19 15:38:05.257 2151-2151/? E/TAG: 468新的高度
// 04-19 15:38:05.275 2151-2151/? E/TAG: 473新的高度
// 04-19 15:38:05.291 2151-2151/? E/TAG: 477新的高度
// 04-19 15:38:05.308 2151-2151/? E/TAG: 479新的高度
// 04-19 15:38:05.325 2151-2151/? E/TAG: 480新的高度
// 04-19 15:38:05.342 2151-2151/? E/TAG: 480新的高度
//运动的高度!!!结束的高度-开始运动的高度。是一个-值。然后+开始的高度。。。
int newHeader = (int) ((endHeight - startHeight) * interpolatedTime + startHeight);
v.getLayoutParams().height = newHeader;
v.requestLayout();
// Log.e("TAG", newHeader + "新的高度");
}
}
}
I don't post the source code directly either. . . . That's pretty much it. . . . . .