看看这本书 -《Android游戏编程之从零开始》
Day_4_1 2019/01/22
Animation动画
下面是4种动画效果:
- AlphaAnimation : 透明度渐变动画
- ScaleAnimation : 渐变尺寸缩放动画
- TranslateAnimation : 移动动画
- RotateAnimation : 旋转动画
AlphaAnimation透明度渐变动画
创建:
/**
* 透明度渐变动画
* @param fromAlpha 动画开始时的透明度
* @param toAlpha 动画结束时的透明度
* 取值范围[0,1],从完全透明到完全不透明
* @return
*/
public Animation alphaAnimation(
float fromAlpha ,
float toAlpha )
{
Animation animation = new AlphaAnimation( fromAlpha , toAlpha );
return animation;
}
API:
/**
* Constructor to use when building an AlphaAnimation from code
*
* @param fromAlpha Starting alpha value for the animation, where 1.0 means
* fully opaque and 0.0 means fully transparent.
* @param toAlpha Ending alpha value for the animation.
*/
public AlphaAnimation(float fromAlpha, float toAlpha)
ScaleAnimation渐变尺寸缩放动画
创建:
/**
* 渐变尺寸缩放动画
* @param fromX 动画起始时X坐标上的伸缩比例
* @param toX 动画结束时X坐标上的伸缩比例
* @param fromY 动画起始时Y坐标上的伸缩比例
* @param toY 动画结束时Y坐标上的伸缩比例
* @param pivotXType 动画在X轴相对物体的位置类型
* @param pivotXValue 动画相对于物体X坐标的位置
* @param pivotYType 动画在Y轴相对物体的位置类型
* @param pivotYValue 动画相对于物体Y坐标的位置
* 位置类型:
* Animation.ABSOLUTE 相对位置是屏幕左上角,绝对位置
* Animation.RELATIVE_TO_SELF 相对位置是自身view
* 取值为0时,表示相对于是自身左上角,取值为1是相对于自身右下角
* Animation.RELATIVE_TO_PARENT 相对父类View的位置
*/
public Animation scaleAnimation(
float fromX ,
float toX ,
float fromY ,
float toY ,
int pivotXType ,
float pivotXValue ,
int pivotYType ,
float pivotYValue
)
{
Animation animation = new ScaleAnimation( fromX , toX , fromY , toY ,
pivotXType , pivotXValue , pivotYType , pivotYValue );
return animation;
}
API:
/**
* Constructor to use when building a ScaleAnimation from code
*
* @param fromX Horizontal scaling factor to apply at the start of the
* animation
* @param toX Horizontal scaling factor to apply at the end of the animation
* @param fromY Vertical scaling factor to apply at the start of the
* animation
* @param toY Vertical scaling factor to apply at the end of the animation
* @param pivotXType Specifies how pivotXValue should be interpreted. One of
* Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or
* Animation.RELATIVE_TO_PARENT.
* @param pivotXValue The X coordinate of the point about which the object
* is being scaled, specified as an absolute number where 0 is the
* left edge. (This point remains fixed while the object changes
* size.) This value can either be an absolute number if pivotXType
* is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
* @param pivotYType Specifies how pivotYValue should be interpreted. One of
* Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or
* Animation.RELATIVE_TO_PARENT.
* @param pivotYValue The Y coordinate of the point about which the object
* is being scaled, specified as an absolute number where 0 is the
* top edge. (This point remains fixed while the object changes
* size.) This value can either be an absolute number if pivotYType
* is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
*/
public ScaleAnimation(float fromX, float toX, float fromY, float toY,
int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
TranslateAnimation移动动画
创建:
/**
* 位移动画
* @param fromXDelta 动画起始时X轴上的位置
* @param toXDelta 动画结束时X轴上的位置
* @param fromYDelta 动画起始时Y轴上的位置
* @param toYDelta 动画结束时Y轴上的位置
* @return
*/
public Animation translateAnimation(
float fromXDelta ,
float toXDelta ,
float fromYDelta ,
float toYDelta )
{
Animation animation = new TranslateAnimation( fromXDelta , toXDelta ,
fromYDelta , toYDelta );
return animation;
}
API:
/**
* Constructor to use when building a TranslateAnimation from code
*
* @param fromXDelta Change in X coordinate to apply at the start of the
* animation
* @param toXDelta Change in X coordinate to apply at the end of the
* animation
* @param fromYDelta Change in Y coordinate to apply at the start of the
* animation
* @param toYDelta Change in Y coordinate to apply at the end of the
* animation
*/
public TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)
RotateAnimation旋转动画
创建:
/**
* 旋转动画
* @param fromDegrees 动画起始时的旋转角度
* @param toDegrees 动画旋转到的角度
* @param pivotXType 动画在X轴相对于物体位置类型
* @param pivotXValue 动画相对于物体X坐标的位置
* @param pivotYType 动画在Y轴相对物体的位置类型
* @param pivotYValue 动画相对于物体Y坐标的位置
* @return
*/
public Animation rotateAnimation(
float fromDegrees ,
float toDegrees ,
int pivotXType ,
float pivotXValue ,
int pivotYType ,
float pivotYValue )
{
Animation animation = new RotateAnimation( fromDegrees , toDegrees , pivotXType ,
pivotXValue , pivotYType , pivotYValue );
return animation;
}
API:
/**
* Constructor to use when building a RotateAnimation from code
*
* @param fromDegrees Rotation offset to apply at the start of the
* animation.
*
* @param toDegrees Rotation offset to apply at the end of the animation.
*
* @param pivotXType Specifies how pivotXValue should be interpreted. One of
* Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or
* Animation.RELATIVE_TO_PARENT.
* @param pivotXValue The X coordinate of the point about which the object
* is being rotated, specified as an absolute number where 0 is the
* left edge. This value can either be an absolute number if
* pivotXType is ABSOLUTE, or a percentage (where 1.0 is 100%)
* otherwise.
* @param pivotYType Specifies how pivotYValue should be interpreted. One of
* Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or
* Animation.RELATIVE_TO_PARENT.
* @param pivotYValue The Y coordinate of the point about which the object
* is being rotated, specified as an absolute number where 0 is the
* top edge. This value can either be an absolute number if
* pivotYType is ABSOLUTE, or a percentage (where 1.0 is 100%)
* otherwise.
*/
public RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue,
int pivotYType, float pivotYValue)
通用方法
reset();//重新播放动画
setDuration( int time );//设置动画播放时间,单位是毫秒
下面是demo代码
public class AnimationView extends View implements Animation.AnimationListener
{
private Paint paint;
private Bitmap bitmap = BitmapFactory.decodeResource( getResources() , R.drawable.image );
private AnimationApi animationApi;
private Animation animation;
public AnimationView( Context context )
{
super( context );
setFocusable( true );
paint = new Paint();
paint.setColor( Color.BLACK );
animationApi = new AnimationApi();
}
@Override
protected void onDraw( Canvas canvas )
{
super.onDraw( canvas );
canvas.drawColor( Color.BLACK );
canvas.drawText( "方向键↑ 渐变透明度动画效果" , 80 , this.getHeight() - 80 , paint );
canvas.drawText( "方向键↓ 渐变尺寸伸缩动画效果" , 80 , this.getHeight() - 60 , paint );
canvas.drawText( "方向键← 位置移动动画效果" , 80 , this.getHeight() - 40 , paint );
canvas.drawText( "方向键→ 画面旋转动画效果" , 80 , this.getHeight() - 20 , paint );
canvas.drawBitmap( bitmap , this.getWidth() / 2 - bitmap.getWidth() / 2 , this.getHeight() / 2 - bitmap
.getHeight() / 2 , paint );
}
@Override
public boolean onKeyDown(
int keyCode ,
KeyEvent event )
{
switch( keyCode )
{
case KeyEvent.KEYCODE_DPAD_UP:
animation = animationApi.alphaAnimation( 0.1f , 1.0f );
animation.setDuration( 3000 );
break;
case KeyEvent.KEYCODE_DPAD_DOWN:
animation = animationApi.scaleAnimation( 0.0f , 2.0f , 1.5f , 1.5f , Animation.RELATIVE_TO_PARENT ,
0.5f , Animation.RELATIVE_TO_PARENT , 0.0f );
animation.setDuration( 2000 );
break;
case KeyEvent.KEYCODE_DPAD_LEFT:
animation = animationApi.translateAnimation( 0 , 100 , 0 , 100 );
animation.setDuration( 2000 );
break;
case KeyEvent.KEYCODE_DPAD_RIGHT:
animation = animationApi.rotateAnimation( 0.0f , 360.0f , Animation.RELATIVE_TO_SELF , 0.5f ,
Animation.RELATIVE_TO_SELF , 0.5f );
animation.setDuration( 3000 );
break;
default:
break;
}
if( animation != null )
{
this.startAnimation( animation );
}
return super.onKeyDown( keyCode , event );
}
@Override
public boolean onKeyUp(
int keyCode ,
KeyEvent event )
{
return super.onKeyUp( keyCode , event );
}
@Override
public boolean onTouchEvent( MotionEvent event )
{
return super.onTouchEvent( event );
}
@Override
public void onAnimationStart( Animation animation )
{
//动画开始时响应
}
@Override
public void onAnimationEnd( Animation animation )
{
//动画结束时响应
}
@Override
public void onAnimationRepeat( Animation animation )
{
//动画重播时响应
}
}
186/406