Android滚动原理及实现

来源:互联网 发布:c语言编译器安卓版 编辑:程序博客网 时间:2024/06/01 19:15

1、layout

view.layout(left,top,right,bottom);

//注:这里的left,top,right,bottom全部是相对于parent的坐标,活动结果也是在整个parent中的变化结果


2、offsetLeftAddRight和offsetTopAddBttom

//相对于parent左右移动

offsetLeftAddRight(horizital_distance);

//相对于parent上下移动

offsetLeftAddRight(vertical_distance);

效果完全等同于layout


3、LayoutParams

ViewGroup.MarginLayoutParams params= 
(ViewGroup.MarginLayoutParams) getLayoutParams();params.leftMargin=getLeft()+x;params.topMargin=getTop()+y;setLayoutParams(params);
原理都是一样的,只是通过margin属性来展现而已
4、scrollTo与scrollBy
顾名思义,to是到具体的位置,by是变化量
注意:view.crollBy()和view.crollTo()滚动的都是View的content!!!不是View,
想要滚动View的话请getParent.crollBy()。
eg:
scrollBy(10,20)你是想让View的content向右向下分别滑动10和20;
然而事实是content向左向上分别滑动了10和20,OK,看出来了吧,他们的参考系是不一样的,
你只需scrollBy(-offsetX,-offsetY)就可以了
还有许多滚动相关的辅助类,太复杂,暂时不说了。




0 0