自定义控件ViewGroup绘制过程以及使用Scroller类处理滑动效果

知识点:

1 ViewGroup的绘制

2 Scroller类的用法


1 ViewGroup绘制

对于View绘制的基本过程在参考了一篇博客,里面较为详细的介绍了绘制的基本流程:http://blog.csdn.net/qinjuning/article/details/7110211

在实际的开发中根据View绘制的基本流程,只需要在自定义空间中重写OnMeasure,以及OnLayout函数即可。

OnMeasure:

@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		// TODO Auto-generated method stub
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		
		int childCount = getChildCount(); // 获得子控件个数
		// 获取ViewGroup实际的宽和高
		int specSize_Widht = MeasureSpec.getSize(widthMeasureSpec);
		int specSize_Heigth = MeasureSpec.getSize(heightMeasureSpec);
		
		// 设置本ViewGroup的宽和高
		setMeasuredDimension(specSize_Widht, specSize_Heigth);

		/**
		 * 子ChildView
		 */
		for (int i = 0; i < childCount; i++) {
			View child = getChildAt(i);
			child.measure(50, 50);
			this.measureChild(child, widthMeasureSpec, heightMeasureSpec);

		}

	}

OnLayout:

@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		// System.out.println("onLayout!!!");
		int startLeft = 0;
		int startTop = 10;
		int childCount = getChildCount();
		int tempRefreshHeight=0;
		for (int i = 0; i < childCount; i++) {
			View child = getChildAt(i);
			child.layout(startLeft, startTop,
					startLeft + child.getMeasuredWidth(),
					startTop + child.getMeasuredHeight());
			
			startTop = startTop + child.getMeasuredHeight() + 10;
			
		}
		
	}

二 Scroller类的使用方法:

Android里Scroller类是为了实现View平滑滚动的一个Helper类。通常在自定义的View时使用,在View中定义一个私有成员mScroller = new Scroller(context)。设置mScroller滚动的位置时,并不会导致View的滚动,通常是用mScroller记录/计算View滚动的位置,再重写View的computeScroll(),完成实际的滚动。

Scroller类具体的API:

mScroller.getCurrX() //获取mScroller当前水平滚动的位置  
mScroller.getCurrY() //获取mScroller当前竖直滚动的位置  
mScroller.getFinalX() //获取mScroller最终停止的水平位置  
mScroller.getFinalY() //获取mScroller最终停止的竖直位置  
mScroller.setFinalX(int newX) //设置mScroller最终停留的水平位置,没有动画效果,直接跳到目标位置  
mScroller.setFinalY(int newY) //设置mScroller最终停留的竖直位置,没有动画效果,直接跳到目标位置  
  
//滚动,startX, startY为开始滚动的位置,dx,dy为滚动的偏移量, duration为完成滚动的时间  
mScroller.startScroll(int startX, int startY, int dx, int dy) //使用默认完成时间250ms  
mScroller.startScroll(int startX, int startY, int dx, int dy, int duration)  
  
mScroller.computeScrollOffset() //返回值为boolean,true说明滚动尚未完成,false说明滚动已经完成。这是一个很重要的方法,通常放在View.computeScroll()中,用来判断是否滚动是否结束。  

具体实现:

public class CustomView extends LinearLayout {  
  
    private static final String TAG = "Scroller";  
  
    private Scroller mScroller;  
  
    public CustomView(Context context, AttributeSet attrs) {  
        super(context, attrs);  
        mScroller = new Scroller(context);  
    }  
  
    //调用此方法滚动到目标位置  
    public void smoothScrollTo(int fx, int fy) {  
        int dx = fx - mScroller.getFinalX();  
        int dy = fy - mScroller.getFinalY();  
        smoothScrollBy(dx, dy);  
    }  
  
    //调用此方法设置滚动的相对偏移  
    public void smoothScrollBy(int dx, int dy) {  
  
        //设置mScroller的滚动偏移量  
        mScroller.startScroll(mScroller.getFinalX(), mScroller.getFinalY(), dx, dy);  
        invalidate();//这里必须调用invalidate()才能保证computeScroll()会被调用,否则不一定会刷新界面,看不到滚动效果  
    }  
      
    @Override  
    public void computeScroll() {  
      
        //先判断mScroller滚动是否完成  
        if (mScroller.computeScrollOffset()) {  
          
            //这里调用View的scrollTo()完成实际的滚动  
            scrollTo(mScroller.getCurrX(), mScroller.getCurrY());  
              
            //必须调用该方法,否则不一定能看到滚动效果  
            postInvalidate();  
        }  
        super.computeScroll();  
    }  
}  

Scroller类的用法转自一篇小而精美的博客:http://ipjmc.iteye.com/blog/1615828
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值