Android动画(1):View动画

View动画:平移、缩放、旋转、透明度;

对应的在xml中的标签为:traslate、scale、rotate、alpha;

四种变换效果对应Animation的四个子类:TranslateAnimation、ScaleAnimation、RotateAnimation、AlphaAnimation.

描述view动画的xml语法如下所示:



    

    

    

    


其中,标签标示动画的集合,对应于AnimationSet类,其中interpalator属性表示动画所采用的插值器,默认为@android:anim/accelerate_decelerate_interpolator,先加速,后减速的效果。shareInterpolator属性表示集合中的动画是否都共享同一个插值器。
对于xml中各种数值的单位,比如80%指相对于自身的80%,该数值实际上代表了两种属性,一个pivotType和pivotValue,80%在java代码中对应于pivotType = Animation.RELATIVE_TO_SELF,
pivot
Value = 0.8f。另外一种是相当于父布局,比如80%p,此时,pivot*Type = Animation.RELATIVE_TO_PARENT。

Java代码如下:

AnimationSet animationSet = new AnimationSet(true);
                ScaleAnimation scaleAnimation = new ScaleAnimation(0.1f,1f,0.1f,1f,
                        Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
                TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_PARENT,0f,Animation.RELATIVE_TO_PARENT,0.5f,
                        Animation.RELATIVE_TO_PARENT,0f,Animation.RELATIVE_TO_PARENT,0.5f);
                RotateAnimation rotateAnimation = new RotateAnimation(0,360,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
                AlphaAnimation alphaAnimation = new AlphaAnimation(0.3f,1f);
                animationSet.addAnimation(scaleAnimation);
                animationSet.addAnimation(rotateAnimation);
                animationSet.addAnimation(alphaAnimation);
                animationSet.addAnimation(translateAnimation);
                animationSet.setFillAfter(true);
                scaleAnimation.setDuration(3000);
                translateAnimation.setDuration(3000);
                alphaAnimation.setDuration(3000);
                rotateAnimation.setDuration(500);
                rotateAnimation.setRepeatCount(6);
                mShowAnimationTv.startAnimation(animationSet);

上面的代码是将左上角的textView移到右下角,并且伴随着旋转,透明度,缩放的动画(代码实现时要注意set集合add时的顺序)

实现原理

首先,从动画开始的地方AnimatonUtils.loadAnimaiton()方法开始,源码如下:

public static Animation loadAnimation(Context context, @AnimRes int id)
            throws NotFoundException {

        XmlResourceParser parser = null;
        try {
            parser = context.getResources().getAnimation(id);
            return  createAnimationFromXml(context, parser);
        } catch (XmlPullParserException ex) {
            NotFoundException rnf = new NotFoundException("Can't load animation resource ID #0x" +
                    Integer.toHexString(id));
            rnf.initCause(ex);
            throw rnf;
        } catch (IOException ex) {
            NotFoundException rnf = new NotFoundException("Can't load animation resource ID #0x" +
                    Integer.toHexString(id));
            rnf.initCause(ex);
            throw rnf;
        } finally {
            if (parser != null) parser.close();
        }
    }

最后会调用createAnimationFromXml(context, parser),此处使用了XmlResourceParse。

private static Animation createAnimationFromXml(Context c, XmlPullParser parser)
            throws XmlPullParserException, IOException {

        return createAnimationFromXml(c, parser, null, Xml.asAttributeSet(parser));
    }
private static Animation createAnimationFromXml(Context c, XmlPullParser parser,
            AnimationSet parent, AttributeSet attrs) throws XmlPullParserException, IOException {

        Animation anim = null;

        // Make sure we are on a start tag.
        int type;
        int depth = parser.getDepth();

        while (((type=parser.next()) != XmlPullParser.END_TAG || parser.getDepth() > depth)
               && type != XmlPullParser.END_DOCUMENT) {

            if (type != XmlPullParser.START_TAG) {
                continue;
            }

            String  name = parser.getName();

            if (name.equals("set")) {
                anim = new AnimationSet(c, attrs);
                createAnimationFromXml(c, parser, (AnimationSet)anim, attrs);
            } else if (name.equals("alpha")) {
                anim = new AlphaAnimation(c, attrs);
            } else if (name.equals("scale")) {
                anim = new ScaleAnimation(c, attrs);
            }  else if (name.equals("rotate")) {
                anim = new RotateAnimation(c, attrs);
            }  else if (name.equals("translate")) {
                anim = new TranslateAnimation(c, attrs);
            } else {
                throw new RuntimeException("Unknown animation name: " + parser.getName());
            }

            if (parent != null) {
                parent.addAnimation(anim);
            }
        }

        return anim;

    }

最终会调用XmlPullParser,基于事件驱动模型来解析xml文件中的动画,从代码中可以看出来,每一个节点都对应一个相应的View动画类,这样与实际写代码差不多,在Java代码中也是先创建出相应的view动画类,然后按照顺序添加到集合中。

你可能感兴趣的:(Android动画(1):View动画)