当前位置: 移动技术网 > IT编程>移动开发>Android > Android仿网易游戏的精美开场动画+动画基础详解

Android仿网易游戏的精美开场动画+动画基础详解

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

贵州都匀毛尖,千金归来腾讯视频,白成贤

可以看到现在不少app都加入了开场动画,效果还是挺不错的,今天我就在这分享一下仿网易游戏的开场动画制作,同时我也在文章中加入了android动画基础的详解,接下来就进入主题吧。

首先是布局文件:



 

很简单,只是加入了一个imageview由于放置开场图。

mainactivity代码:

public class mainactivity extends appcompatactivity {
 private imageview imageview;
 
 @override
 protected void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_main);
  imageview=(imageview)findviewbyid(r.id.image);
  imageview.setimageresource(r.drawable.netease);
  alphaanimation animation = new alphaanimation(0, 1);
  animation.setduration(3000);
  imageview.startanimation(animation);
  animation.setanimationlistener(new animation.animationlistener() {
@override
public void onanimationstart(animation animation) {

  }

@override
public void onanimationend(animation animation) {
alphaanimation alphaanimation = new alphaanimation(1, 0);
alphaanimation.setduration(1500);
imageview.startanimation(animation);
alphaanimation.setfillafter(true);
alphaanimation.setanimationlistener(new animation.animationlistener() {
 @override
 public void onanimationstart(animation animation) {

 }

 @override
 public void onanimationend(animation animation) {
  intent intent=new intent(mainactivity.this,indexactivity.class);
  startactivity(intent);
  mainactivity.this.finish();
 }

 @override
 public void onanimationrepeat(animation animation) {
 }
});
  }

@override
public void onanimationrepeat(animation animation) {
  }
 });
}
 }

\

代码实现了仿网易游戏开场图渐变的效果,实际上这里只用到了android的alphaanimation透明度动画。除了透明度动画外,android系统还包括rotateanimation旋转动画,translateanimation移位动画,scaleanimation缩放动画,和animationset 动画效果集合,当然还可以实现自定义动画。

先介绍一下各类动画中包含的方法,实际上动画效果是可以在xml中实现的,这里由于篇幅有限,只介绍java方式。

xml属性 java方法 解释
android:detachwallpaper setdetachwallpaper(boolean) 是否在壁纸上运行
android:duration setduration(long) 动画持续时间,毫秒为单位
android:fillafter setfillafter(boolean) 控件动画结束时是否保持动画最后的状态
android:fillbefore setfillbefore(boolean) 控件动画结束时是否还原到开始动画前的状态
android:fillenabled setfillenabled(boolean) 与android:fillbefore效果相同
android:interpolator setinterpolator(interpolator) 设定插值器(指定的动画效果,譬如回弹等)
android:repeatcount setrepeatcount(int) 重复次数
android:repeatmode setrepeatmode(int) 重复类型有两个值,reverse表示倒序回放,restart表示从头播放
android:startoffset setstartoffset(long) 调用start函数之后等待开始运行的时间,单位为毫秒
android:zadjustment setzadjustment(int) 表示被设置动画的内容运行时在z轴上的位置(top/bottom/normal),默认为normal

接下来介绍一下各种动画效果及实现方式:

1.透明度动画alphaanimation:

java中的基本使用方法:

 alphaanimation animation = new alphaanimation(0, 1);
  animation.setduration(3000);
  imageview.startanimation(animation);

其中创建alphaanimation对象时传入了参数0,1,代表透明度从0(完全透明)到1(不透明)变化。

在我的代码中用到了.setanimationlistener方法为动画添加监听事件,使用时必须重写onanimationstart,onanimationend,onanimationrepeat这三个方法,分别代表动画开始,动画结束,动画重放是的逻辑功能。

2.旋转动画rotateanimation

rotateanimation rotate = new rotateanimation(0, 360,
animation.relative_to_self,0.5f,animation.relative_to_self, 0.5f);
rotate.setduration(1000);
rotatebutton.startanimation(rotate);

构造函数中的四个变量分别代表从角度0转到360度,animation.relative_to _self意为旋转中心类型,意思是相对自身,偏移量0.5f,意为50%,在这里即表示让组件以自身中心点为中心轴旋转。

3.平移动画translateanimation

translateanimation translate = new translateanimation(animation.relative_to_self, 0, animation.relative_to_self, 0.5f, animation.relative_to_self, 0, animation.relative_to_self, 0.5f);
 translate.setduration(1000);
 translatebutton.startanimation(translate);

translateanimation的使用方法与rotateanimation 类似,重点是指定相对哪个组件来移动

4.缩放动画scaleanimation?

scaleanimation scale = new scaleanimation(1, 0, 1, 2, animation.relative_to_self, 0.5f, animation.relative_to_self, 0.5f);
 scale.setduration(1000);
 scalebutton.startanimation(scale);

构造函数中各个变量分别代表组件在x轴从一缩小到零,y轴从一放大到二,缩放中心为相对自身的中心点

5.动画集合animationset

 animationset animationset = new animationset(true);
 animationset.setduration(3000);
 alphaanimation alphaanimation = new alphaanimation(0, 1);
 rotateanimation rotateanimation = new 
 rotateanimation(0, 360, animation.relative_to_self, 0.5f, animation.relative_to_self, 0.5f);
 animationset.addanimation(alphaanimation);
 animationset.addanimation(rotateanimation);
 imageview.startanimation(animationset);

这个很好理解吧?动画集合也就是同时实现多种动画效果。相信看到这里小伙伴们一定能自把己写出一些基础的动画效果了,顺便把我的地址也发布一下,里面不仅有开场动画,还有开场导航页的功能,希望对你们有所帮助。

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

相关文章:

验证码:
移动技术网