黄岩模架招聘信息:Animation动画效果的实现
来源:百度文库 编辑:九乡新闻网 时间:2024/04/29 17:52:15
提供了三种动画效果:逐帧动画(frame-by-frame animation),这种动画和GIF一样,一帧一帧的显示来组成动画效果;布局动画(layout animation),这种动画用来设置layout内的所有UI控件;控件动画(view animation),这种是应用到具体某个view上的动画。
在这三种动画实现中逐帧动画是最简单的,而控件动画是有点复杂的,要涉及到线性代数中的矩阵运算,下面就由易到难逐个介绍,先来看看逐帧动画如何实现。 逐帧动画逐帧动画是通过OPhone中的android.graphics.drawable.AnimationDrawable类来实现的,在该类中保存了帧序列以及显示的时间,为了简化动画的创建OPhone提供了一种通过XML来创建逐帧动画的方式,这样把动画的创建和代码分来以后如果需要修改动画内容,只需要修改资源文件就可以了不用修改代码,简化开发维护工作。在res/drawable/文件夹下创建一个XML文件,下面是一个示例文件(res\drawable\qq_animation.xml):view plaincopy to clipboardprint?- xmlns:android="http://schemas.android.com/apk/res/android"
- android:oneshot="false">
- "@drawable/qq001" android:duration="80"/>
- "@drawable/qq002" android:duration="80"/>
- "@drawable/qq003" android:duration="80"/>
- "@drawable/qq004" android:duration="80"/>
- "@drawable/qq005" android:duration="80"/>
- "@drawable/qq006" android:duration="80"/>
- "@drawable/qq007" android:duration="80"/>
- "@drawable/qq008" android:duration="80"/>
"http://schemas.android.com/apk/res/android" android:oneshot="false"> - "@drawable/qq001" android:duration="80"/>
- "@drawable/qq002" android:duration="80"/>
- "@drawable/qq003" android:duration="80"/>
- "@drawable/qq004" android:duration="80"/>
- "@drawable/qq005" android:duration="80"/>
- "@drawable/qq006" android:duration="80"/>
- "@drawable/qq007" android:duration="80"/>
- "@drawable/qq008" android:duration="80"/>
- "@drawable/qq001" android:duration="80"/>
在上面的定义中通过animation-list来指定这是个AnimationDrawable动画定义,里面的item来指定每帧图片和显示时间(单位为毫秒),帧显示的顺序就是item定义的顺序。如果android:oneshot设置为true表明该动画只播放一次,否则该动画会循环播放。这些设置也可以通过AnimationDrawable提供的函数来设置。动画中引用的文件为QQ表情文件,包含在示例项目代码中。然后在layout中定义一个ImageView来显示上面定义的AnimationDrawable,layout代码如下(res\layout\main.xml): view plaincopy to clipboardprint?
- "1.0" encoding="utf-8"?>
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- android:id="@+id/animation_view"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:src="@drawable/qq_animation" />
- "1.0" encoding="utf-8"?>
"http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> "@+id/animation_view" android:layout_width="fill_parent" android:layout_height="wrap_content" android:src="@drawable/qq_animation" /> "@+id/animation_btn" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/start_animation" /> "@+id/one_shot_btn" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/play_once" />
注意这里的ImageView 通过android:src="@drawable/qq_animation"引用了前面定义的AnimationDrawable,下面是两个按钮用来控制播放动画和设置AnimationDrawable的oneshot属性。
下面就是控制动画播放的类代码(src\org\goodev\animation\AnimActivity.java):
view plaincopy to clipboardprint?
- 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();
- }
- }
- }
- 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(); } } }
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(); } } }布局动画介绍 布局动画和逐帧动画是由本质的不同的,逐帧动画是一帧帧图片组成的,而布局动画是渐变动画,OPhone通过改变UI的属性(大小、位置、透明度等)来实现动画效果。 在OPhone显示系统中,每个view都对应一个矩阵来控制该view显示的位置,通过不同的方式来改变该控制矩阵就可以实现动画效果,例如旋转、移动、缩放等。 不同的矩阵变换有不同的类来实现,android.view.animation.Animation类代表所有动画变换的基类,目前在OPhone系统中有如下五个实现(都位于android.view.animation包中):
- l AlphaAnimation:实现alpha渐变,可以使界面逐渐消失或者逐渐显现
- l TranslateAnimation:实现位置移动渐变,需要指定移动的开始和结束坐标
- l ScaleAnimation: 实现缩放渐变,可以指定缩放的参考点
- l RotateAnimation:实现旋转渐变,可以指定旋转的参考点,默认值为(0,0)左上角。
- l AnimationSet: 代表上面的渐变组合
"http://schemas.android.com/apk/res/android" - android:interpolator="@android:anim/decelerate_interpolator">
- android:fromAlpha="0.0"
- android:toAlpha="1.0"
- android:duration="1000" />
"http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/decelerate_interpolator"> "0.0" android:toAlpha="1.0" android:duration="1000" />
如果只定义一种渐变效果,则可以去除set元素,如下:
view plaincopy to clipboardprint?
- 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" />
"http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator" android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="1000" />
上面定义的alpha从0(透明)到1(不透明)渐变,渐变时间为1000毫秒。 view plaincopy to clipboardprint?
- 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" />
"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" />
上面是一个缩放渐变的定义,from... 和 to... 分别定义缩放渐变的开始和结束的缩放倍数,上面定义X轴都为1不缩放,而Y轴从0.1到1逐渐放大(开始高度为正常大小的0.1然后逐渐放大到正常大小)。缩放持续的时间为500毫秒,缩放的中心点(通过pivotX,pivotY定义)在控件的中间位置。startOffset指定了在缩放开始前等待的时间。view plaincopy to clipboardprint?
- 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" />
"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" />
上面定义了旋转变换,从0度变化到360度(旋转一周),时间为500毫秒,变换的中心点位控件的中心位置。 view plaincopy to clipboardprint?
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:interpolator="@android:anim/accelerate_interpolator"
- android:fromYDelta="-100%"
- android:toYDelta="0"
- android:duration="500" />
"http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator" android:fromYDelta="-100%" android:toYDelta="0" android:duration="500" />
上面定义了位置变换,实现一种下落的效果。 把上面不同的定义放到set中就可以实现不同的组合动画效果了,下面的示例实现了控件在下落的过程中逐渐显示的效果(res\anim\translate_alpha_anim.xml): view plaincopy to clipboardprint?
"http://schemas.android.com/apk/res/android" - android:interpolator="@android:anim/decelerate_interpolator">
- android:fromYDelta="-100%"
- android:toYDelta="0"
- android:duration="500" />
- android:fromAlpha="0.0"
- android:toAlpha="1.0"
- android:duration="500" />
"http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/decelerate_interpolator"> "-100%" android:toYDelta="0" android:duration="500" /> "0.0" android:toAlpha="1.0" android:duration="500" />
要把上面定义的动画效果应用的layout中,就要使用另外一个类:android.view.animation.LayoutAnimationController。该类把指定的效果应用到layout中的每个控件上去,使用layoutAnimation元素在xml文件中定义 view plaincopy to clipboardprint?
- LayoutAnimationController,该文件同样位于res/anim/目录下,下面是一个示例(res\anim\layout_anim_ctrl.xml):
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:delay="30%"
- android:animationOrder="reverse"
- android:animation="@anim/translate_alpha_anim" />
- LayoutAnimationController,该文件同样位于res/anim/目录下,下面是一个示例(res\anim\layout_anim_ctrl.xml):
"http://schemas.android.com/apk/res/android" android:delay="30%" android:animationOrder="reverse" android:animation="@anim/translate_alpha_anim" />
LayoutAnimationController,该文件同样位于res/anim/目录下,下面是一个示例(res\anim\layout_anim_ctrl.xml):上面通过animation指定了使用哪个动画(通过修改该值可以测试不同的动画效果),animationOrder指定了通过逆序的方式应用动画(在垂直的LinearLayout中就是从下往上逐个控件应用),delay指定了layout中的每个控动画的延时时间为动画持续总时间的30%。 定义好LayoutAnimationController后就可以在Layout中使用了,这里使用一个ListView做演示,布局代码如下:view plaincopy to clipboardprint?
- "1.0" encoding="utf-8"?>
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- android:id="@+id/list"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:persistentDrawingCache="animation|scrolling"
- android:layoutAnimation="@anim/layout_anim_ctrl" />
- "1.0" encoding="utf-8"?>
"http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> "@+id/list" android:layout_width="fill_parent" android:layout_height="fill_parent" android:persistentDrawingCache="animation|scrolling" android:layoutAnimation="@anim/layout_anim_ctrl" />
上面的代码中通过layoutAnimation指定了前面定义的LayoutAnimationController,为了使动画效果比较流畅这里还通过persistentDrawingCache设置了控件的绘制缓存策略,一共有4中策略:PERSISTENT_NO_CACHE 说明不在内存中保存绘图缓存; PERSISTENT_ANIMATION_CACHE 说明只保存动画绘图缓存;PERSISTENT_SCROLLING_CACHE 说明只保存滚动效果绘图缓存PERSISTENT_ALL_CACHES 说明所有的绘图缓存都应该保存在内存中。 在Activity中并没有什么变化,代码如下(src\org\goodev\animation\ListActivity.java):view plaincopy to clipboardprint?
- 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
listItemAdapter = - new ArrayAdapter
(this, - android.R.layout.simple_list_item_1, mListItems);
- ListView lv = (ListView) this.findViewById(R.id.list);
- lv.setAdapter(listItemAdapter);
- }
- }
- 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
listItemAdapter = new ArrayAdapter (this, android.R.layout.simple_list_item_1, mListItems); ListView lv = (ListView) this.findViewById(R.id.list); lv.setAdapter(listItemAdapter); } }
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控件动画介绍其实控件动画也是布局动画的一种,可以看做是自定义的动画实现,布局动画在XML中定义OPhone已经实现的几个动画效果(AlphaAnimation、TranslateAnimation、ScaleAnimation、RotateAnimation)而控件动画就是在代码中继承android.view.animation.Animation类来实现自定义效果。 控件动画实现通过重写Animation的 applyTransformation (float interpolatedTime, Transformation t)函数来实现自定义动画效果,另外一般也会实现 initialize (int width, int height, int parentWidth, int parentHeight)函数,这是一个回调函数告诉Animation目标View的大小参数,在这里可以初始化一些相关的参数,例如设置动画持续时间、设置Interpolator、设置动画的参考点等。OPhone在绘制动画的过程中会反复的调用applyTransformation 函数,每次调用参数interpolatedTime值都会变化,该参数从0渐变为1,当该参数为1时表明动画结束。通过参数Transformation 来获取变换的矩阵(matrix),通过改变矩阵就可以实现各种复杂的效果。关于矩阵的详细信息可以参考android.graphics.Matrix的API文档(http://androidappdocs-staging.appspot.com/reference/android/graphics/Matrix.html )。 下面来看一个简单的实现: view plaincopy to clipboardprint?listItemAdapter = new ArrayAdapter (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);
- }
- }
- 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); } }
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); } }上面的代码很简单,在initialize函数中设置变换持续的时间2500毫秒,然后设置Interpolator为LinearInterpolator并设置FillAfter为true这样可以在动画结束的时候保持动画的完整性。在applyTransformation函数中通过MatrixsetScale函数来缩放,该函数的两个参数代表X、Y轴缩放因子,由于interpolatedTime是从0到1变化所在这里实现的效果就是控件从最小逐渐变化到最大。调用View的startAnimation函数(参数为Animation)就可以使用自定义的动画了。代码如下(src\org\goodev\animation\ViewAnimActivity.java): view plaincopy to clipboardprint?
- 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());
- }
- });
- }
- }
- 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()); } }); } }
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()); } }); } }布局代码如下(res\layout\view_anim_layout.xml):view plaincopy to clipboardprint?
- "1.0" encoding="utf-8"?>
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- android:id="@+id/play_btn"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="Start Animation"
- />
- android:id="@+id/anim_image"
- android:persistentDrawingCache="animation|scrolling"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:src="@drawable/ophone"
- />
- "1.0" encoding="utf-8"?>
"http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > "@+id/play_btn" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Start Animation" /> "@+id/anim_image" android:persistentDrawingCache="animation|scrolling" android:layout_width="fill_parent" android:layout_height="wrap_content" android:src="@drawable/ophone" />
从上图可以看到ImageView是从左上角出来的,这是由于没有指定矩阵的变换参考位置,默认位置为(0,0),如果要想让ImageView从中间出来,可以通过矩阵变换来把参考点移动到中间来,如下实现: view plaincopy to clipboardprint?
- 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);
- }
- }
- 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); } }
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); } }preTranslate函数是在缩放前移动而postTranslate是在缩放完成后移动。现在ImageView就是从中间出来的了。这样通过操作Matrix 可以实现各种复杂的变换。由于操作Matrix是实现动画变换的重点,这里简单介绍下Matrix的常用操作:
- 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();
- }
- }
- 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(); } }
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(); } }camera.translate(0.0f, 0.0f, (1300 - 1300.0f * interpolatedTime))在第一次调用的时候interpolatedTime值为0,相当于把ImageView在Z轴后移1300像素,然后逐步的往前移动到0,同时camera.rotateY(360 * interpolatedTime)函数又把ImageView沿Y轴翻转360度
Animation动画效果的实现
Qt 4.6 Animation动画实现之状态机
动画简介Animation Introduction
关于皮克斯动画工作室-Pixar Animation Studios
Photoshop打造小溪潺潺流水的动画效果
photoshop制作飘动的头发动画效果
用毛笔写字的动画效果
Ps打造小溪潺潺流水的动画效果
实现动态模糊效果
教你如何用flash实现鞭炮的爆炸效果
VB,VB编程实现图像的漂亮效果,珠江路在线
怎么实现文字环绕图片/多媒体的效果
PPT课件中实现"时针"的旋转效果
Premiere中马赛克追踪效果的二种实现方法
foxpro实现的几则小功能(文字动画)
机械舞神的级别!!Poppin John Animation Clip
Android 动画效果
制作PowerPoint动画效果
自定义PopupWindow动画效果
android animation
高铭暄:李昌奎获死刑实现了法律效果和社会效果的统一
打字效果的个性留言板\鼠标跟随动画 (自用)
最全的人体穴位经络图 (动画效果)
太最全的人体穴位经络图(动画效果)