动画新创建的未知大小的视图

时间:2016-12-27 09:42:36

标签: android android-animation android-custom-view

我希望在将视图添加到父级(类似于DrawerLayout)后立即对其进行动画处理。问题是View的大小不同,动画目标位置取决于该大小。简化的示例代码:

select columnname,count(column name) from tablename group by  column name having count(*)>1

与此类似的代码会触发动画:

    AnimatingView extends View {
        public int offsetX;

        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            final int screenWidth = MeasureSpec.getSize(widthMeasureSpec);
            final int screenHeight = MeasureSpec.getSize(heightMeasureSpec);
            offsetX = calculateOffset(screenWidth);
            ...
        }
    }

在这种情况下, @Override public void onClick(View v) { AnimatingView animatingView = new AnimatingView(getContext()); parentLayout.addView(animatingView); animatingView.animate().x(animatingView.offsetX).setDuration(500).start(); } 发生在onMeasure()之后,因此动画失败。做依赖于视图测量的东西的正确方法是什么?

简单&愚蠢的方式类似于基于animate()标志的animateOnceAfterMeasuring(),但我不认为这是正确的做法。

2 个答案:

答案 0 :(得分:2)

这应该有效:

public function saveProductTabData(Varien_Event_Observer $observer)
{
    if (!self::$_singletonFlag) {
        self::$_singletonFlag = true;   
        $product = $observer->getEvent()->getProduct();
        try {
            $customFieldValue =  $this->_getRequest()->getPost('custom_field');
            $product->setNewAttribute($customFieldValue); 
            #$product->custom_field = $customFieldValue;
            $product->save();
        }
        catch (Exception $e) {
            Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
        }
    }
}

答案 1 :(得分:1)

您可以将ViewTreeObserver用于此

@Override
public void onClick(View v) {
    final AnimatingView animatingView = new AnimatingView(getContext());
    parentLayout.addView(animatingView);
    animatingView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            if (Build.VERSION.SDK_INT < 16) {
                animatingView.getViewTreeObserver().removeGlobalOnLayoutL‌​istener(this);
            } else {
                animatingView.getViewTreeObserver().removeOnGlobalLayoutList‌​ener(this);
            }
            animatingView.animate().x(animatingView.offsetX).setDuration(500).start();
        }
    });
}