摘要:本文将带你了解Android应用开发android Tween动画四种动画效果的实现步骤教程,希望本文对大家学Android有所帮助。
本文将带你了解Android应用开发android Tween动画四种动画效果的实现步骤教程,希望本文对大家学Android有所帮助。
Tween Animation:通过预先定义一组指令,指令指定了图形变幻的类型、触发时间、持续时间,对场景里的对象不断平移、缩放、旋转等变幻来产生动画效果。
Animation是以XML格式定义的,定义好的XML文件放在"res/anim"目录中。
在xml文件中,Tween动画主要包括以下四种动画效果:(箭头后是与之对应的java代码方法)
Alpha:渐变透明-------------->AlphaAnimation
Scale:伸缩 --------------->ScaleAnimation
Translate:移动--------------->TranslateAnimation
Rotate:旋转 ---------------->RotateAnimation
实现步骤:
1.定义Animation对象
2.设置动画的属性
3.通过startAnimation()方法开始动画
例子:
1.java代码:
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
/**定义Alpha动画*/
private Animation animAlpha=null;
/**定义Scale动画*/
private Animation animScale=null;
/**定义Translate动画*/
private Animation animTranslate=null;
/**定义Rotate动画*/
private Animation animRotate=null;
private ImageView img;
private TextView alpha,scale,translate,rotate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img=findViewById(R.id.iv_animation);
alpha=findViewById(R.id.alpha);
scale=findViewById(R.id.scale);
translate=findViewById(R.id.translate);
rotate=findViewById(R.id.rotate);
alpha.setOnClickListener(this);
scale.setOnClickListener(this);
translate.setOnClickListener(this);
rotate.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.alpha:
/**创建Alpha动画*/
animAlpha=new AlphaAnimation(0.1f,1.0f);
/**设置动画时间*/
animAlpha.setDuration(3000);
/**开始播放动画*/
img.startAnimation(animAlpha);
break;
case R.id.scale:
/**创建Scale动画*/
animScale=new ScaleAnimation(0.0f,1.0f,0.0f,1.0f,
Animation.RELATIVE_TO_SELF,Animation.RELATIVE_TO_SELF);
/**设置动画时间*/
animScale.setDuration(5000);
/**开始播放动画*/
img.startAnimation(animScale);
break;
case R.id.translate:
/**创建Translate动画*/
animTranslate=new TranslateAnimation(10,100,10,100);
/**设置动画时间*/
animTranslate.setDuration(1000);
/**开始播放动画*/
img.startAnimation(animTranslate);
break;
case R.id.rotate:
/**创建Translate动画*/
animRotate=new RotateAnimation(0.0f,+360.0f,
Animation.RELATIVE_TO_SELF,0.5f,
Animation.RELATIVE_TO_SELF,0.5f);
/**设置动画时间*/
animRotate.setDuration(1000);
/**开始播放动画*/
img.startAnimation(animRotate);
break;
}
}
}
效果:
alpha: scale:
translate:rotate:
=======================================================================================
/**
* 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) {
mFromAlpha = fromAlpha;
mToAlpha = toAlpha;
}
/**
* 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 pivotX 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.)
* @param pivotY 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.)
*/
public ScaleAnimation(float fromX, float toX, float fromY, float toY,
float pivotX, float pivotY) {
mResources = null;
mFromX = fromX;
mToX = toX;
mFromY = fromY;
mToY = toY;
mPivotXType = ABSOLUTE;
mPivotYType = ABSOLUTE;
mPivotXValue = pivotX;
mPivotYValue = pivotY;
initializePivotPoint();
}
=====================================================================================
/**
* 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) {
mFromXValue = fromXDelta;
mToXValue = toXDelta;
mFromYValue = fromYDelta;
mToYValue = toYDelta;
mFromXType = ABSOLUTE;
mToXType = ABSOLUTE;
mFromYType = ABSOLUTE;
mToYType = ABSOLUTE;
}
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之Android频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号