码迷,mamicode.com
首页 > 移动开发 > 详细

Android上几种Animation和多个动画同时播放以ScaleAnimation应用详解

时间:2014-08-23 16:42:41      阅读:781      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   http   color   java   os   使用   io   

在API Demo的View->Animation下可以找到四个Animation的Demo,第一个3D Translate比较复杂,最后再讲,先讲第2个Interpolator。该Activity对应的是view包内的Animation3.java,和layout的animation_3.xml。

界面的布局不加解释了,就一个Spinner和一个TextView。不是本文内容。

主要解释下几个重点语句。

 

初始化Animation,从类的名字可以看出是一个变换View的位置的动画,参数起点横坐标,终点横坐标,起点纵坐标,终点纵坐标。

 

Animation a = new TranslateAnimation(0.0f,  
                targetParent.getWidth() - target.getWidth() - targetParent.getPaddingLeft() -  
                targetParent.getPaddingRight(), 0.0f, 0.0f);  
 

 

 

下面是动画的参数设置,我加上了注释

 

 

[java] view plaincopy
 
a.setDuration(1000);//设置动画所用的时间  
        a.setStartOffset(300);//设置动画启动的延时  
        //设置重复模式,RESTART为结束后重新开始,REVERSE为按原来的轨迹逆向返回  
        a.setRepeatMode(Animation.RESTART);  
        //设置重复次数,INFINITE为无限  
        a.setRepeatCount(Animation.INFINITE);  
        //根据用户在Spinner的选择设置target的进入的方式  
        switch (position) {  
            case 0:  
                //加速进入  
                a.setInterpolator(AnimationUtils.loadInterpolator(this,  
                        android.R.anim.accelerate_interpolator));  
                break;  
            case 1:  
                //减速进入  
                a.setInterpolator(AnimationUtils.loadInterpolator(this,  
                        android.R.anim.decelerate_interpolator));  
                break;  
            case 2:  
                //加速进入.与第一个的区别为当repeatMode为reverse时,仍为加速返回原点  
                a.setInterpolator(AnimationUtils.loadInterpolator(this,  
                        android.R.anim.accelerate_decelerate_interpolator));  
                break;  
            case 3:  
                //先往后退一点再加速前进  
                a.setInterpolator(AnimationUtils.loadInterpolator(this,  
                        android.R.anim.anticipate_interpolator));  
                break;  
            case 4:  
                //减速前进,冲过终点前再后退  
                a.setInterpolator(AnimationUtils.loadInterpolator(this,  
                        android.R.anim.overshoot_interpolator));  
                break;  
            case 5:  
                //case 3,4的结合体  
                a.setInterpolator(AnimationUtils.loadInterpolator(this,  
                        android.R.anim.anticipate_overshoot_interpolator));  
                break;  
            case 6:  
                //停止前来回振几下  
                a.setInterpolator(AnimationUtils.loadInterpolator(this,  
                        android.R.anim.bounce_interpolator));  
                break;  
        }  
        //让target开始执行这个动画  
        target.startAnimation(a);  
    }  

 

 

这里使用的是Android已预设的一些动作,我们也可以自定义XML来实现更好看的动画效果的,这个下一篇再讲。

除了TranslationAnimation,还有AlphaAnimation、RotateAnimation、ScaleAnimation,使用这几个基体动作的组合,可以形成一系列复杂的动画效果。具体用法请查看SDK。

整个都比较简单,就一个函数的调用,还不懂的看一下API的注释和SDK文档,没什么难理解的。

 

From:http://blog.csdn.net/lxw1980/article/details/6162985

 

 

AnimationSet 可以包含多个Animation,但都是在同一个时间执行的,是并行,不是串行执行的。
如果AnimationSet中有一些设定,如duration,fillBefore等,它包含的子动作也设定了的话,
子动作中的设定将会给覆盖掉。

If AnimationSet sets any properties that its children also set (for example, duration 
or fillBefore), the values of AnimationSet override the child values. 

AnimationSet as = new AnimationSet( true );
as.addAnimation( ta );
TranslateAnimation t1 = new TranslateAnimation( 2, 200, 8, 300);
t1.setDuration( 2000 );
as.addAnimation( t1 );
RotateAnimation r1 = new RotateAnimation( (float) 1, (float) 0.1 );
r1.setDuration( 2000 );
//as.addAnimation( r1 );
imgView.setAnimation(as);
//as.setDuration( 6000 );
//as.setStartTime( 1000 );
as.start();

 

 ScaleAnimation应用详解

 

android中提供了4中动画: 
AlphaAnimation 透明度动画效果 
ScaleAnimation 缩放动画效果 
TranslateAnimation 位移动画效果 
RotateAnimation 旋转动画效果 

本节讲解ScaleAnimation 动画, 
ScaleAnimation(float fromX, float toX, float fromY, float toY,int pivotXType, float pivotXValue, int pivotYType, float pivotYValue) 
参数说明: 
复制代码 代码如下:

float fromX 动画起始时 X坐标上的伸缩尺寸 
float toX 动画结束时 X坐标上的伸缩尺寸 
float fromY 动画起始时Y坐标上的伸缩尺寸 
float toY 动画结束时Y坐标上的伸缩尺寸 
int pivotXType 动画在X轴相对于物件位置类型 
float pivotXValue 动画相对于物件的X坐标的开始位置 
int pivotYType 动画在Y轴相对于物件位置类型 
float pivotYValue 动画相对于物件的Y坐标的开始位置 

 

public class MainActivity extends Activity { 
ImageView image; 
Button start; 
Button cancel; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 
image = (ImageView) findViewById(R.id.main_img); 
start = (Button) findViewById(R.id.main_start); 
cancel = (Button) findViewById(R.id.main_cancel); 
/** 设置缩放动画 */ 
final ScaleAnimation animation =new ScaleAnimation(0.0f, 1.4f, 0.0f, 1.4f, 
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); 
animation.setDuration(2000);//设置动画持续时间 
/** 常用方法 */ 
//animation.setRepeatCount(int repeatCount);//设置重复次数 
//animation.setFillAfter(boolean);//动画执行完后是否停留在执行完的状态 
//animation.setStartOffset(long startOffset);//执行前的等待时间 
start.setOnClickListener(new OnClickListener() { 
public void onClick(View arg0) { 
image.setAnimation(animation); 
/** 开始动画 */ 
animation.startNow(); 
} 
}); 
cancel.setOnClickListener(new OnClickListener() { 
public void onClick(View v) { 
/** 结束动画 */ 
animation.cancel(); 
} 
}); 
} 
} 

 

 

Android上几种Animation和多个动画同时播放以ScaleAnimation应用详解

标签:android   style   blog   http   color   java   os   使用   io   

原文地址:http://www.cnblogs.com/niray/p/3931402.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!