android动画介绍--Animation实现loading动画效果

动画效果是通过Animation来实现的,一共有四种,分别为:

  • AlphaAnimation:渐变透明度动画
  • ScaleAnimation:尺寸渐变动画
  • TranslateAnimation:水平移动动画
  • RotateAnimation:旋转动画

实践:
首先在Activity的布局文件中加入一个ImageView和TextVIew

 <ImageView
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_centerInParent="true"
        android:src="@drawable/ic_loading"
        android:id="@+id/iv_loading_point"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv_loading_text"
        android:layout_below="@id/iv_loading_point"
        android:layout_centerHorizontal="true"
        android:text="Loading"
        android:textSize="20sp"
        />

然后把需要声明的属性声明:

 <ImageView
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_centerInParent="true"
        android:src="@drawable/ic_loading"
        android:id="@+id/iv_loading_point"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv_loading_text"
        android:layout_below="@id/iv_loading_point"
        android:layout_centerHorizontal="true"
        android:text="Loading"
        android:textSize="20sp"
        />

这里由于ImageView和TextView都使用了组合的动画效果,所以需要有两个容器AnimationSet来存放动画效果

TranslateAnimation ta = new TranslateAnimation(200,0,300,0);  
        ta.setDuration(5000);  
        RotateAnimation ra = new RotateAnimation(0,360, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);  
        ra.setDuration(5000);  
        mImageAni.addAnimation(ta);  
        mImageAni.addAnimation(ra);  

mImageAni里存放了一个TranslateAnimation也就是水平移动动画,它的参数是从哪里到哪里,这里是从200—>0,300—>0,之后调用setDuration()方法来设置动画效果持续的时间。另一个是RotateAnimation,也就是旋转动画,它的参数是从多少度到多少度,后面几个参数是从自身为基准,50%也就是中心点旋转
这两个动画效果组合起来实现了小圆球的旋转效果

ScaleAnimation sa = new ScaleAnimation(0,2.5f,0,2.5f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);  
      sa.setDuration(5000);  
      AlphaAnimation aa = new AlphaAnimation(0,1);  
      aa.setDuration(5000);  
      mTextAni.addAnimation(sa);  
      mTextAni.addAnimation(aa);  

第一个动画效果是ScaleAnimation也就是缩放效果动画,它的参数与与平移相似,从多少扩大到多少,后面的参数也是以自身为基准的50%,也就是中心。而AlphaAnimation透明动画的参数就比较简单,它的参数为透明度从多少到多少,这里是从消失到显示

mImageView.setOnClickListener(new View.OnClickListener() {  
            @Override  
            public void onClick(View view) {  
                mImageView.startAnimation(mImageAni);  
                mTextView.startAnimation(mTextAni);  
            }  
        });  

最后的效果:
这里写图片描述

本文来源:
http://blog.csdn.net/wingichoy/article/details/47104433

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值