<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!--这是topView-->
<RelativeLayout
android:id="@+id/rl_top"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<!--这是imageView,一定要设置scaleType为centerCrop-->
<ImageView
android:id="@+id/iv_stretch_pic"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:src="@drawable/stretch_s"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/iv_stretch_pic"
android:text="你最美,你最酷…………^^"
android:textSize="16sp"/>
</RelativeLayout>
<!--这是自定义的listview-->
<com.example.zwr.myapplication.widget.StretchListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cacheColorHint="@null"
android:divider="@null"
android:listSelector="#00000000"/>
</LinearLayout>
/***
* @param topView
* @param imgResId 图片id
*/
public void setTopView(View topView, int imgResId) {
if (null != topView) {
this.topView = topView;
imageView = (ImageView) topView.findViewById(imgResId);
}
}
ACTION_DOWN:
case MotionEvent.ACTION_DOWN:
startY = ev.getRawY();
if (!hadInit) {//初始化,只要初始化一次就够了
childAt0Top = getChildAt(0).getTop();
ivInitHeight = imageView.getHeight();
hadInit = true;
}
break;
case MotionEvent.ACTION_MOVE:
Log.d(TAG, "dy = " + dy);
dy = ev.getRawY() - startY;
if (dy > 0 && 0 == getFirstVisiblePosition() &&
childAt0Top == getChildAt(0).getTop()) {//(1)手指从上往下拉:下拉
int tempDy = (int) (dy + 0.5);
//一定也要给topView增加一定的高度,否则从上啦到下拉就不会显示
imageView.getLayoutParams().height = imageView.getHeight() + tempDy;
topView.getLayoutParams().height = topView.getHeight() + tempDy;
topView.requestLayout();
isChangedHeight = true;
} else {//(2)手指从下往上拉:上拉
int tempDy = (int) (dy - 0.5);
int currHeight = imageView.getHeight();
float translationY = getNegativeMaxValue(tempDy, -currHeight, 0);
if (translationY <= 0 && currHeight > 0) {
LinearLayout.LayoutParams lp =
(LinearLayout.LayoutParams) topView.getLayoutParams();
//一定要减去titleBar,如果没有去掉Winow.xxx.Title,还要减去这个高度,否则会显示不全
lp.height = topView.getHeight() + (int) translationY;
topView.requestLayout();//
isChangedHeight = true;
}
}
//用这个getRawY而不是用getY,是因为listview也会随着改变,
//而getY获取的就是listview本身的Y,所以基本是变化不大的,
// 而使用getRawY相对于屏幕的距离,保证滑动了多大的距离就改变多大的距离
startY = ev.getRawY();
break;
float translationY = getNegativeMaxValue(tempDy, -currHeight, 0);
/***
* 手指上移过程dy是负数
* 返回负数最大值:0是最大值,不可以超过
*
* @param value 移动的最终距离:上次的位置+当次移动的偏移量之和,就是本次要移动的最终的偏移量
* @param canMoveMaxValue 可移动的最大值
* @param maxValue
* @return
*/
public static float getNegativeMaxValue(float value,float canMoveMaxValue, float maxValue) {
return Math.min(maxValue, Math.max(canMoveMaxValue, value));
}
case MotionEvent.ACTION_OUTSIDE:
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
if (isChangedHeight) {
if (imageView.getHeight() > ivInitHeight) {// (1)手指从上往下拉:下拉
ResetAnimation resetAnimation =
new ResetAnimation(ivInitHeight, imageView, topView);
resetAnimation.setDuration(200);
imageView.startAnimation(resetAnimation);
} else {//(2)手指从下往上拉:上拉。。。这个不用处理。。。因为上拉后松开让其topview固定
}
isChangedHeight = false;
}
break;
/**
* 自定义回弹动画,使imageView和topView过渡回弹到初始位置
*/
static class ResetAnimation extends Animation {
private View topView;
private int topCurrHeight;
private ImageView ivStretch;
private int ivInitHeight;
private int ivCurrHeight;
public ResetAnimation(int ivInitHeiht, ImageView ivStretch, View topView) {
this.ivInitHeight = ivInitHeiht;
this.ivCurrHeight = ivStretch.getHeight();
this.topCurrHeight = topView.getHeight();
this.ivStretch = ivStretch;
this.topView = topView;
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
int dy = (int) ((ivCurrHeight - ivInitHeight) * interpolatedTime);
Log.d(TAG, "anim dy = " + dy);
ivStretch.getLayoutParams().height = ivCurrHeight - dy;
topView.getLayoutParams().height = topCurrHeight - dy;
topView.requestLayout();
}
}
/***
* 这个方法是在滑出屏幕时回调,但是由于android系统国内厂商修改的面目全非,有些机型是不会回调的,比如vivo
* 所以不要使用这个方法搞事情
*
* @param scrollX
* @param scrollY
*/
@Override
protected boolean overScrollBy(int deltaX, int deltaY, int scrollX, int scrollY,
int scrollRangeX, int scrollRangeY, int maxOverScrollX, int maxOverScrollY,
boolean isTouchEvent) {
Log.d(TAG, "deltax = " + deltaX + " deltaY = " + deltaY);
return super.overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX,
scrollRangeY, maxOverScrollX, maxOverScrollY, isTouchEvent);
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有