Hello everyone. I make custom AdapterView which items should be
scrolled in two directions (from bottom left to top rigth and
conversely). During scrolling I use TranslateAnimation and
AlphaAnimation.

Here is the steps of scrolling:

1) Top (Bottom) View starts to hidem using AlphaAnimation
2) I start TranslateAnimation for every other items in loop and they
relocated to the new positions
3) At the end of AlphaAnimation I make border View invisible and
remove
4) At the end of TranslateAnimation every translated item get new
positions (using layout() calling)), I remove faded View from layout
and store it as convert in the buffer
5) Then I call adapter's getView() to get convertedView and position
it on the bottom (top) of my AdapterView, make this view visible and
start AlphaAnimation (fade in)

The problem is that this combination of animation works well with
deffinite values of their durations, but if I make duration values
smaller they stop working after third running.

Here are the code snippets for more clearness:

private void hideView(final View v, final boolean scrollDown) {
                AlphaAnimation hiding = new AlphaAnimation(1.0f, 0.0f);
                hiding.setDuration(700);

                hiding.setAnimationListener(
                        new AnimationListener() {

                                @Override
                                public void onAnimationStart(Animation 
animation) {}

                                @Override
                                public void onAnimationRepeat(Animation 
animation) {}

                                @Override
                                public void onAnimationEnd(Animation animation) 
{
                                        v.setVisibility(View.INVISIBLE);
                                }
                        }
                );
                v.startAnimation(hiding);
        }

private void scrollDown() {
                final View frontView = getChildAt(getChildCount() - 1);
                hideView(frontView, true);

                for(int i = getChildCount() - 2; i >= 0; i--) {
                        final View child = getChildAt(i);
                        final int newleft = child.getLeft() - 
mOffsetBetweenItems;
                        final int newTop = child.getTop() + mOffsetBetweenItems;
                        final int index = i;
                        TranslateAnimation scrollDown = new 
TranslateAnimation(0, -
mOffsetBetweenItems, 0, mOffsetBetweenItems);
                        scrollDown.setDuration(750);
                        scrollDown.setFillEnabled(true);
                        scrollDown.setAnimationListener(
                                new AnimationListener() {

                                        @Override
                                        public void onAnimationStart(Animation 
animation) {}

                                        @Override
                                        public void onAnimationRepeat(Animation 
animation) {}

                                        @Override
                                        public void onAnimationEnd(Animation 
animation) {
                                                child.layout(newleft, newTop, 
newleft +
child.getMeasuredWidth(), newTop + child.getMeasuredHeight() );
                                                if(index == 0) {
                                                        removeFromFront();
                                                        fillFromBehind();
                                                }
                                        }
                                }
                        );

                        child.startAnimation(scrollDown);
                }
        }

private void scrollUp() {
                final View backView = getChildAt(0);
                hideView(backView, true);

                for(int i = 1; i < getChildCount(); i++) {
                        final View child = getChildAt(i);
                        final int newleft = child.getLeft() + 
mOffsetBetweenItems;
                        final int newTop = child.getTop() - mOffsetBetweenItems;
                        final int index = i;
                        TranslateAnimation scrollUp = new TranslateAnimation(0,
mOffsetBetweenItems, 0, -mOffsetBetweenItems);
                        scrollUp.setDuration(750);
                        scrollUp.setFillEnabled(true);
                        scrollUp.setAnimationListener(
                                new AnimationListener() {

                                        @Override
                                        public void onAnimationStart(Animation 
animation) {}

                                        @Override
                                        public void onAnimationRepeat(Animation 
animation) {}

                                        @Override
                                        public void onAnimationEnd(Animation 
animation) {
                                                child.layout(newleft, newTop, 
newleft +
child.getMeasuredWidth(), newTop + child.getMeasuredHeight() );
                                                if(index == getChildCount() - 
1) {
                                                        removeFromBehind();
                                                        fillFromTop();
                                                }
                                        }
                                }
                        );

                        child.startAnimation(scrollUp);
                }

        }

private void removeFromBehind() {
                int childCount = getChildCount();

        if (childCount > 1) {
                View behindChild = getChildAt(0);

                if(behindChild != null) {
                        removeViewInLayout(behindChild);
                        if(!mViewBuffer.isEmpty())
                                mViewBuffer.remove();
                        mViewBuffer.add(behindChild);
                        if(mLastItemPosition == 0) {
                                mLastItemPosition = mAdapter.getCount() - 1;
                        } else {
                                mLastItemPosition --;
                        }
                }
                else {
                        Log.d("onLayout()", "VIEW IS NULL!!!");
                }
        }
}

private void removeFromFront() {
        int childCount = getChildCount();

        if (childCount > 1) {
                View frontChild = getChildAt(childCount - 1);

                if(frontChild != null) {
                        removeViewInLayout(frontChild);
                        if(!mViewBuffer.isEmpty())
                                mViewBuffer.remove();
                        mViewBuffer.add(frontChild);
                        if(mFirstItemPosition == mAdapter.getCount() - 1) {
                                mFirstItemPosition = 0;
                        } else {
                                mFirstItemPosition ++;
                        }
                }
        }
}

private void fillFromBehind() {
        int countOfAdaptedItems = mAdapter.getCount();

        if(mLastItemPosition == countOfAdaptedItems - 1) {
                mLastItemPosition = 0;
        } else {
                mLastItemPosition++;
        }

        final View child = mAdapter.getView(mLastItemPosition,
getCachedView(), this);

        if(child.getVisibility() != VISIBLE) child.setVisibility(VISIBLE);

        addAndMeasureChild(child, false);

        int top = getChildAt(1).getTop() - mOffsetBetweenItems;
        int width = child.getMeasuredWidth();
        int height = child.getMeasuredHeight();
        int left = getChildAt(1).getLeft() + mOffsetBetweenItems;

        child.layout(left, top, left + width, top + height);

        showUpView(child);
}

private void fillFromTop() {
        if(mFirstItemPosition == 0) {
                mFirstItemPosition = mAdapter.getCount() - 1;
        }
        else {
                mFirstItemPosition--;
        }

        final View child = mAdapter.getView(mFirstItemPosition,
getCachedView(), this);

        if(child.getVisibility() != VISIBLE) child.setVisibility(VISIBLE);

        addAndMeasureChild(child, true);

        int top = getChildAt(getChildCount() - 2).getTop() +
mOffsetBetweenItems;
        int width = child.getMeasuredWidth();
        int height = child.getMeasuredHeight();
        int left = getChildAt(getChildCount() - 2).getLeft() -
mOffsetBetweenItems;

        child.layout(left, top, left + width, top + height);

        showUpView(child);
}

private void showUpView(final View v) {
        AlphaAnimation appearing = new AlphaAnimation(0.0f, 1.0f);
        appearing.setDuration(700);

        v.startAnimation(appearing);
}

private View getCachedView() {
        if(!mViewBuffer.isEmpty())
                return mViewBuffer.getFirst();

        return null;
}

It works well with this duration values of animation, but if I set for
example 500 ms for scroll animation it works only 3 times. I'v got
really feeling like there is infinite loop in startAnimation().

Tell me please what don't I consider in my implementation?

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to