简体   繁体   English

如何在Android中以编程方式为视图设置动画?

[英]How to animate a view programatically in android?

I am making an animated tutorial for the application. 我正在为该应用程序制作动画教程。 I am using a fragment over the activity to show image view sliding effect. 我在活动上方使用一个片段来显示图像视图滑动效果。 I am using Translate animation to move the image view to the specific view to describe its working, but actually I want to animate the image view to move infinite times after reaching to the view. 我正在使用“转换动画”将图像视图移动到特定视图以描述其工作,但是实际上我想对图像视图进行动画处理,以使其到达该视图后无限次移动。

If you want to animate infinite times, you could use android RotateAnimation class 如果您想无限动画,可以使用android RotateAnimation

Hope this could help you: 希望这可以帮助您:

RotateAnimation anim = new RotateAnimation(0f, 350f, 15f, 15f);
anim.setInterpolator(new LinearInterpolator());
anim.setRepeatCount(Animation.INFINITE);
anim.setDuration(700);

// Start animating the image
final ImageView splash = (ImageView) findViewById(R.id.splash);
splash.startAnimation(anim);

// Later.. stop the animation
splash.setAnimation(null);

Assume that view v is suppose to animate 假设视图v应该是动画

    public void ViewExapand(View v)
        {
                        v.setVisibility(View.VISIBLE);

                        final int widthSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
                        final int heightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
                        v.measure(widthSpec, heightSpec);
                        ValueAnimator mAnimator = slideAnimator(0, v.getMeasuredHeight(),v);
                        mAnimator.start();
        }



private ValueAnimator slideAnimator(int start, int end, final View v)
    {
        ValueAnimator animator = ValueAnimator.ofInt(start, end);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                //Update Height
                int value = (Integer) valueAnimator.getAnimatedValue();
                ViewGroup.LayoutParams layoutParams = v.getLayoutParams();
                layoutParams.height = value;
                v.setLayoutParams(layoutParams);
            }
        });
        return animator;
    }

You can also use ObjectAnimator 您也可以使用ObjectAnimator

ObjectAnimator.ofInt(your view, "left", 100, 250)
    .setDuration(250)
    .start();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM