I have used OverScroller for implementing scroll of my view.

Here is some code:

@Override
protected void onDraw(Canvas canvas) {
    if (scroller.computeScrollOffset()){
        int x = scroller.getCurrX();
        int y = scroller.getCurrY();
        scrollTo(x, y);

        ViewCompat.postInvalidateOnAnimation(this);
    }

    super.onDraw(canvas);
}

public void open(){
    scroller.startScroll(0, 0, 0, -mContent.getMeasuredHeight(), ANIMATION_TIME);
    invalidate();
}

public void close(){
    scroller.startScroll(0, getScrollY(), 0, mContent.getMeasuredHeight(), ANIMATION_TIME);
    invalidate();
}

It works fine. But on device with Full HD screen (Sony xperia Z) the method onDraw calls 4 times. On "Samsung Galaxy Note 2" it calls about 10 times. Hence on xperia i see lags. What can i do to improve performance ?

UPD: Here is full code http://xsnippet.org/359714

有帮助吗?

解决方案

you should override computeScroll() for things like that and not onDraw()

其他提示

You can override onFinishInflate () method in your custom View class and you can write OverScroll related code under this method instead of onDraw(). For more detail click here.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top