最近遇到這樣的需求蔚晨,一開始自己寫出來乍钻,總是會有跳頓感肛循,不是那么流暢,去網(wǎng)上查了下银择,在加上自己修改了下多糠,便成了現(xiàn)在的樣子。(滑動過程中上面的圖片是縮放的浩考,里面的一個小icon是縮小放大透明度漸變的夹孔,真機加Vysor,看的效果特別模糊的析孽,沒入Vysor pro搭伤,這畫質(zhì)將就看吧)
當然應該還可以繼續(xù)優(yōu)化吧,不過最近比較忙绿淋,暫時先這樣吧闷畸。
為了以后方便自己整理學習,先寫到簡書吧吞滞。
首先初始化一些基本的參數(shù)
private void initDistance() {
//這是頂部標題欄的初始高度佑菩;
OrignHight = layout.getLayoutParams().height;
//給它設定個上拉的最小高度;
minHight = px2dp(100);
//需要滑動的距離裁赠;
Distance = OrignHight - minHight;
}
判斷ListView是否滑動到頂部殿漠;
public boolean isListViewReachTopEdge(ListView listView){
boolean result = false;
if(listView.getFirstVisiblePosition() == 0){
View childAt = listView.getChildAt(0);
result = childAt.getTop()==0;
}
return result;
}
下面的是對touchevent的處理
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
//如果ListView是在頂部,進行處理佩捞,不在頂部绞幌,不執(zhí)行
if(isListViewReachTopEdge(listView)){
int y = (int) ev.getY();
int distanceY = 0;
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
pointY = y;
afterup = false;
return super.dispatchTouchEvent(ev);
case MotionEvent.ACTION_MOVE:
y = (int) ev.getY();
if (currentDistance >= Distance && distanceY > 0) {
return super.dispatchTouchEvent(ev);
}
if (currentDistance <= 0 && distanceY < 0) {
return super.dispatchTouchEvent(ev);
}
distanceY = pointY - y;
if (rate == 1) {
if (distanceY > 0) {
return super.dispatchTouchEvent(ev);
}
}
moveLayout(distanceY);
pointY = y;
break;
case MotionEvent.ACTION_UP:
afterup = true;
elasticityHeight();
return super.dispatchTouchEvent(ev);
}
}else {
return super.dispatchTouchEvent(ev);
}
return false;
}```
接下來就是里面用的各種方法
```java
//滑動過程中的處理
private void moveLayout(int distanceY) {
ViewGroup.LayoutParams layoutParams = layout.getLayoutParams();
layoutParams.height = layoutParams.height - distanceY;
layout.setLayoutParams(layoutParams);
elasticityHeight();
layout.requestLayout();
currentDistance = OrignHight - layout.getLayoutParams().height;
rate = (float) (currentDistance * 1.0 / Distance);
moveViewByrate(rate);}
//抬手后進行的處理
private void elasticityHeight() {
ViewGroup.LayoutParams layoutParams = layout.getLayoutParams();
if (layoutParams.height < minHight) {
layoutParams.height = minHight;
layout.setLayoutParams(layoutParams);
layout.requestLayout();
}
if (layoutParams.height > OrignHight) {
layoutParams.height = OrignHight;
layout.setLayoutParams(layoutParams);
layout.requestLayout();
}
if (afterup) {
if (layoutParams.height < OrignHight / 1.8) {
layoutParams.height = minHight;
layout.setLayoutParams(layoutParams);
imageView.setVisibility(View.GONE);
layout.requestLayout();
}
if (layoutParams.height >= OrignHight / 1.8) {
layoutParams.height = OrignHight;
layout.setLayoutParams(layoutParams);
imageView.setVisibility(View.VISIBLE);
imageView.setAlpha((float) 1.0 - 0);
imageView.setScaleX(1);
imageView.setScaleY(1);
layout.requestLayout();
}
}
}
//滑動過程中,控件的大小一忱,透明度的變化
private void moveViewByrate(float rate) {
if (rate >= 1) {
imageView.setVisibility(View.GONE);
} else {
imageView.setVisibility(View.VISIBLE);
imageView.setAlpha(1 - rate);
imageView.setScaleX(1 - rate);
imageView.setScaleY(1 - rate);
}
}
文章中有部分是轉(zhuǎn)載自網(wǎng)絡莲蜘,在這里說聲抱歉。
因為時間倉促帘营,里面可能有些錯誤票渠,希望指正。