2 orosem625 orosem625 于 2013.01.22 16:22 提问

android中的动画可以改变一个view的高和宽吗?

简单点说把,就是在android中通过动画可以改变这个对象的高度和宽度吗?举个例子,现在有一个图片,是imageview,我可不可以给他做一个动画让他高度变大?这个变大是指他实际占用的位置,比如这个imagview我在xml里配置的是200dip,通过动画我可以让他再动画结束后的高度变成500dip吗?

6个回答

redCoral_
redCoral_   2013.01.22 17:39
已采纳

类似效果,你的动画改变view布局参数,应该对imageView应用新的布局参数。

创建应用新布局参数(lp)的自定义动画,对不同imageView每个框架都设置不同高宽。然后图片就可以增大尺寸,并且移动其他图片,这样的例子在网上可以找到。

@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
    android.view.ViewGroup.LayoutParams lp = mContent.getLayoutParams();
    lp.height = (int) (mStartHeight + mDeltaHeight * interpolatedTime);
    mContent.setLayoutParams(lp);
}
redCoral_
redCoral_ 是的,你可以看看这个链接:http://stackoverflow.com/questions/5165682/how-to-implement-expandable-panels-in-android
接近 5 年之前 回复
orosem625
orosem625 我感觉你的讲述有点接近我想要的效果,你说的这个动画是指scale的动画还是什么?是要让这个高度和动画的时间绑定上吗?可以给个传送门什么的吗?谢谢
接近 5 年之前 回复
Kakalapa1986
Kakalapa1986   2013.01.22 17:01

如果你用了ScaleAnimation, 可以设置属性setFillAfter(true);

u013415968
u013415968 应该是直接占用 看一下源码
3 年多之前 回复
orosem625
orosem625 你好,这个是平移,如果我x方向不移动,y方向移动的那个是拉伸,请问这个拉伸对于mapview的话它是视觉效果变了还是他这个mapview的直接占用区域也变了呢?
接近 5 年之前 回复
u010859737
u010859737   2013.05.27 22:12
sunhangok
sunhangok   2014.01.03 12:05

也可以用属性动画来解决这个问题。属性动画:http://developer.android.com/guide/topics/graphics/prop-animation.html

chang_xing
chang_xing   2014.04.03 09:50

可以的 ,做动画,或是动态改变属性

u013415968
u013415968   2014.05.25 21:30

应该是直接占用 看一下源码

/**
* Gets the transformation to apply at a specified point in time. Implementations of this
* method should always replace the specified Transformation or document they are doing
* otherwise.
*
* @param currentTime Where we are in the animation. This is wall clock time.
* @param outTransformation A transformation object that is provided by the
* caller and will be filled in by the animation.
* @return True if the animation is still running
*/
public boolean getTransformation(long currentTime, Transformation outTransformation) {
if (mStartTime == -1) {
mStartTime = currentTime;
}

    final long startOffset = getStartOffset();
    final long duration = mDuration;
    float normalizedTime;
    if (duration != 0) {
        normalizedTime = ((float) (currentTime - (mStartTime + startOffset))) /
                (float) duration;
    } else {
        // time is a step-change with a zero duration
        normalizedTime = currentTime < mStartTime ? 0.0f : 1.0f;
    }

    final boolean expired = normalizedTime >= 1.0f;
    mMore = !expired;

    if (!mFillEnabled) normalizedTime = Math.max(Math.min(normalizedTime, 1.0f), 0.0f);

    if ((normalizedTime >= 0.0f || mFillBefore) && (normalizedTime <= 1.0f || mFillAfter)) {
        if (!mStarted) {
            fireAnimationStart();
            mStarted = true;
            if (USE_CLOSEGUARD) {
                guard.open("cancel or detach or getTransformation");
            }
        }

        if (mFillEnabled) normalizedTime = Math.max(Math.min(normalizedTime, 1.0f), 0.0f);

        if (mCycleFlip) {
            normalizedTime = 1.0f - normalizedTime;
        }

        final float interpolatedTime = mInterpolator.getInterpolation(normalizedTime);
        applyTransformation(interpolatedTime, outTransformation);
    }

    if (expired) {
        if (mRepeatCount == mRepeated) {
            if (!mEnded) {
                mEnded = true;
                guard.close();
                fireAnimationEnd();
            }
        } else {
            if (mRepeatCount > 0) {
                mRepeated++;
            }

            if (mRepeatMode == REVERSE) {
                mCycleFlip = !mCycleFlip;
            }

            mStartTime = -1;
            mMore = true;

            fireAnimationRepeat();
        }
    }

    if (!mMore && mOneMoreTime) {
        mOneMoreTime = false;
        return true;
    }

    return mMore;
}

还有 这个

/**
* If fillAfter is true, the transformation that this animation performed
* will persist when it is finished. Defaults to false if not set.
* Note that this applies to individual animations and when using an {@link
* android.view.animation.AnimationSet AnimationSet} to chain
* animations.
*
* @param fillAfter true if the animation should apply its transformation after it ends
* @attr ref android.R.styleable#Animation_fillAfter
*
* @see #setFillEnabled(boolean)
*/
public void setFillAfter(boolean fillAfter) {
mFillAfter = fillAfter;
}

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!