补间动画

来源:互联网 发布:o reilly 知乎 编辑:程序博客网 时间:2024/06/05 15:25

实现弧形坠落效果动画:


/** * ClassName:MainActivity * Description TODO 补间动画 * created by 漠天 * Data 2017/1/12 12:05 */public class MainActivity extends AppCompatActivity {    private ImageView mRotateIv, centerIv,funIv,rightIv;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mRotateIv = (ImageView) findViewById(R.id.rotateIv);        centerIv = (ImageView) findViewById(R.id.centerIv);        centerIv.setVisibility(View.INVISIBLE);        mRotateIv.setVisibility(View.INVISIBLE);        funIv = (ImageView) findViewById(R.id.funIv);        rightIv= (ImageView) findViewById(R.id.rightIv);        funIv.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                animationSetManage(mRotateIv,centerIv);            }        });        rightIv.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                centerIv.setVisibility(View.INVISIBLE);            }        });    }    /**     * 管理动画set     * @return     */    public void animationSetManage(final View fromView, final View toView){        //获取起点坐标        int[] location2 = new int[2];        fromView.getLocationInWindow(location2);        int x1 = location2[0];        int y1 = location2[1];        //获取终点坐标,最近拍摄的坐标        int[] location = new int[2];        toView.getLocationInWindow(location);        int x2 = location[0]+toView.getLayoutParams().width;        int y2 = location[1]+toView.getLayoutParams().height;        int offsetX = 10;        int offsetY = 10;        //两个位移动画        TranslateAnimation translateAnimationX = new TranslateAnimation(-fromView.getLayoutParams().width, -(x1 - x2) - offsetX, 0, 0);   //横向动画        TranslateAnimation translateAnimationY = new TranslateAnimation(-fromView.getLayoutParams().height, 0, 0, y2 - y1 + offsetY); //竖向动画        translateAnimationX.setInterpolator(new LinearInterpolator());  //横向动画设为匀速运动        translateAnimationX.setRepeatCount(0);// 动画重复执行的次数        translateAnimationY.setInterpolator(new AccelerateInterpolator());  //竖向动画设为开始结尾处加速,中间迅速        translateAnimationY.setRepeatCount(0);// 动画重复执行的次数        RotateAnimation animation =new RotateAnimation(-45f,0f,Animation.RELATIVE_TO_SELF, 0.5f,Animation.RELATIVE_TO_SELF,0.5f);        animation.setInterpolator(new LinearInterpolator());  //横向动画设为匀速运动        animation.setRepeatCount(0);        ScaleAnimation scaleAnimation = new ScaleAnimation(0.0f,1.0f,0.0f,1.0f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);        scaleAnimation.setRepeatCount(0);        // 组合动画        AnimationSet anim = new AnimationSet(false);        anim.setFillAfter(false);   //动画结束不停留在最后一帧        anim.addAnimation(translateAnimationX);        anim.addAnimation(translateAnimationY);        anim.addAnimation(scaleAnimation);        anim.addAnimation(animation);        anim.setAnimationListener(new Animation.AnimationListener() {   //抛物线动画结束后,执行淡出动画            @Override            public void onAnimationStart(Animation animation) {            }            @Override            public void onAnimationRepeat(Animation animation) {            }            @Override            public void onAnimationEnd(Animation animation) {                fromView.setVisibility(View.GONE);                toView.setVisibility(View.VISIBLE);                TranslateAnimation animation1 = new TranslateAnimation(0, -5, 0, -5);                animation1.setInterpolator(new OvershootInterpolator());                animation1.setDuration(100);                animation1.setRepeatCount(3);                animation1.setRepeatMode(Animation.REVERSE);                toView.startAnimation(animation1);            }        });        fromView.setVisibility(View.VISIBLE);        anim.setDuration(1000);// 动画的执行时间        anim.setStartOffset(400);        fromView.startAnimation(anim);    }}

还可使用属性动画实现

0 0
原创粉丝点击