Animation动画效果的实现
提供了三种动画效果:逐帧动画(frame-by-frame animation),这种动画和GIF一样,一帧一帧的显示来组成动画效果;布局动画(layout animation),这种动画用来设置layout内的所有UI控件;控件动画(view animation),这种是应用到具体某个view上的动画。
- <animation-list
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:oneshot="false">
- <item android:drawable="@drawable/qq001" android:duration="80"/>
- <item android:drawable="@drawable/qq002" android:duration="80"/>
- <item android:drawable="@drawable/qq003" android:duration="80"/>
- <item android:drawable="@drawable/qq004" android:duration="80"/>
- <item android:drawable="@drawable/qq005" android:duration="80"/>
- <item android:drawable="@drawable/qq006" android:duration="80"/>
- <item android:drawable="@drawable/qq007" android:duration="80"/>
- <item android:drawable="@drawable/qq008" android:duration="80"/>
- </animation-list>
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <ImageView
- android:id="@+id/animation_view"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:src="@drawable/qq_animation" />
- <Button
- android:id="@+id/animation_btn"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/start_animation" />
- <Button
- android:id="@+id/one_shot_btn"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/play_once" />
- </LinearLayout>
- public class AnimActivity extends Activity {
- /** Called when the activity is first created. */
- AnimationDrawable mAd;
- Button mPlayBtn;
- Button mOneShotBtn;
- boolean mIsOneShot;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- ImageView iv = (ImageView) findViewById(R.id.animation_view);
- mAd = (AnimationDrawable) iv.getDrawable();
- mPlayBtn = (Button) findViewById(R.id.animation_btn);
- mPlayBtn.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View view) {
- startAnimation();
- }
- });
- mOneShotBtn = (Button) findViewById(R.id.one_shot_btn);
- mOneShotBtn.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View view) {
- if (mIsOneShot) {
- mOneShotBtn.setText("Play Once");
- } else {
- mOneShotBtn.setText("Play Repeatly");
- }
- mAd.setOneShot(!mIsOneShot);
- mIsOneShot = !mIsOneShot;
- }
- });
- }
- /**
- * 通过AnimationDrawable的start函数播放动画,
- * stop函数停止动画播放,
- * isRunning来判断动画是否正在播放。
- */
- public void startAnimation() {
- if (mAd.isRunning()) {
- mAd.stop();
- } else {
- mAd.stop();
- mAd.start();
- }
- }
- }
- l AlphaAnimation:实现alpha渐变,可以使界面逐渐消失或者逐渐显现
- l TranslateAnimation:实现位置移动渐变,需要指定移动的开始和结束坐标
- l ScaleAnimation: 实现缩放渐变,可以指定缩放的参考点
- l RotateAnimation:实现旋转渐变,可以指定旋转的参考点,默认值为(0,0)左上角。
- l AnimationSet: 代表上面的渐变组合
- <set xmlns:android="http://schemas.android.com/apk/res/android"
- android:interpolator="@android:anim/decelerate_interpolator">
- <alpha
- android:fromAlpha="0.0"
- android:toAlpha="1.0"
- android:duration="1000" />
- </set>
- <alpha
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:interpolator="@android:anim/accelerate_interpolator"
- android:fromAlpha="0.0"
- android:toAlpha="1.0"
- android:duration="1000" />
- <scale
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:interpolator="@android:anim/accelerate_interpolator"
- android:fromXScale="1"
- android:toXScale="1"
- android:fromYScale="0.1"
- android:toYScale="1.0"
- android:duration="500"
- android:pivotX="50%"
- android:pivotY="50%"
- android:startOffset="100" />
- <rotate
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:interpolator="@android:anim/accelerate_interpolator"
- android:fromDegrees="0.0"
- android:toDegrees="360"
- android:pivotX="50%"
- android:pivotY="50%"
- android:duration="500" />
- <translate
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:interpolator="@android:anim/accelerate_interpolator"
- android:fromYDelta="-100%"
- android:toYDelta="0"
- android:duration="500" />
- <set xmlns:android="http://schemas.android.com/apk/res/android"
- android:interpolator="@android:anim/decelerate_interpolator">
- <translate
- android:fromYDelta="-100%"
- android:toYDelta="0"
- android:duration="500" />
- <alpha
- android:fromAlpha="0.0"
- android:toAlpha="1.0"
- android:duration="500" />
- </set>
- LayoutAnimationController,该文件同样位于res/anim/目录下,下面是一个示例(res\anim\layout_anim_ctrl.xml):
- <layoutAnimation
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:delay="30%"
- android:animationOrder="reverse"
- android:animation="@anim/translate_alpha_anim" />
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <ListView
- android:id="@+id/list"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:persistentDrawingCache="animation|scrolling"
- android:layoutAnimation="@anim/layout_anim_ctrl" />
- </LinearLayout>
- public class ListActivity extends Activity {
- String[] mListItems =
- { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5"};
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.list);
- ArrayAdapter<String> listItemAdapter =
- new ArrayAdapter<String>(this,
- android.R.layout.simple_list_item_1, mListItems);
- ListView lv = (ListView) this.findViewById(R.id.list);
- lv.setAdapter(listItemAdapter);
- }
- }
- class ViewAnimation extends Animation {
- public ViewAnimation() {
- }
- @Override
- public void initialize(int width, int height, int parentWidth, int parentHeight) {
- super.initialize(width, height, parentWidth, parentHeight);
- setDuration(2500);
- setFillAfter(true);
- setInterpolator(new LinearInterpolator());
- }
- @Override
- protected void applyTransformation(float interpolatedTime,
- Transformation t) {
- final Matrix matrix = t.getMatrix();
- matrix.setScale(interpolatedTime, interpolatedTime);
- }
- }
- public class ViewAnimActivity extends Activity {
- Button mPlayBtn;
- ImageView mAnimImage;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.view_anim_layout);
- mAnimImage = (ImageView) this.findViewById(R.id.anim_image);
- mPlayBtn = (Button) findViewById(R.id.play_btn);
- mPlayBtn.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View view) {
- mAnimImage.startAnimation(new ViewAnimation());
- }
- });
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <Button
- android:id="@+id/play_btn"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="Start Animation"
- />
- <ImageView
- android:id="@+id/anim_image"
- android:persistentDrawingCache="animation|scrolling"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:src="@drawable/ophone"
- />
- </LinearLayout>
- class ViewAnimation extends Animation {
- int mCenterX;//记录View的中间坐标
- int mCenterY;
- public ViewAnimation() {
- }
- @Override
- public void initialize(int width, int height, int parentWidth, int parentHeight) {
- super.initialize(width, height, parentWidth, parentHeight);
- //初始化中间坐标值
- mCenterX = width/2;
- mCenterY = height/2;
- setDuration(2500);
- setFillAfter(true);
- setInterpolator(new LinearInterpolator());
- }
- @Override
- protected void applyTransformation(float interpolatedTime,
- Transformation t) {
- final Matrix matrix = t.getMatrix();
- matrix.setScale(interpolatedTime, interpolatedTime);
- //通过坐标变换,把参考点(0,0)移动到View中间
- matrix.preTranslate(-mCenterX, -mCenterY);
- //动画完成后再移回来
- matrix.postTranslate(mCenterX, mCenterY);
- }
- }
- Reset():重置该矩阵
- setScale():设置矩阵缩放
- setTranslate():设置矩阵移动
- setRotate():设置矩阵旋转
- setSkew(): 使矩阵变形(扭曲)
- class ViewAnimation extends Animation {
- int mCenterX;//记录View的中间坐标
- int mCenterY;
- Camera camera = new Camera();
- public ViewAnimation() {
- }
- @Override
- public void initialize(int width, int height, int parentWidth,
- int parentHeight) {
- super.initialize(width, height, parentWidth, parentHeight);
- //初始化中间坐标值
- mCenterX = width/2;
- mCenterY = height/2;
- setDuration(2500);
- setFillAfter(true);
- setInterpolator(new LinearInterpolator());
- }
- @Override
- protected void applyTransformation(float interpolatedTime,
- Transformation t) {
- // final Matrix matrix = t.getMatrix();
- // matrix.setScale(interpolatedTime, interpolatedTime);
- // //通过坐标变换,把参考点(0,0)移动到View中间
- // matrix.preTranslate(-mCenterX, -mCenterY);
- // //动画完成后再移回来
- // matrix.postTranslate(mCenterX, mCenterY);
- final Matrix matrix = t.getMatrix();
- camera.save();
- camera.translate(0.0f, 0.0f, (1300 - 1300.0f * interpolatedTime));
- camera.rotateY(360 * interpolatedTime);
- camera.getMatrix(matrix);
- matrix.preTranslate(-mCenterX, -mCenterY);
- matrix.postTranslate(mCenterX, mCenterY);
- camera.restore();
- }
- }
Animation动画效果的实现相关推荐
- android中设置Animation 动画效果
在 Android 中, Animation 动画效果的实现可以通过两种方式进行实现,一种是 tweened animation 渐变动画,另一种是 frame by frame animation ...
- animation动画效果 1002 css3
animation动画效果 1002 css3 什么是动画 使用步骤 如何定义动画 @keyframes 动画名称{from{属性:值}percentage{属性:值}to{属性:值} } 或者 @k ...
- android编程xml动画,Android中xml设置Animation动画效果详解
在Android中,Animation动画效果的实现可以通过两种方式进行实现,一种是tweened animation渐变动画,另一种是frame by frame animation画面转换动画. ...
- CSS animation动画效果实现精灵图、雪碧图动画,多动画应用
CSS animation动画效果实现精灵图.雪碧图动画,多动画应用 素材下载 分析图片动画过程 图片可以分成4*6=24 小图片也就是24帧 方法1 可以把每一行成一个动画,所以可以拆分4个动画 方 ...
- Android Animation动画效果简介
AlphaAnimation 淡入淡出动画 <alpha>A fade-in or fade-out animation. Represents an AlphaAnimation. a ...
- Android动画开发——Animation动画效果
动画类型 Android的animation由四种类型组成 XML中 alpha 渐变透明度动画效果 scale 渐变尺寸伸缩动画效果 translate 画面转换位置移动动画效果 rotate 画面 ...
- android+动画悬浮窗口,悬浮窗能实现自定Animation动画效果吗?
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 我的软件运行后会直接去调用Service来显示悬浮的Button 后来在学习Dialog时认识了Animation,显示.退出对话窗时都能自定动画 就在想 ...
- 在GridView的行绑定中应用Animation动画效果
今天做一个功能需要实现绑定GridView的行Button点击就实现Animation的动画效果,终于被我解决了!主要代码如下: GridVIew行绑定: <ItemTemplate> ...
- android 属性动画伸缩,Android动画开发——Animation动画效果详解
在Android中,分别可以在xml中定义Animation,也可以在程序代码中定义. 动画类型Android的animation由四种类型组成 XML中alpha渐变透明度动画效果 scale渐变尺 ...
- ios animation 动画效果实现
1.过渡动画 CATransition CATransition *animation = [CATransition animation];[animation setDuration:1.0];[ ...
最新文章
- C# 将DataTable数据源转换成实体类
- wxruby框架例子1
- mongodb数据合并设计_「时间序列数据」和MongoDB(二)-模式设计最佳实践
- Css中图片局部放大,将图片中局部放大效果
- Qt Creator指定编辑器设置
- 【蓝桥官网试题 - 算法提高】change(思维)
- 清华大学计算机音乐,清华特奖候选人丨胡昌然:关于机器,音乐和灵魂
- php毕业论文结论,科学网—如何写好学位论文的摘要和结论 - 王东升的博文
- python将密文解密为明文_三分钟教你学会如何将密文解码成明文
- 将一个链表分为奇偶两个链表
- Anbox之arm平台lxc调试(十一)
- 理解RTF和RTX指标
- 使用Vivado软件进行硬件调试
- 高等数学:第一章 函数与极限(2)数列极限
- mysql5.045_Microsoft SQL Server数据库各版本下载地址集合
- 9个可视化图表在线制作工具,总有一款适合你
- Mac下载Bilibili视频
- C++11 参数绑定-bind函数用法
- localStorage的图片缓存
- CAD学习笔记(一)