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

android 动画效果

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

商务礼仪论文,陈思思的老公是谁,荒村红杏免费阅读

android的动画效果可以通过在代码中animation实现,可以实现的效果有平移,旋转,透明,缩放,同样的效果可以使用anim文件夹下的xml实现同样的动画效果,还可以在drawable文件使用多张图片形成动画。

 

1.透明度动画

代码

//1.创建透明度动画对象,数值越小越透明
alphaanimation alphaanimation = new alphaanimation(1, 0.1f);
//设置动画的持续时间
alphaanimation.setduration(3000);
//设置是否保留最终状态
alphaanimation.setfillafter(true);
//设置重复次数,填-1无限循环
alphaanimation.setrepeatcount(1);
//设置动画的重复模式,默认是restart,reverse是反方向执行
alphaanimation.setrepeatmode(animation.reverse);
//通过控件启动动画
img.startanimation(alphaanimation);

xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="3000"
android:repeatmode="reverse"
android:fillafter="true"
>
<!-- 透明度动画 -->
<!-- repeatmode 设置动画的重复模式,默认是restart,reverse是反方向执行 -->
<!-- fillafter 保持最终形态-->
<!-- repeatcount 设置动画重复次数-->
<!-- android:fromalpha="1" android:toalpha="0.2" 从完全不透明到透明-->
<alpha android:fromalpha="1" android:toalpha="0.2"
android:repeatcount="0"/>
</set>


2.缩放动画

代码

scaleanimation scaleanimation = new scaleanimation(0.5f, 2, 0.5f, 2, animation.relative_to_self, 0.5f, animation.relative_to_self, 0.5f);
scaleanimation.setduration(3000);
scaleanimation.setfillafter(true);
img.startanimation(scaleanimation);

xml

 

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator= "@android:anim/decelerate_interpolator"
android:duration="10000"
android:fromxscale="0.0"
android:fromyscale="0.0"
android:pivotx="50%"
android:pivoty="50%"
android:repeatcount="1"
android:repeatmode="reverse"
android:startoffset="0"
android:toxscale="1.5"
android:toyscale="1.5" />

3.旋转动画

代码

//相对自己的左上角旋转,正数代表顺时针,负数逆时针
rotateanimation rotateanimation = new rotateanimation(0,-180);
//相对(200,300)点旋转
//相对于中心旋转
//rotateanimation rotateanimation = new rotateanimation(0,-180,animation.relative_to_self,0.5f,animation.relative_to_self,0.5f);
//rotateanimation rotateanimation = new rotateanimation(0,-180,200,300);
rotateanimation.setduration(3000);
img.startanimation(rotateanimation);

xml

 

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="10000"
android:fromdegrees="300"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:pivotx="10%"
android:pivoty="100%"
android:todegrees="-360" />
</set>

4.平移动画

代码

int screenwidth = getresources().getdisplaymetrics().widthpixels;
int imgewidth = img.getwidth();
//translateanimation translateanimation = new translateanimation(0,screenwidth-imgewidth,0,0);
translateanimation translateanimation = new translateanimation(0,200,0,0);
translateanimation.setduration(3000);
translateanimation.setfillafter(true);
img.startanimation(translateanimation);

xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillafter="true"
>
<translate
android:duration="1000"
android:fromxdelta="0"
android:fromydelta="0"
android:toxdelta="200"
android:toydelta="0" />
</set>

执行xml动画的代码

animation anim = animationutils.loadanimation(this, r.anim.rotate);
img.setanimation(anim);
img.startanimation(nim);

 动画还有可以将图片循环播放的一种动画,使用animation_list来选择多张图片animationdrawable类来处理

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false" >
<item
android:drawable="@drawable/img1"
android:duration="200"/>
<item
android:drawable="@drawable/img2"
android:duration="200"/>
<item
android:drawable="@drawable/img3"
android:duration="200"/>

</animation-list>

开始动画和停止动画

img.setimageresource(r.drawable.animation_img);
//给动画资源赋值
animationdrawable = (animationdrawable) img.getdrawable();
animationdrawable.start();//开始
animationdrawable.stop();//结束

实例下载网址:https://github.com/tempest1/amin 

 

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

相关文章:

验证码:
移动技术网