Heim  >  Artikel  >  Web-Frontend  >  android动画介绍Animation 实现loading动画效果_html/css_WEB-ITnose

android动画介绍Animation 实现loading动画效果_html/css_WEB-ITnose

WBOY
WBOYOriginal
2016-06-24 11:40:091362Durchsuche

Animation的使用方法并不难。这里简单的介绍一下使用方法。

先看效果图:


效果还是不错的吧。 下面来看看使用方法。

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

AlphaAnimation : 渐变透明度动画

ScaleAnimation:  尺寸渐变动画

TranslateAnimation: 水平移动动画

RotateAnimation:  旋转动画

那么为了实现我的效果图上的效果呢。我们所有的动画都使用到了。

首先我们在Activity的布局文件中加入一个ImageView和一个TextView,使他们在布局居中。

    <imageview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerinparent="true" android:src="@drawable/point" android:id="@+id/point"></imageview>    <textview android:layout_below="@+id/point" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="loadIng..." android:layout_centerhorizontal="true" android:textsize="20sp" android:id="@+id/loading"></textview>

然后修改MainActivity.java

先把需要声明的属性声明

    private ImageView mImageView;    private TextView mTextView;    private AnimationSet mImageAni;    private AnimationSet mTextAni;

这里由于 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, 也就是旋转动画。他的参数是从多少度到多少度(0-->360),后面几个参数是从自身为基准,50% 也就是中心点旋转.

这两个动画效果组合起来就实现了小圆球的旋转效果。

接下来来看一下mTextAni

  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  透明动画的参数就比较简单,它的参数为从透明度多少到透明度多少,这里是从消失到显示.

最后我们给ImageView 添加一个监听事件,点击的时候播放动画效果

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


大功告成! 快去试试吧,另外如果你感兴趣,还可以看看其他的参数,和怎样用xml来实现动画。

源代码下载

版权声明:本文为博主原创文章,未经博主允许不得转载。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn