Android动画之补间动画
2015-11-02 13:37
417 查看
Android动画之补间动画
概述
原形态变成新形态时为了过渡变形过程,生成的动画就叫补间动画,包括平移,缩放,透明,旋转Demo
MainActivity.java
import android.os.Bundle; import android.app.Activity; import android.view.Menu; 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 RotateAnimation ra; private AlphaAnimation aa; private ScaleAnimation sa; private TranslateAnimation ta; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iv = (ImageView) findViewById(R.id.iv); } /* 平移 */ public void translate(View v){ /** * 平移动画 * new TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta); * fromXDelta : 动画的起始x坐标,注意不是屏幕的x坐标,而是相对于被设置动画控件的x坐标为原点 * toXDelta : 动画的结束x坐标 * fromYDelta : 动画的起始y坐标 * toYDelta : 动画的结束y坐标 */ // ta = new TranslateAnimation(10, 100, 20, 200); /** * 平移动画 * int fromXType, float fromXValue, * type Animation.RELATIVE_TO_SELF * 相对于自己 依旧是相对于被设置动画控件本身而言.如果传入2,则 fromX = 控件x + 2 * (控件的width) * int toXType, float toXValue, * int fromYType, float fromYValue, * int toYType, float toYValue */ ta = new TranslateAnimation(Animation.RELATIVE_TO_SELF, -1, Animation.RELATIVE_TO_SELF, 1, Animation.RELATIVE_TO_SELF, -1f, Animation.RELATIVE_TO_SELF, 1f); // 设置播放时间 ta.setDuration(2000); // 设置重复次数 -1:无数次 ta.setRepeatCount(-1); // 设置重复的模式 ta.setRepeatMode(Animation.RESTART); // 开始动画 iv.startAnimation(ta); } /* 缩放 */ public void scale(View v){ /** * 参数描述与下面一致,默认的缩放中心点在view左上角 */ // sa = new ScaleAnimation(1, 2, 1, 2); /** * 参数说明: *float fromX : 参数1表示动画的起始宽度是真实宽度的1倍 *float toX : 参数2表示动画的结束宽度是真实宽度的2倍 *float fromY : 参数1表示动画的结束高度是真实高度的1倍 *float toY : 参数2表示动画的结束高度是真实高度的2倍 *float pivotX : 缩放的中心点x *float pivotY : 缩 cb18 放的中心点y */ sa = new ScaleAnimation(1, 2, 1, 2, iv.getWidth() / 2, iv.getHeight() / 2); /** * 前四个参数与上面描述一致 * Animation.RELATIVE_TO_SELF 表示中心点的x,y都是相对于本身宽高度的0.5倍 */ // sa = new ScaleAnimation(1, 2, 1, 2, Animation.RELATIVE_TO_SELF, 0.5f, // Animation.RELATIVE_TO_SELF, 0.5f); sa.setDuration(2000); // 填充动画的结束位置 sa.setRepeatCount(1); sa.setRepeatMode(Animation.REVERSE); sa.setFillAfter(true); iv.startAnimation(sa); } /* 透明 */ public void alpha(View v){ /** * float fromAlpha : 起始的透明度 * float toAlpha : 结束的透明度 */ aa = new AlphaAnimation(1, 0); // 0表示透明,1表示不透明 从透明到不透明 aa.setDuration(2000); aa.setRepeatCount(1); iv.startAnimation(aa); } /* 旋转 */ public void rotate(View v){ /** * float fromDegrees : 开始旋转的角度 * float toDegrees :结束旋转的角度 * 默认旋转的圆心在view的左上角 */ // ra = new RotateAnimation(0, 180); /** * 前面两个参数与上述描述一致 * 后面两个参数用来指定圆心 * Animation.RELATIVE_TO_SELF 相对于自己,中心点的x,y分别是自己宽度和高度的0.5倍 */ ra = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); ra.setDuration(2000); ra.setRepeatCount(-1); ra.setRepeatMode(Animation.RESTART); iv.startAnimation(ra); } public void fly(View v){ // AnimationSet 动画集合,放入多个动画,同时执行 AnimationSet set = new AnimationSet(false); set.addAnimation(ta); set.addAnimation(sa); set.addAnimation(ra); set.addAnimation(aa); iv.startAnimation(set); } }
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="平移" android:onClick="translate" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="缩放" android:onClick="scale" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="透明" android:onClick="alpha" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="旋转" android:onClick="rotate" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="集合" android:onClick="fly" /> </LinearLayout> <ImageView android:id="@+id/iv" android:layout_width="60dp" android:layout_height="60dp" android:src="@drawable/f005" android:layout_centerInParent="true" /> </RelativeLayout>
代码效果展示
平移
缩放
透明
旋转
动画同时播放
相关文章推荐
- Android开发时图片Bitmap序列化问题(How to fix a java.io.NotSerializableException: android.graphics.Bitmap)
- Android:onNewIntent()触发机制及注意事项
- android短信备份报 java.lang.IllegalArgumentException错误
- 手机通知功能Notification
- Android PackageManagerService流程详细分析(三)之PackageHandler
- Android异步消息机制Handler详解,源码剖析(API 23)
- 为AndroidStudio安装jd-intellij,反编译任意.jar .class
- Android studio 多渠道打包(超简洁版)
- Software Design Patterns on Android
- 异步加载AsyncTask小谈+实例
- Caused by: java.lang.ClassNotFoundException[android的终极解决错误]
- android编程实现系统图片剪裁的方法
- 【Android】【源码】如何阅读Android系统源码
- 【Android】【源码】如何阅读Android系统源码
- 【Android】【源码】如何阅读Android系统源码
- 【Android】【源码】如何阅读Android系统源码
- 【Android】【源码】如何阅读Android系统源码
- 【Android】【源码】如何阅读Android系统源码
- 【Android】【源码】如何阅读Android系统源码
- 【Android】【源码】如何阅读Android系统源码