昨晚失眠了~~~~~哎。早上起来,有是雾霾天气。你敢信。。。让我们继续吧!
那就加油吧~今天给大家讲解一篇仿qq空间个人中心背景可以下拉放大的列子~~
话不多说先上图(哈哈)
欧巴~~~就是这个效果了,还是相当的清晰地~
我在这是用的ListView。上代码把
**
*
* @author dell
* @date 2018/3/14
*/
public class MyListViewextends ListView{
private ImageViewheaderView ;
private int originHeight;
private int maxHeight;
public MyListView(Context context) {
super(context);
}
public MyListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyListView(Context context, AttributeSet attrs,int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public void setParallaxImageView(ImageView headerView) {
this.headerView = headerView ;
originHeight = headerView.getHeight();
maxHeight = headerView.getDrawable().getIntrinsicHeight();
}
/**
* 重写overScrollBy,能获取ListView下拉的距离
*
* @param deltaX:横向的变化量
* @param deltaY:纵向的变化量
* @param scrollX:横向X的偏移量
* @param scrollY:纵向Y的偏移量
* @param scrollRangeX:横向X偏移范围
* @param scrollRangeY:纵向Y的偏移范围
* @param maxOverScrollX:横向X最大的偏移量
* @param maxOverScrollY:纵向Y最大的偏移量
* @param isTouchEvent:是否是触摸产生的滑动超出
* @return
*/
@Override
protected boolean overScrollBy(int deltaX,int deltaY,int scrollX,int scrollY,int scrollRangeX,int scrollRangeY,int maxOverScrollX,int maxOverScrollY,boolean isTouchEvent) {
if (isTouchEvent && deltaY <0){
int newHeight = (int) (headerView.getHeight() + Math.abs(deltaY /3.0f));
newHeight = Math.min(newHeight,maxHeight);
headerView.getLayoutParams().height = newHeight ;
headerView.requestLayout();
}
return super.overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX, scrollRangeY, maxOverScrollX, maxOverScrollY, isTouchEvent);
}
/**
* ResetAnimation 为封装的动画类
* @param ev
* @return
*/
@Override
public boolean onTouchEvent(MotionEvent ev) {
if (MotionEventCompat.getActionMasked(ev) == MotionEvent.ACTION_UP){
ResetAnimation resetAnimation =new ResetAnimation(headerView,originHeight);
headerView.startAnimation(resetAnimation);
}
return super.onTouchEvent(ev);
}
}
个人觉得还是很简单的。哈哈,接下来看动画类(ResetAnimation )
public class ResetAnimationextends Animation {
private int startHeight;
private int tragetHeight;
private ImageViewimageView;
public ResetAnimation(ImageView imageView,int tragetHeight) {
this.imageView = imageView ;
this.startHeight = imageView.getHeight();
this.tragetHeight = tragetHeight;
this.setDuration(500);
setInterpolator(new OvershootInterpolator());
System.out.println("构造函数被调用了");
}
/**
*
* @param interpolatedTime:从0.0 ————> 到1.0的百分比
* @param t
*/
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
IntEvaluator intEvaluator =new IntEvaluator();
Integer newHeight = intEvaluator.evaluate(interpolatedTime,startHeight,tragetHeight);
System.out.println(startHeight);
imageView.getLayoutParams().height = newHeight ;
imageView.requestLayout();
}
至于mainactivity就跟简单了~~
private static final String[]names =new String[]{
"我爱你❤",
"我爱你❤",
"我爱你❤",
"我爱你❤",
"我爱你❤",
"我爱你❤",
"我爱你❤",
"我爱你❤",
"我爱你❤",
"我爱你❤",
"我爱你❤",
"我爱你❤",
};
private MyListViewlv;
private ImageViewheaderView;
private Viewview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
initData();
}
private void initData() {
lv.addHeaderView(view);
lv.setAdapter(new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1,names) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView tv = (TextView)super.getView(position, convertView, parent);
tv.setTextColor(Color.BLACK);
return tv;
}
});
//设置滚动模式
lv.setOverScrollMode(ListView.OVER_SCROLL_NEVER);
headerView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
lv.setParallaxImageView(headerView);
headerView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
});
}
private void initViews() {
view = View.inflate(MainActivity.this, R.layout.activity_heander,null);
headerView = (ImageView)view.findViewById(R.id.iv);
lv = (MyListView) findViewById(R.id.my_listview);
}
在你的main的xml里面布局引用你的自定义的myListview。
请忽略变量名。哈哈。到这就结束了。如果有好的方法希望指出,不对的地方各位大佬也要指出~~谢谢!
网友评论