本文实例为大家分享了Android补间动画展示的具体代码,供大家参考,具体内容如下
创新互联公司服务项目包括津市网站建设、津市网站制作、津市网页制作以及津市网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,津市网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到津市省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!
首先看看补间动画的共同属性:
Duration:动画持续的时间(单位:毫秒)
fillAfter:设置为true,动画转化在动画被结束后被应用
fillBefore:设置为true,动画转化在动画开始前被应用
interpolator:动画插入器(加速、减速插入器)
repeatCount:动画重复的次数
repeatMode:顺序动画(restart)/倒序动画(reverse)
startOffset:动画之间时间间隔
对于动画的创建一般有两种方式:
第一种是在res/新建一个anim文件夹,然后在其下面分别建立四种动画
第二种方式是通过java代码的方式创建
在补间动画中我们通常有以下四种动画:
位移动画
创建方式一:
在anim文件下新建一个translate资源文件
<?xml version="1.0" encoding="utf-8"?>
//通过以下代码注册该动画
private void creatTranslateByInflate(){
Animation animation = AnimationUtils.loadAnimation(this, R.anim.translate);
mCircle.startAnimation(animation);
}创建方式二:(代码创建)
private void creatTranslateByCode() {
TranslateAnimation animation = new TranslateAnimation(0, 100, 0, 0);
animation.setDuration(2000);
animation.setRepeatMode(Animation.REVERSE);
animation.setRepeatCount(2);
mCircle.startAnimation(animation);
}旋转动画
创建方式一:
<?xml version="1.0" encoding="utf-8"?>
终止的角度)
//这里就是顺时针的旋转180
android:pivotX="50%" //旋转中轴的x点,50%表示以控件宽为基准,在控件的中间x点
android:pivotY="50%" //旋转中轴的y点,50%表示以控件高为基准,在控件的中间y点
android:duration="2000"
android:repeatMode="reverse"
android:repeatCount="2"
>
//通过以下代码注册该动画
private void createRotateByInflate(){
Animation animation = AnimationUtils.loadAnimation(this, R.anim.rotate);
mCircle.startAnimation(animation);
}创建方式二:(代码创建)
private void createRotateByCode() {
float pivotX = mCircle.getWidth() / 2.0f;
float pivotY = mCircle.getHeight() / 2.0f;
RotateAnimation animation = new RotateAnimation(0, 180, pivotX, pivotY);
animation.setDuration(2000);
animation.setRepeatMode(Animation.REVERSE);
animation.setRepeatCount(2);
mCircle.startAnimation(animation);
}缩放动画
创建方式一:
<?xml version="1.0" encoding="utf-8"?>
//通过以下代码注册该动画
private void createScaleByInflate(){
Animation animation = AnimationUtils.loadAnimation(this, R.anim.scale);
mCircle.startAnimation(animation);
}创建方式二:(代码创建)
private void createScaleByCode() {
//创建动画Animation.RELATIVE_TO_PARENT 以父容器为参照物
//Animation.RELATIVE_TO_SELF 以自己为参照物, 如果以父容器为参照物会导致控件移动
float pivotX = mCircle.getWidth() / 2.0f;
float pivotY = mCircle.getHeight() / 2.0f;
ScaleAnimation animation = new ScaleAnimation(0.1f, 1.0f, 0.1f, 1.0f, pivotX,pivotY);
animation.setDuration(2000);
animation.setRepeatMode(Animation.REVERSE);
animation.setRepeatCount(2);
mCircle.startAnimation(animation);
}渐变动画
创建方式一:
<?xml version="1.0" encoding="utf-8"?>
//通过以下代码注册该动画
private void createAlphaByInflate(){
Animation animation = AnimationUtils.loadAnimation(this, R.anim.alpha);
mCircle.startAnimation(animation);
}创建方式二:(代码创建)
private void createAlphaByCode() {
AlphaAnimation animation = new AlphaAnimation(0.1f, 1.0f);
animation.setDuration(2000);
animation.setRepeatMode(Animation.REVERSE);
animation.setRepeatCount(2);
mCircle.startAnimation(animation);
}以上的四种可以单独使用也可以结合起来使用,如果要结合起来使用的话,直接在anim文件夹下创建set集合,然后将需要结合的动画
放在一起即可
如下示例:
<?xml version="1.0" encoding="UTF-8"?>
基本的创建方式,以及基本属性都在这里了,至于如何实现一个具有美感的效果图,那就看个人的设计感了。
最后看看运行结果图

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。