当前位置: 移动技术网 > IT编程>移动开发>Android > Android 属性动画实现效果

Android 属性动画实现效果

2018年03月25日  | 移动技术网IT编程  | 我要评论

义母的乳汁,搜,x220评测

本来想先放实现效果,后来手机实在捉急,只能后续再补啦。
一 实现透明效果:

//属性动画变透明
    private void alphaAnim(){
        //不透明--->透明--->不透明 2s钟时间平均分配
        ObjectAnimator objectAnimator=ObjectAnimator.ofFloat(imageView,"alpha",1f,0f,1f);
        //从不透明变成透明
//        ObjectAnimator objectAnimator=ObjectAnimator.ofFloat(imageView,"alpha",1f,0f);
        //只执行一次动画时,可以省略1f
//        ObjectAnimator objectAnimator=ObjectAnimator.ofFloat(imageView,"alpha",0f);
        objectAnimator.setDuration(2000);  //设置动画时间为2000ms
        objectAnimator.start();
    }

1f为完全不透明   0f为完全透明

二:实现旋转效果

//属性动画 旋转
    private void rotationAnim(){
        //0-->360 顺时针 360-->0 逆时针
        //下个度数大于上个度数,顺时针旋转;下个度数小于上个度数,逆时针旋转。
        ObjectAnimator objectAnimator=ObjectAnimator.ofFloat(imageView,"rotation",0f,360,0);
        objectAnimator.setDuration(5000);
        objectAnimator.start();
    }

三 实现平移效果:

//平移
    private void translationAnim(){
        // translationX 沿着x轴移动 上个数大于下个数,左移。反之右移
//        ObjectAnimator objectAnimator=ObjectAnimator.ofFloat(imageView,"translationX",0f,-300f,0f);  //左移-->右移
        // translationX 沿着x轴移动 上个数大于下个数,上移。反之下移
        ObjectAnimator objectAnimator=ObjectAnimator.ofFloat(imageView,"translationY",0f,300f,0f);  //下移-->上移
        objectAnimator.setDuration(5000);
        objectAnimator.start();
    }

四 实现缩放效果

//缩放
    private void scaleAnim(){
        ObjectAnimator objectAnimator=ObjectAnimator.ofFloat(imageView,"scaleX",1f,2f,1f); //沿着x轴放大两倍再返回
        ObjectAnimator objectAnimator1=ObjectAnimator.ofFloat(imageView,"scaleY",1f,2f,1f); //沿着Y轴放大两倍再返回
        objectAnimator.setDuration(5000);
        objectAnimator1.setDuration(5000);
        objectAnimator.start();
        objectAnimator1.start();
    }

五 组合动画
以上都是基本动画,如果只有基本动画,显然是不够的,所以还有个类AnimatorSet,专门来组合这些动画。
AnimatorSet:这个类提供了一个play()方法,调用后将会返回一个AnimatorSet.Builder的实例,AnimatorSet.Builder中包括以下四个方法:

after(Animator anim) :将现有动画插入到传入的动画之后执行
after(long delay) :将现有动画延迟指定毫秒后执行
before(Animator anim): 将现有动画插入到传入的动画之前执行
with(Animator anim) :将现有动画和传入的动画同时执行

//组合动画
    private void setAnim(){
        //沿x轴放大
        ObjectAnimator scaleXAnimator = ObjectAnimator.ofFloat(imageView, "scaleX", 1f, 2f, 1f);
        //沿y轴放大
        ObjectAnimator scaleYAnimator = ObjectAnimator.ofFloat(imageView, "scaleY", 1f, 2f, 1f);
        //移动
        ObjectAnimator translationXAnimator = ObjectAnimator.ofFloat(imageView, "translationX", 0f, 200f, 0f);
        //透明动画
        ObjectAnimator alphaAnimator = ObjectAnimator.ofFloat(imageView, "alpha", 1f, 0f, 1f);
        AnimatorSet animatorSet=new AnimatorSet();
        //同时沿X,Y轴放大,且改变透明度,然后移动
        //在3s内,沿x、y轴同时放大,然后缩小,在缩放的同时还要改变透明度。然后再完成3s的左右移动。
        animatorSet.play(scaleXAnimator).with(scaleYAnimator).with(alphaAnimator).before(translationXAnimator);
        //每个动画都设置成3s,也可以分别设置
        animatorSet.setDuration(3000);
        animatorSet.start();
    }

六 在xml中实现动画效果
1.透明:



    

在布局中的实现:

//alpha.xml对应的代码实现
    private void alphaXml(){
        Animator animator= AnimatorInflater.loadAnimator(this,R.animator.alpha);
        animator.setTarget(imageView);
        animator.start();
    }

2.旋转


3.移动


4.缩放:


5.组合:



    
        
        
            
            
        
    
    
        
        
            
            
        
    
这里需要说下set中的属性android:ordering:规定了这个set中的动画的执行顺序,包括:
together(默认):set中的动画同时执行
sequentially:set中的动画按顺序执行

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网