View 平滑移动
来源:互联网 发布:多线程编程书籍 编辑:程序博客网 时间:2024/05/16 13:46
实现View滑动有多种方式,比如使用动画,动态改变margin值等,本文暂时只讨论使用scrollTo/scrollBy方式,结合Scroller类,实现view的平滑移动。
View内部实现了定义了scrollTo,scrollBy方法
/** * Set the scrolled position of your view. This will cause a call to * {@link #onScrollChanged(int, int, int, int)} and the view will be * invalidated. * @param x the x position to scroll to * @param y the y position to scroll to * 设置view的滚动位置,会调用onScroolChanded(),并使view失效,重绘 */ public void scrollTo(int x, int y) { if (mScrollX != x || mScrollY != y) { int oldX = mScrollX; int oldY = mScrollY; mScrollX = x; mScrollY = y; invalidateParentCaches(); onScrollChanged(mScrollX, mScrollY, oldX, oldY); if (!awakenScrollBars()) { postInvalidateOnAnimation(); } } }
/** * Move the scrolled position of your view. This will cause a call to * {@link #onScrollChanged(int, int, int, int)} and the view will be * invalidated. * @param x the amount of pixels to scroll by horizontally * @param y the amount of pixels to scroll by vertically */ public void scrollBy(int x, int y) { scrollTo(mScrollX + x, mScrollY + y); }
scrollBy()内部会调用scrollTo();
在View.java中提供了了如下两个变量以及相应的属性方法去读取滚动值 ,如下: View.java类中
/** * The offset, in pixels, by which the content of this view is scrolled * horizontally. * {@hide} */ protected int mScrollX; //该视图内容相当于视图起始坐标的偏移量 , X轴 方向 /** * The offset, in pixels, by which the content of this view is scrolled * vertically. * {@hide} */ protected int mScrollY; //该视图内容相当于视图起始坐标的偏移量 , Y轴方向 /** * Return the scrolled left position of this view. This is the left edge of * the displayed part of your view. You do not need to draw any pixels * farther left, since those are outside of the frame of your view on * screen. * * @return The left edge of the displayed part of your view, in pixels. */ public final int getScrollX() { return mScrollX; } /** * Return the scrolled top position of this view. This is the top edge of * the displayed part of your view. You do not need to draw any pixels above * it, since those are outside of the frame of your view on screen. * * @return The top edge of the displayed part of your view, in pixels. */ public final int getScrollY() { return mScrollY; }
public void scrollTo(int x, int y)
说明:在当前视图内容偏移至(x , y)坐标处,即显示(可视)区域位于(x , y)坐标处。
public void scrollBy(int x, int y)
说明:在当前视图内容继续偏移(x , y)个单位,显示(可视)区域也跟着偏移(x,y)个单位。
平滑移动的实现:
1 自定义View MyView.java
public class MyView extends View
2 new Scroller对象
Scroller mScroller = new Scroller(getContext());
3 view中重写computeScroll()方法
@Override public void computeScroll() { if (!mScroller.computeScrollOffset()){ scrollTo(mScroller.getCurrX(),mScroller.getCurrY()); postInvalidate(); } }
4 实现smoothScrollBy(int destX, int destY);
public void smothScrollBy(int destX, int destY){ mScroller.startScroll(getScrollX(), getScrollY(), destX, destY, 400); invalidate(); }
需要注意的是,view 的 ScrollTo平移的是view的内容,不能改变view在父容器中的位置
0 1
- View 平滑移动
- Android移动view动画问题(让移动更平滑)
- 与viewpager联动实现其他view平滑移动效果
- viewpager联动实现其他view平滑移动效果
- View绘制总结--View结合Scroller实现平滑移动(computeScroll())
- 自定义View-平滑滚动
- 自绘控件开发探索3--如何实现view的平滑移动
- Gallery平滑移动
- js平滑移动到锚点
- 摄像机平滑移动
- ListView 平滑移动
- camera平滑移动
- 平滑的手写签名View
- ListView进行平滑的移动
- js 滚动条平滑移动
- 159_平滑移动动画
- 照相机随人物平滑移动
- 移动平均和指数平滑
- Java中如何安全的结束一个线程(转载)
- ISE设置默认引脚 ,上拉或下拉或三态
- Android TextView 设置超链接、关键字高亮等效果
- 创建安卓app的经验教训
- 视图UIView的frame、bounds与center属性
- View 平滑移动
- 正则表达式 - 语法
- 写出高效清晰Layout布局文件的一些技巧--------自定义CoordinatorLayout:实现淘宝和QQ ToolBar透明渐变效果-------自定义Coordi快速返回效果
- vs2008突然不能使用查找功能了
- 学习Discuz! X3.2记录:使用superslide幻灯特效,配置方法
- 第八章 多态
- TortoiseSVN客户端重新设置用户名和密码
- codeforces587B Duff in Beach
- 韩顺平 javascript教学视频_学习笔记25_bom介绍_dom对象层次图_dom对象(window对象1)