Search code examples
androidlistviewanimationscale

android grow box animation


Inside of a ListView row, I have a box (right now its an imageview, but i will probably be converting it to a linear or relative layout object at some point). when you click on the box, i have it start a scale animation that makes it appear to grow. however, once the animation is over, the box goes immediately back to its original state. I'd like to have the box stay at the transformed size (at least until they click it again).

here's the xml:

<scale
    android:fromXScale="1" android:toXScale="5"
    android:fromYScale="1" android:toYScale="1"
    android:pivotX="100%" android:pivotY="0%" android:fillAfter="true"
    android:duration="600" />


Solution

  • the only way i was able to do this was by changing the width of the box on a new thread every 1ms. not pretty but it seemed to be the only way to maintain the borders of the box. if you come up w/ an option that uses the animation f/w please do let me know...

    here's the code Rohan, don't forget to give me an up vote :)

        private void toggleMenu(final ViewHolder menu, final boolean doExpand) {
        final LayoutParams params = menu.actionMenu.getLayoutParams();
        final int originalWidth = params.width;
    
        Thread t = new Thread() {
            @Override
            public void run() {
                for (int i = 0; i < 200; i++) {
                    if (doExpand) {
                        params.width = originalWidth + i;
                    } else {
                        params.width = originalWidth - i;
                    }
                    menu.actionMenu.post(new Runnable() {
    
                        @Override
                        public void run() {
                            menu.actionMenu.requestLayout();
    
                        }
                    });
                    try {
                        Thread.sleep(1);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
    
                menu.expandCollapseButton.post(new Runnable() {
                    @Override
                    public void run() {
                        if (doExpand) {
                            menu.expandCollapseButton
                                    .setImageResource(R.drawable.monotone_arrow_right);
                        } else {
                            menu.expandCollapseButton
                                    .setImageResource(R.drawable.monotone_arrow_leftpng);
                        }
    
                    }
                });
    
                // menu.isExpanded = !menu.isExpanded;
            }
        };
        t.start();
    }