android中Animation的使用!

  
public class MainActivity extends Activity implements OnClickListener     
{     
    /**   
     * 定义四个按钮和一张图片   
     */     
    private ImageView imageView = null;     
    private Button rotateButton = null;     
    private Button scaleButton = null;     
    private Button alphaButton = null;     
    private Button translateButton = null;     
     
    @Override     
    public void onCreate(Bundle savedInstanceState)     
    {     
        super.onCreate(savedInstanceState);     
        setContentView(R.layout.main);     
        initView();     
    }     
     
    /**   
     * 初始化界面   
     */     
    public void initView()     
    {     
        imageView = (ImageView) findViewById(R.id.imageViewId);     
     
        rotateButton = (Button) findViewById(R.id.rotateButtonId);     
        translateButton = (Button) findViewById(R.id.translateButtonId);     
        scaleButton = (Button) findViewById(R.id.scaleButtonId);     
        alphaButton = (Button) findViewById(R.id.alphaButtonId);     
     
        rotateButton.setOnClickListener(this);     
        scaleButton.setOnClickListener(this);     
        alphaButton.setOnClickListener(this);     
        translateButton.setOnClickListener(this);     
    }     
     
    @Override     
    public void onClick(View v)     
    {     
        // TODO Auto-generated method stub     
        int switchID = v.getId();     
        switch (switchID)     
        {     
        case R.id.alphaButtonId:     
        {     
                 
            AnimationSet animationSet = new AnimationSet(true);//创建一个AnimationSet对象     
            AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);//创建一个AlphaAnimation对象              
            alphaAnimation.setDuration(1000);//设置动画执行的时间(单位:毫秒)          
            animationSet.addAnimation(alphaAnimation);//将AlphaAnimation对象添加到AnimationSet当中           
            imageView.startAnimation(animationSet);//使用ImageView的startAnimation方法开始执行动画          
            break;     
        }     
     
        case R.id.rotateButtonId:     
        {     
            AnimationSet animationSet = new AnimationSet(true);     
     
            /**   
             * 前两个参数定义旋转的起始和结束的度数,后两个参数定义圆心的位置   
             */     
            RotateAnimation rotateAnimation = new RotateAnimation(0, 360,     
                    Animation.RELATIVE_TO_PARENT, 1f,     
                    Animation.RELATIVE_TO_PARENT, 0f);     
     
            rotateAnimation.setDuration(5000);     
            animationSet.addAnimation(rotateAnimation);     
            imageView.startAnimation(animationSet);     
            break;     
        }     
     
        case R.id.scaleButtonId:     
        {     
            AnimationSet animationSet = new AnimationSet(true);     
                 
            /**   
             * 围绕一个点伸缩   
             */     
            ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.1f, 1,     
                    0.1f, Animation.RELATIVE_TO_SELF, 0.5f,     
                    Animation.RELATIVE_TO_SELF, 0.5f);     
            animationSet.addAnimation(scaleAnimation);     
            animationSet.setStartOffset(1000);     
            animationSet.setFillAfter(true);     
            animationSet.setFillBefore(false);     
            animationSet.setDuration(2000);     
            imageView.startAnimation(animationSet);     
            break;     
        }     
     
        case R.id.translateButtonId:     
        {     
            AnimationSet animationSet = new AnimationSet(true);     
                 
            /**   
             * x和y轴的起始和结束位置   
             */     
            TranslateAnimation translateAnimation = new TranslateAnimation     
            (     
                    Animation.RELATIVE_TO_SELF, 0f,      
                    Animation.RELATIVE_TO_SELF,0.5f,      
                    Animation.RELATIVE_TO_SELF, 0f,     
                    Animation.RELATIVE_TO_SELF, 1.0f     
            );     
                 
            translateAnimation.setDuration(1000);     
            animationSet.addAnimation(translateAnimation);     
            imageView.startAnimation(animationSet);     
            break;     
        }     
        }     
    }     
}    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值