动画初步使用以及自定义插值器

动态初始化Animation,并让动画一个组件运动到另外一个组件的代码

    
    final ImageView to=..
    final ImageView from=..
    
    
    int [] location = new int [2];
    from.getLocationInWindow(location);
    int [] locationTo = new int [2];
    to.getLocationInWindow(locationTo);
    AnimationSet set = new AnimationSet(true);
    animation = null;
    
    float scaleX = to.getDrawable().getIntrinsicWidth()/(float)from.getDrawable().getIntrinsicWidth();
    float scaleY = to.getDrawable().getIntrinsicHeight()/(float)from.getDrawable().getIntrinsicHeight();
    // 动画的所有from属性,都是相对与做动画的组件的做参考的
    //比如,scale动画,from 1代表相对当前组件缩放比例不变     translate动画,from 0 代表,相对当前组件位置,没位移
    animation = new ScaleAnimation(1, scaleX, 1, scaleY);
    set.addAnimation(animation);
    
    animation = new TranslateAnimation(0, locationTo[0]-location[0], 0, locationTo[1]-location[1]);
    set.addAnimation(animation);
    
    set.setDuration(1000);
    set.setRepeatCount(0);
    set.setInterpolator(new MyInterpolator());
    from.startAnimation(set);

 自己定义的几种插值器

/**
     * 插值器类中的方程是 时间和变化幅度(如TranslateAnimation动画,则代表的位移) 0,0 ---1,1 区域内的曲线方程。当然,也可以超出这个区域,但是曲线必须经过1,1这个点
     * 变化速率或速度调整当然就是切线角度了
     * @author cs
     *
     */
    static class MyInterpolator implements Interpolator{

        @Override
        public float getInterpolation(float input) {
            return getSquareDown(input);
        }
        //速度慢慢增大,再减小。
        private static float getSin(float input){
            return (float) (Math.sin(input*Math.PI-Math.PI/2)+1)/2;
        }
        //速度持续增大
        private static float getCubeUp(float input){
            return input*input*input;
        }
        //速度慢慢减缓。
        private static float getSquareDown(float input){
            input -= 1;
            return -input*input+1;
        }
        // 非平滑曲线 
        private static float getSeparete(float input){
            if(input<0.2){// 会有突变到暂停不变的效果
                return 0.1f;
            }else{
                return (9*input-1)/8;
            }
        }
        
    }

 

posted @ 2013-09-02 09:57  boliu  阅读(1193)  评论(0编辑  收藏  举报
hljs.initHighlightingOnLoad(); 最恶心的莫过于找不出的bug和夏天的蚊子
最爽的当然也是干掉的bug和拍死的蚊子