




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】如何在Android中实现一个补间动画效果
这篇文章主要介绍了如何在Android中实现一个补间动画效果,此处通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考价值,需要的朋友可以参考下:补间动画原形态变成新形态时为了过渡变形过程,生成的动画就叫补间动画位移、旋转、缩放、透明位移:参数10指的是X的起点坐标,但不是指屏幕x坐标为10的位置,而是imageview的真实X+10参数150指的是X的终点坐标,它的值是imageview的真实X+150//创建为位移动画对象,设置动画的初始位置和结束位置TranslateAnimationta=newTranslateAnimation(10,150,20,140);1.
x坐标的起点位置,如果相对于自己,传0.5f,那么起点坐标就是真实X+0.5*iv宽度;2.
x坐标的终点位置,如果传入2,那么终点坐标就是真实X+2*iv的宽度;3.
y坐标的起点位置,如果传入0.5f,那么起点坐标就是真实Y+0.5*iv高度;4.
y坐标的终点位置,如果传入2,那么终点坐标就是真实Y+2*iv高度。
TranslateAnimationta=newTranslateAnimation(Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,2,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,2);动画播放相关的设置
//设置动画持续时间
ta.setDuration(2000);
//动画重复播放的次数
ta.setRepeatCount(1);
//动画重复播放的模式
ta.setRepeatMode(Animation.REVERSE);
//动画播放完毕后,组件停留在动画结束的位置上
ta.setFillAfter(true);
//播放动画
iv.startAnimation(ta);缩放:1.参数0.1f表示动画的起始宽度是真实宽度的0.1倍2.参数4表示动画的结束宽度是真实宽度的4倍3.缩放的中心点在iv左上角ScaleAnimationsa=newScaleAnimation(0.1f,4,0.1f,4);1.
参数0.1f和4意义与上面相同2.
改变缩放的中心点:传入的两个0.5f,类型都是相对于自己,这两个参数改变了缩放的中心点3.
中心点x坐标=真实X+0.5*iv宽度4.
中心点Y坐标=真实Y+0.5*iv高度
ScaleAnimationsa=newScaleAnimation(0.1f,4,0.1f,4,Animation.RELATIVETOSELF,0.5f,Animation.RELATIVETOSELF,0.5f);透明:0为完全透明,1为完全不透明AlphaAnimationaa=newAlphaAnimation(0,0.5f);旋转:1.
20表示动画开始时的iv的角度2.
360表示动画结束时iv的角度3.
默认旋转的圆心在iv左上角RotateAnimationra=newRotateAnimation(20,360);1.
20,360的意义和上面一样2.
指定圆心坐标,相对于自己,值传入0.5,那么圆心的x坐标:真实X+iv宽度*0.53.
圆心的Y坐标:真实Y+iv高度*0.5RotateAnimationra=newRotateAnimation(20,360,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);所有动画一起飞
//创建动画集合
AnimationSet
set
=
new
AnimationSet(false);
//往集合中添加动画
set.addAnimation(aa);
set.addAnimation(sa);
set.addAnimation(ra);
iv.startAnimation(set);效果如图:布局代码:
xmlns:tools="/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_alignParentBottom="true"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="translate"
android:text="平移"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="scale"
android:text="缩放"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="alpha"
android:text="透明"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="rotate"
android:text="旋转"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="fly"
android:text="一起飞"
/>
</LinearLayout>
</RelativeLayout>MainActivity.javaimport
android.app.Activity;
import
android.os.Bundle;
import
android.view.View;
import
android.view.animation.AlphaAnimation;
import
android.view.animation.Animation;
import
android.view.animation.AnimationSet;
import
android.view.animation.RotateAnimation;
import
android.view.animation.ScaleAnimation;
import
android.view.animation.TranslateAnimation;
import
android.widget.ImageView;
public
class
MainActivity
extends
Activity
{
private
ImageView
iv;
private
TranslateAnimation
ta;
private
ScaleAnimation
sa;
private
AlphaAnimation
aa;
private
RotateAnimation
ra;
@Override
protected
void
onCreate(Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv
=
(ImageView)
findViewById(R.id.iv);
}
//平移
public
void
translate(View
view)
{
//
ta
=
new
TranslateAnimation(10,
100,
20,
200);
ta
=
new
TranslateAnimation(Animation.RELATIVE_TO_SELF,
-1,
Animation.RELATIVE_TO_SELF,
2,
Animation.RELATIVE_TO_SELF,
-0.5f,
Animation.RELATIVE_TO_SELF,
1.5f);
//设置播放时间
ta.setDuration(2000);
//设置重复次数
ta.setRepeatCount(1);
//动画重复播放的模式
ta.setRepeatMode(Animation.REVERSE);
iv.startAnimation(ta);
}
//缩放
public
void
scale(View
view)
{
//
sa
=
new
ScaleAnimation(2,
4,
2,
4,
iv.getWidth()
/
2,
iv.getHeight()
/
2);
sa
=
new
ScaleAnimation(0.5f,
2,
0.1f,
3,
Animation.RELATIVE_TO_SELF,
0.5f,
Animation.RELATIVE_TO_SELF,
0.5f);
//设置播放时间
sa.setDuration(2000);
//设置重复次数
sa.setRepeatCount(1);
//动画重复播放的模式
sa.setRepeatMode(Animation.ABSOLUTE);
//动画播放完毕后,组件停留在动画结束的位置上
sa.setFillAfter(true);
iv.startAnimation(sa);
}
//透明
public
void
alpha(View
view)
{
aa
=
new
AlphaAnimation(0,
1);
//设置播放时间
aa.setDuration(2000);
//设置重复次数
aa.setRepeatCount(1);
//动画重复播放的模式
aa.setRepeatMode(Animation.REVERSE);
//动画播放完毕后,组件停留在动画结束的位置上
//
aa.setFillAfter(true);
iv.startAnimation(aa);
}
//旋转
public
void
rotate(View
view)
{
ra
=
new
RotateAnimation(0,
720,
Animation.RELATIVE_TO_SELF,
0.5f,
Animation.RELATIVE_TO_SELF,
0.5f);
//设置播放时间
ra.setDuration(2000);
//设置重复次数
ra.setRepeatCount(1);
//动画重复播放的模式
ra.setRepeatMode(Animation.REVERSE);
//动画播放完毕后,组件停留在动画结束的位
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- C1跨学科学习活动设计与文本阅读心得体会
- 六年级应急安全演练教学计划
- 倍速生活视角下的2025高三作文范文
- 化工厂工程质量目标及安全措施
- 小学一年级语文教学评价心得体会
- 中学2024-2025学年度第一学期实验教学计划
- 以形助数以数解形:数形结合法在高中数学教学中的深度融合与应用
- 以实践为翼:基于核心素养的高中地理野外考察-孝顺中学的探索与启示
- 教科版科学四年级上册教学能力提升计划
- 2024-2025年春季第二学期小学德育科技教育计划
- GB/T 4074.8-2009绕组线试验方法第8部分:测定漆包绕组线温度指数的试验方法快速法
- GB/T 28575-2020YE3系列(IP55)三相异步电动机技术条件(机座号63~355)
- 国际公法学 马工程课件 4 第四章
- 青海省西宁市《职业能力测试》事业单位国考真题
- 沟通中的提问技巧课件
- 2023年浙江黄龙体育发展有限公司招聘笔试模拟试题及答案解析
- 外科学骨折概论课件
- 阿片类药物镇痛机制课件
- 产品成本核算流程图
- 母线槽安装检验批质量验收记录
- 生物吸附课件
评论
0/150
提交评论