android动画总结

        好的动画效果往往让这个应用变得更有活力。下面我讲讲我到现在经历的动画历程。

我们都知道android现在主要有三种动画:

    View Animation  控件动画
    Drawable Animation  帧动画
    Property Animation  属性动画(后面出来的)

        首先接触到的是 帧动画,当时做一个项目时,用到加载的时候上面是一只猫尾巴一直摇。只要切很多图片,尾巴的位置不同即可:然后写个drawable文件命名为loading.xml

        

    <animation-list xmlns:android="http://schemas.android.com/apk/res/android"  
        android:oneshot="true">  
        <item android:drawable="@drawable/cat_1" android:duration="200" />  
        <item android:drawable="@drawable/cat_2" android:duration="200" />  
        <item android:drawable="@drawable/cat_3" android:duration="200" />  
        <item android:drawable="@drawable/cat_4" android:duration="200" />  
    </animation-list>  

         ImageView loadingImg = (ImageView) findViewById(R.id.loading);  
            loadingImg.setBackgroundResource(R.drawable.loading);  
            loadingAnimation = (AnimationDrawable) loadingImg.getBackground();
         loadingAnimation.start();就可以开启这个动画了。loadingAnimation.stop();停止动画

        接着项目需求做一个类似摇一摇的动画:所以开始接触到控件动画,大家都知道它有四种主要动画分别为

AlphaAnimation,RotateAnimation,ScaleAnimation,TranslateAnimation,分别对应透明度,旋转,大小,位移四种变化。而我用到的就是平移动画。强调一点,这些动画只是效果而不是实际移动,当动画结束后,控件会回到原来位置(经常遇到的困惑),要想结束以后不回到原来位置,请设置动画监听,在结束的时候,设置控件的位置。如

    Anim.setAnimationListener(new AnimationListener() {  
      
                            @Override  
                            public void onAnimationStart(Animation animation) {  
                                // TODO Auto-generated method stub  
      
                            }  
                            @Override  
                            public void onAnimationRepeat(Animation animation) {  
                                // TODO Auto-generated method stub  
      
                            }  
      
                            @Override  
                            public void onAnimationEnd(Animation animation) {  
                                // TODO Auto-generated method stub  
                               //手写控件所在位置。
                                view.clearAnimation();  
                            }  
                        });  


具体调用看下方代码:

public void startAnim() { // 定义摇一摇动画动画
 
AnimationSet animup = new AnimationSet(true);
 
animup.setAnimationListener(new AnimationListener() {

@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub

}

@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub

}

@Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
//dialogBuilder.show();
showProgress();
isrefresh=true;
showDialog();

}

});
TranslateAnimation  mup0 = new TranslateAnimation(
 
Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f,
 
Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF,
 
-0.3f);
 
 mup0.setDuration(500);
 
TranslateAnimation  mup1 = new TranslateAnimation(
 
Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f,
 
Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF,
 
+0.3f);
 
 mup1.setDuration(500);
 
//延迟执行1秒
 
 mup1.setStartOffset(1000);
 
animup.addAnimation( mup0);
 
animup.addAnimation( mup1);
 
//上图片的动画效果的添加
getView(R.id.shakeone).startAnimation(animup);
 
AnimationSet animdn = new AnimationSet(true);
 
TranslateAnimation  mdn0 = new TranslateAnimation(
 
Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f,
 
Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF,
 
+0.3f);
 
 mdn0.setDuration(500);
 
TranslateAnimation  mdn1 = new TranslateAnimation(
 
Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f,
 
Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF,
 
-0.3f);
 
 mdn1.setDuration(500);
 
//延迟执行1秒
 
 mdn1.setStartOffset(1000);
 
animdn.addAnimation( mdn0);
 
animdn.addAnimation( mdn1);
 
//下图片动画效果的添加
getView(R.id.shaketwo).startAnimation(animdn);
 
}

补充一点:在用到平移动画之外还应用了一个dialog的动画,即当我们摇到奖品的时候会有一个动画:类似微信摇一摇:下面代码是我写的窗口显示位置以及相应的动画,还为其做了适配:
//设置窗口显示  
    public void windowDeploy(){  
    
        window = dialog.getWindow(); //得到对话框  
        if(getScreenHeight(context)==800){
        window.setWindowAnimations(R.style.dialogAnim800); //设置窗口弹出动画
        }else if(getScreenHeight(context)==1280){
         window.setWindowAnimations(R.style.dialogAnim1280); //设置窗口弹出动画
        }else if(getScreenHeight(context)==1920){
         window.setWindowAnimations(R.style.dialogAnim1920); //设置窗口弹出动画
        }else if(getScreenHeight(context)==480){
         window.setWindowAnimations(R.style.dialogAnim480); //设置窗口弹出动画
        }else {
         window.setWindowAnimations(R.style.dialogAnim800); //设置窗口弹出动画
}
        //window.setBackgroundDrawableResource(R.color.vifrification); //设置对话框背景为透明  
        WindowManager.LayoutParams wl = window.getAttributes();  
        wl.width=(int)(getScreenWidth(context)*0.9);
        wl.height=(int)(getScreenWidth(context)*0.35);
        //根据x,y坐标设置窗口需要显示的位置  
        wl.x = 0; //x小于0左移,大于0右移  
        wl.y = getScreenHeight(context)/3; //y小于0上移,大于0下移   
//        wl.alpha = 0.6f; //设置透明度  
//        wl.gravity = Gravity.BOTTOM; //设置重力  
        window.setAttributes(wl);  
        //设置触摸对话框意外的地方取消对话框  
        dialog.setCanceledOnTouchOutside(true);
        dialog.show();  
    }
最后就是高大上的属性动画了:
好吧,我就不说我写属性代码的经历了,因为再也写不过鸿洋大神的blog写的那么详细了,附上其博客地址
http://blog.csdn.net/lmj623565791/article/details/38067475
     此仅为自己对android动画学习的一个总结,有不足之处欢迎大家批评指正。



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

水的川

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值