首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用ViewPropertyAnimator将宽度设置为特定值

如何使用ViewPropertyAnimator将宽度设置为特定值
EN

Stack Overflow用户
提问于 2015-02-19 15:02:52
回答 2查看 1.5K关注 0票数 5

如何使用ViewPropertyAnimator设置视图宽度?

我可以缩放或平移(见下文),但是我不能设置一个特定的宽度。

代码语言:javascript
复制
frame_1.animate().scaleX(5).scaleY(5).start();

但是没有

代码语言:javascript
复制
frame_1.animate().width(1024).height(768).start();
EN

回答 2

Stack Overflow用户

发布于 2015-11-26 15:07:29

使用简单的动画代替ViewPropertyAnimator

代码语言:javascript
复制
public class ResizeWidthAnimation extends Animation
{ 
    private int mWidth;
    private int mStartWidth;
    private View mView;

    public ResizeWidthAnimation(View view, int width)
    { 
        mView = view;
        mWidth = width;
        mStartWidth = view.getWidth();
    } 

    @Override 
    protected void applyTransformation(float interpolatedTime, Transformation t)
    { 
        int newWidth = mStartWidth + (int) ((mWidth - mStartWidth) * interpolatedTime);

        mView.getLayoutParams().width = newWidth;
        mView.requestLayout();
    } 

    @Override 
    public void initialize(int width, int height, int parentWidth, int parentHeight)
    { 
        super.initialize(width, height, parentWidth, parentHeight);
    } 

    @Override 
    public boolean willChangeBounds() 
    { 
            return true; 
    } 
} 
票数 1
EN

Stack Overflow用户

发布于 2016-07-18 19:42:17

试试这个,即使是Android 2.3也支持:

代码语言:javascript
复制
    ValueAnimatorCompat exitAnimator = AnimatorCompatHelper.emptyValueAnimator();
    exitAnimator.setDuration(TransitCompat.ANIM_DURATION * 4);
    exitAnimator.addUpdateListener(new AnimatorUpdateListenerCompat() {
        private float oldWidth =  view.getWidth();
        private float endWidth = 0;
        private float oldHeight =  view.getHeight();
        private float endHeight = 0;
        private Interpolator interpolator2 = new BakedBezierInterpolator();//Any other will be also O.K.

        @Override
        public void onAnimationUpdate(ValueAnimatorCompat animation) {
            float fraction = interpolator2.getInterpolation(animation.getAnimatedFraction());

            float width =  oldWidth + (fraction * (endWidth - oldWidth));
            mTarget.get().getLayoutParams().width = (int) width;

            float height =  oldHeight + (fraction * (endHeight - oldHeight));
            view.getLayoutParams().height = (int) height;


            view.requestLayout();
        }
    });

    exitAnimator.start();
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28600412

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档