Tween动画是对视图对象中的内容进行一系列简单的转换,比如位置的移动,大小的缩放,旋转,透明度得变化等等。Tween动画可以写到一个xml文件中,就像定义布局文件一样,当然,也可以写到android代码中,不过推荐写到xml文件中,因为它具备的阅读性,可重用性大大超过了硬编码。xml文件放在工程的res/anim目录中,这个目录中要包含一个根元素,可以是<scale>,<translate>,<alpha>或者<rotate>,当然,这些元素都可以放到一个动画集合<set>中,默认情况下,所有的动画指令都是同时发生的,为了让他们按顺序发生,需要设置一个特殊的属性startOffset。下面定义了一个动画的集合:
1 <? xml version="1.0" encoding="utf-8" ?> 2 < set xmlns:android ="http://schemas.android.com/apk/res/android" 3 android:shareInterpolator ="false" > 4 < scale android:interpolator ="@android:anim/accelerate_decelerate_interpolator" 5 android:fromXScale ="1.0" 6 android:toXScale ="1.4" 7 android:fromYScale ="1.0" 8 android:toYScale ="0.6" 9 android:pivotX ="50%" 10 android:pivotY ="50%" 11 android:fillAfter ="false" 12 android:duration ="700" /> 13 14 < set android:interpolator ="@android:anim/decelerate_interpolator" > 15 < scale 16 android:fromXScale ="1.4" 17 android:toXScale ="0.0" 18 android:fromYScale ="0.6" 19 android:toYScale ="0.0" 20 android:pivotX ="50%" 21 android:pivotY ="50%" 22 android:startOffset ="700" 23 android:duration ="400" 24 android:fillBefore ="false" /> 25 < rotate 26 android:fromDegrees ="0" 27 android:toDegrees ="-45" 28 android:toYScale ="0.0" 29 android:pivotX ="50%" 30 android:pivotY ="50%" 31 android:startOffset ="700" 32 android:duration ="400" /> 33 </ set > 34 </ set >
这里解释一下这段代码的作用:
首先是一个动画的集合set,在这个set中有一个属性shareInterpolater,如果设置为true,则这个集合下面所有的子元素都享有同样的interpolater,api上面是这样说的:
android:shareInterpolator
1 <? xml version="1.0" encoding="utf-8" ?> 2 < LinearLayout 3 xmlns:android ="http://schemas.android.com/apk/res/android" 4 android:layout_width ="match_parent" 5 android:layout_height ="match_parent" > 6 < ImageView 7 android:id ="@+id/imageView1" 8 android:src ="@drawable/duola" 9 android:layout_width ="match_parent" 10 android:layout_height ="match_parent" ></ ImageView > 11 </ LinearLayout >
package com.test.shang; import android.app.Activity; import android.graphics.drawable.AnimationDrawable; import android.graphics.drawable.TransitionDrawable; import android.os.Bundle; import android.view.MotionEvent; import android.view.animation.Animation; import android.view.animation.AnimationSet; import android.view.animation.AnimationUtils; import android.widget.ImageView; public class TestStyle extends Activity { AnimationDrawable animationDrawable; @Override protected void onCreate (Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.test); ImageView iv = (ImageView) findViewById(R.id.imageView1); Animation animation = (AnimationSet) AnimationUtils.loadAnimation( this , R.anim.anim_set); iv.setAnimation(animation); animation.start(); } }
1 <? xml version="1.0" encoding="utf-8" ?> 2 < animation-list xmlns:android ="http://schemas.android.com/apk/res/android" 3 android:oneshot ="true" > 4 < item android:drawable ="@drawable/register" android:duration ="500" /> 5 < item android:drawable ="@drawable/duola" android:duration ="500" /> 6 < item android:drawable ="@drawable/icon" android:duration ="500" /> 7 </ animation-list >
1 <? xml version="1.0" encoding="utf-8" ?> 2 < LinearLayout 3 xmlns:android ="http://schemas.android.com/apk/res/android" 4 android:layout_width ="match_parent" 5 android:layout_height ="match_parent" > 6 < ImageView 7 android:id ="@+id/imageView1" 8 android:layout_width ="match_parent" 9 android:layout_height ="match_parent" ></ ImageView > 10 </ LinearLayout >
1 package com.test.shang; 2 3 import android.app.Activity; 4 import android.graphics.drawable.AnimationDrawable; 5 import android.graphics.drawable.TransitionDrawable; 6 import android.os.Bundle; 7 import android.view.MotionEvent; 8 import android.view.animation.Animation; 9 import android.view.animation.AnimationSet; 10 import android.view.animation.AnimationUtils; 11 import android.widget.ImageView; 12 13 public class TestStyle extends Activity { 14 AnimationDrawable animationDrawable; 15 16 @Override 17 protected void onCreate (Bundle savedInstanceState) { 18 super .onCreate(savedInstanceState); 19 setContentView(R.layout.test); 20 ImageView iv = (ImageView) findViewById(R.id.imageView1); 21 iv.setBackgroundResource(R.anim.anim_list); 22 animationDrawable = (AnimationDrawable) iv.getBackground(); 23 } 24 @Override 25 public boolean onTouchEvent (MotionEvent event) { 26 if (event.getAction() == MotionEvent.ACTION_DOWN) { 27 animationDrawable.start(); 28 return true ; 29 } 30 return super .onTouchEvent(event); 31 } 32 }
1 package com.test.shang; 2 3 import android.app.Activity; 4 import android.graphics.Bitmap; 5 import android.graphics.BitmapFactory; 6 import android.graphics.Matrix; 7 import android.graphics.drawable.BitmapDrawable; 8 import android.os.Bundle; 9 import android.widget.ImageView; 10 import android.widget.ImageView.ScaleType; 11 import android.widget.LinearLayout; 12 import android.widget.LinearLayout.LayoutParams; 13 14 public class BitmapTest extends Activity { 15 16 @Override 17 protected void onCreate (Bundle savedInstanceState) { 18 super .onCreate(savedInstanceState); 19 setTitle( " 测试图片的缩放和旋转 " ); 20 LinearLayout layout = new LinearLayout( this ); 21 22 // 加载需要操作的图片,这里是机器猫的图片 23 Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(), R.drawable.duola); 24 25 // 获取这个图片的宽和高 26 int width = bitmapOrg.getWidth(); 27 int height = bitmapOrg.getHeight(); 28 29 // 定义预转换成的图片的宽和高 30 int newWidth = 200 ; 31 int newHight = 200 ; 32 33 // 计算缩放率,新尺寸除原尺寸 34 float scaleWidth = ( float )newWidth / width; 35 float scaleHeight = ( float )newHight / height; 36 37 // 创建操作图片用的matrix对象 38 Matrix matrix = new Matrix(); 39 40 // 缩放图片动作 41 matrix.postScale(scaleWidth, scaleHeight); 42 43 // 旋转图片动作 44 matrix.postRotate( 45 ); 45 46 // 创建新的图片 47 Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0 , 0 , width, height, matrix, true ); 48 49 // 将上面创建的Bitmap转换成Drawable对象,使得其可以使用在imageView,imageButton上。 50 BitmapDrawable bitmapDrawable = new BitmapDrawable(resizedBitmap); 51 52 // 创建一个ImageView 53 ImageView iv = new ImageView( this ); 54 55 // 将imageView的图片设置为上面转换的图片 56 iv.setImageDrawable(bitmapDrawable); 57 58 // 将图片居中显示 59 iv.setScaleType(ScaleType.CENTER); 60 61 // 将imageView添加到布局模板中 62 layout.addView(iv, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 63 64 // 设置为本activity的模板 65 setContentView(layout); 66 } 67 }