通過<scale>标簽可以定義縮放補間動畫。下面是一個标準示例:
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="500"
android:fromXScale="0.2"
android:fromYScale="0.2"
android:interpolator="@android:anim/decelerate_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1.0"
android:toYScale="1.0" />
大多數的屬性和<translate>标簽的屬性是相同的,而有些屬性是<scale>獨有的,比如
android:pivotX:表示沿X軸方向縮放的支點位置。如果該屬性值為50%,則支點在沿X軸的中心位置。
android:pivotY:表示沿Y軸方向縮放的支點位置。如果該屬性值為50%,則支點在沿Y軸的中心位置。
如果想通過Java代碼實作縮放補間動畫,可以建立ScaleAnimation對象。ScaleAnimation類構造方法的定義如下:
public ScaleAnimation(float fromX, float toX, float fromY, float toY, float pivotX, float pivotY)
通過ScaleAnimation類的構造方法可以設定上述6個屬性值,設定其他屬性的方法與移動補間動畫相同。
簡單示例代碼:
public class Main extends Activity implements AnimationListener
{
private Animation toLargeAnimation;
private Animation toSmallAnimation;
private ImageView imageView;
@Override
public void onAnimationEnd(Animation animation)
{
if (animation.hashCode() == toLargeAnimation.hashCode())
imageView.startAnimation(toSmallAnimation);
else
imageView.startAnimation(toLargeAnimation);
}
@Override
public void onAnimationRepeat(Animation animation)
{
// TODO Auto-generated method stub
}
@Override
public void onAnimationStart(Animation animation)
{
// TODO Auto-generated method stub
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageView = (ImageView) findViewById(R.id.imageview);
toLargeAnimation = AnimationUtils.loadAnimation(this, R.anim.to_large);
toSmallAnimation = AnimationUtils.loadAnimation(this, R.anim.to_small);
toLargeAnimation.setAnimationListener(this);
toSmallAnimation.setAnimationListener(this);
imageView.startAnimation(toSmallAnimation);
}
}
旋轉補間動畫
通過<rotate>标簽可以定義旋轉補間動畫。
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="10000"
android:fromDegrees="0"
android:interpolator="@anim/linear_interpolator"
android:pivotX="200%"
android:pivotY="300%"
android:repeatCount="infinite"
android:repeatMode="restart"
android:toDegrees="360" />
其中有兩個特殊屬性:
android:fromDegrees:表示旋轉的起始角度。
android:toDegrees:表示旋轉的結束角度。
android:repeatCount:設定旋轉的次數。該值預設值為0,表示隻播放一次,不重複顯示動畫。如果想讓動畫永不停止,則将其設為infinite或-1.
android:repeatMode:設定重複的模式,預設值是restart。該屬性隻有當android:repeatCount設定成大于0的數或infinite時才起作用。android:repeatMode屬性值除了restart還可以設定為reverse,表示偶數次顯示動畫時才會做與動畫檔案定義的方向相反的動作。例如,在第1、3、5、...、2n-1圈順時針旋轉,而在2、4、6、...、2n圈逆時針旋轉。如過想通過Java代碼設定,則可以使用Animation類的setRepeatMode方法,該方法隻接收一個int類型參數。可取的值為Animation.RESTART和Animation.REVERSE。
如果想通過Java代碼實作旋轉補間動畫,可以建立RotateAnimation對象。RotateAnimation類構造函數的定義如下:
public RotateAnimation(float fromDegrees, float toDegrees, float pivotX, float pivotY)
簡單的示例代碼:
linear_interpolator.xml
<linearInterpolator xmlns:android="http://schemas.android.com/apk/res/android"/>
public class Main extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView ivEarth = (ImageView) findViewById(R.id.ivEarth);
ImageView ivHesper = (ImageView) findViewById(R.id.ivHesper);
ImageView ivSun = (ImageView) findViewById(R.id.ivSun);
Animation earthAnimation = AnimationUtils.loadAnimation(this,
R.anim.earth);
Animation hesperAnimation = AnimationUtils.loadAnimation(this,
R.anim.hesper);
Animation sunAnimation = AnimationUtils.loadAnimation(this, R.anim.sun);
ivEarth.startAnimation(earthAnimation);
ivHesper.startAnimation(hesperAnimation);
ivSun.startAnimation(sunAnimation);
}
}
透明度補間動畫
通過<alpha>标簽可以定義透明度補間動畫。
<scale
android:duration="2000"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:interpolator="@android:anim/accelerate_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="0.2"
android:toYScale="0.2" />
<!--其中android:fromAlpha和android:toAlpha屬性分别表示起始透明度和結束透明度,這兩個屬性的值都在0.0到1.0之間。屬性值為0.0表示完全透明,屬性值為1.0表示完全不透明。-->
如果想通過Java代碼實作透明度,可以建立AlphaAnimation對象。AlphaAnimation類構造方法的定義如下:
public AlphaAnimation(float fromAlpha, float toAlpha)
通過AlphaAnimation類構造方法可以設定起始透明度和結束透明度。
示例代碼如下:
missile.xml
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<alpha
android:duration="2000"
android:fromAlpha="1.0"
android:interpolator="@android:anim/accelerate_interpolator"
android:toAlpha="0.1" />
<translate
android:duration="2000"
android:fromXDelta="0"
android:fromYDelta="0"
android:interpolator="@android:anim/accelerate_interpolator"
android:toXDelta="0"
android:toYDelta="-380" />
<scale
android:duration="2000"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:interpolator="@android:anim/accelerate_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="0.2"
android:toYScale="0.2" />
</set>
如上所示,<set>标簽作為XML根節點,所有在<set>标簽中定義的動畫會在同一時間開始,在混合動畫效果的情況下,往往會使用<set>标簽來組合動畫效果。
public class MyImageView extends ImageView
{
public AnimationDrawable animationDrawable;
public ImageView ivMissile;
public Field field;
@Override
protected void onDraw(Canvas canvas)
{
try
{
//利用反射技術得到目前的幀号:當顯示完最後一幅圖像之後,将MyImageView元件隐藏,并顯示炸彈的原始圖像
field = AnimationDrawable.class.getDeclaredField("mCurFrame");
field.setAccessible(true);
int curFrame = field.getInt(animationDrawable);
if (curFrame == animationDrawable.getNumberOfFrames() - 1)
{
setVisibility(View.INVISIBLE);
ivMissile.setVisibility(View.VISIBLE);
}
}
catch (Exception e)
{
}
super.onDraw(canvas);
}
public MyImageView(Context context, AttributeSet attrs)
{
super(context, attrs);
}
}
public class Main extends Activity implements OnTouchListener,
AnimationListener
{
private ImageView ivMissile;
private MyImageView ivBlast;
private AnimationDrawable animationDrawable;
private Animation missileAnimation;
@Override
public boolean onTouch(View view, MotionEvent event)
{
ivMissile.startAnimation(missileAnimation);
return false;
}
@Override
public void onAnimationEnd(Animation animation)
{
ivBlast.setVisibility(View.VISIBLE);
ivMissile.setVisibility(View.INVISIBLE);
try
{
MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.bomb);
mediaPlayer.stop();
mediaPlayer.prepare();
mediaPlayer.start();
}
catch (Exception e)
{
}
animationDrawable.stop();
animationDrawable.start();
}
@Override
public void onAnimationRepeat(Animation animation)
{
// TODO Auto-generated method stub
}
@Override
public void onAnimationStart(Animation animation)
{
// TODO Auto-generated method stub
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ivMissile = (ImageView) findViewById(R.id.ivMissile);
ivMissile.setOnTouchListener(this);
ivBlast = (MyImageView) findViewById(R.id.ivBlast);
ivBlast.setBackgroundResource(R.anim.blast);
Object backgroundObject = ivBlast.getBackground();
animationDrawable = (AnimationDrawable) backgroundObject;
ivBlast.animationDrawable = animationDrawable;
missileAnimation = AnimationUtils.loadAnimation(this, R.anim.missile);
missileAnimation.setAnimationListener(this);
ivBlast.setVisibility(View.INVISIBLE);
ivBlast.ivMissile = ivMissile;
}
}
public class Main extends Activity implements OnTouchListener,
AnimationListener
{
private ImageView ivMissile;
private MyImageView ivBlast;
private AnimationDrawable animationDrawable;
private Animation missileAnimation;
@Override
public boolean onTouch(View view, MotionEvent event)
{
ivMissile.startAnimation(missileAnimation);
return false;
}
@Override
public void onAnimationEnd(Animation animation)
{
ivBlast.setVisibility(View.VISIBLE);
ivMissile.setVisibility(View.INVISIBLE);
try
{
MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.bomb);
mediaPlayer.stop();
mediaPlayer.prepare();
mediaPlayer.start();
}
catch (Exception e)
{
}
animationDrawable.stop();
animationDrawable.start();
}
@Override
public void onAnimationRepeat(Animation animation)
{
// TODO Auto-generated method stub
}
@Override
public void onAnimationStart(Animation animation)
{
// TODO Auto-generated method stub
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ivMissile = (ImageView) findViewById(R.id.ivMissile);
ivMissile.setOnTouchListener(this);
ivBlast = (MyImageView) findViewById(R.id.ivBlast);
ivBlast.setBackgroundResource(R.anim.blast);
Object backgroundObject = ivBlast.getBackground();
animationDrawable = (AnimationDrawable) backgroundObject;
ivBlast.animationDrawable = animationDrawable;
missileAnimation = AnimationUtils.loadAnimation(this, R.anim.missile);
missileAnimation.setAnimationListener(this);
ivBlast.setVisibility(View.INVISIBLE);
ivBlast.ivMissile = ivMissile;
}
}