1.å¦ä½å¨XMLä¸ä½¿ç¨èªå®ä¹Animationå¨ç»ç±»
å¦ä½å¨XMLä¸ä½¿ç¨èªå®ä¹Animationå¨ç»ç±»
å¨å®ååºç¨çå¨ç»å¼åä¸ï¼å¯è½SDKä¸èªå¸¦çè¡¥é´å¨ç»ä¸è½æ»¡è¶³åºç¨çéæ±ï¼éè¦å¨Java代ç ä¸èªå®ä¹ä¸äºå¨ç»ç±»ï¼å½ç¶é½æ¯ç»§æ¿èªAnimationç±»ãå®ç°ä¹åï¼æ们ä¸è¬ç´æ¥å¨ä»£ç ä¸ä½¿ç¨ï¼ç±»ä¼¼ä¸é¢è¿æ ·ï¼CustomAnimationcustomAnimation=newCustomAnimation();customAnimation.setDuration();customAnimation.setFillAfter(true);effectView.startAnimation(customAnimation);å½Viewåæ¶è¦åºç¨åScaleï¼Alphaè¿æ ·çè¡¥é´å¨ç»æ¶ï¼ä½ å°±éè¦å¤æ·»å 类似ä¸é¢ç代ç ï¼CustomAnimationcustomAnimation=newCustomAnimation();customAnimation.setDuration();customAnimation.setFillAfter(true);AnimationscaleAnimation=newScaleAnimation(0f,源码按键梦幻源码1f,0f,1f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);AnimationalphaAnimation=newAlphaAnimation(0.1f,1.0f);scaleAnimation.setDuration();alphaAnimation.setDuration();AnimationSetset=newAnimationSet(true);set.addAnimation(customAnimation);set.addAnimation(scaleAnimation);set.addAnimation(alphaAnimation);set.setFillAfter(true);set.setFillEnabled(true);effectView.startAnimation(set);å¦æç´æ¥å¨xmlä¸ææéçææè¡¥é´å¨ç»ï¼å æ¬èªå®ä¹å¨ç»ç±»æ¾å°ä¸ä¸ªéåï¼äºæ çèµ·æ¥å°±æ²¡é£ä¹å¤æãå¨xmlå®ä¹å¥½å¨ç»éæ两个好å¤ï¼ä½¿ç¨å¨ç»æ¶éè¦æ´å°çJava代ç ï¼æ´ä½ä¸çä¸å»æ´å¹²åå¨xmlä¸å®ä¹ï¼å个å¨ç»å±æ§ä¸ç®äºç¶ä¹æ´éä¸ï¼æ¹ä¾¿é 读ä¸ç»´æ¤æ¢ç¶æè¿æ ·ç好å¤ï¼æ们就å¼å§å¹²å§ãé¦å å¨xmlä¸åä¸é¢è¿æ ·å®ä¹ä¸ä¸ªå¨ç»éï¼R.anim.my_anim_setmyapp:customProp2=""myapp:customProp3="%"android:duration=""android:fillAfter="true"/>ç¶åï¼æ们æç §å¸¸çæ¥ï¼å¨Java代ç ä¸è¿æ ·æ¥å è½½æ们å®ä¹çxmlå¨ç»éï¼AnimationSetset=(AnimationSet)AnimationUtils.loadAnimation(this,R.anim.my_anim_set);effectView.startAnimation(set);ä½æ¯ï¼æ±æï¼ä¸é¢ç代ç æ¯ä¸æ£ç¡®æ§è¡ï¼è¿è¡èµ·æ¥ç¨åºä¼ç´æ¥ç»æ¢ãé£ä»ä¹åå å¢ï¼æ¥çAnimationUtils.loadAnimationæºä»£ç æ们ç¥éï¼å¨å ¶ä»xmlè½½å ¥å¨ç»ç±»çæ¶åï¼åªè®¤alphaãscaleãrotateãtranslateè¿å 个SDKèªå¸¦çå¨ç»ç±»ï¼èæ们åå ¥çèªå®ä¹å¨ç»ç±»CustomAnimationä¼å¯¼è´å ¶æ¥Unknownanimationnameçå¼å¸¸ãå®æ¹SDKä¹æ²¡ææä¾è§£å³è¿ä¸ªé®é¢çå ¶ä»APIæ¹æ³ï¼é£ä¹æä¹è§£å³å¢ï¼å¾ç®åï¼åªéå¨åæçAnimationUtils.loadAnimationæºç ä¸æ¹å¨ä¸è¡ï¼ä»ClassLoaderè½½å ¥èªå®ä¹å¨ç»ç±»å³å¯ãå°å ¶æºç æ·è´è¿æ¥ï¼å®ç°ä¸ä¸ªèªå·±çloadAnimationæ¹æ³ï¼å¦ä¸ï¼OptAnimationLoader.javapublicclassOptAnimationLoader{ publicstaticAnimationloadAnimation(Contextcontext,intid)throwsResources.NotFoundException{ XmlResourceParserparser=null;try{ parser=context.getResources().getAnimation(id);returncreateAnimationFromXml(context,parser);}catch(XmlPullParserExceptionex){ Resources.NotFoundExceptionrnf=newResources.NotFoundException("Can'tloadanimationresourceID#0x"+Integer.toHexString(id));rnf.initCause(ex);throwrnf;}catch(IOExceptionex){ Resources.NotFoundExceptionrnf=newResources.NotFoundException("Can'tloadanimationresourceID#0x"+Integer.toHexString(id));rnf.initCause(ex);throwrnf;}finally{ if(parser!=null)parser.close();}}privatestaticAnimationcreateAnimationFromXml(Contextc,XmlPullParserparser)throwsXmlPullParserException,IOException{ returncreateAnimationFromXml(c,parser,null,Xml.asAttributeSet(parser));}privatestaticAnimationcreateAnimationFromXml(Contextc,XmlPullParserparser,AnimationSetparent,AttributeSetattrs)throwsXmlPullParserException,IOException{ Animationanim=null;//Makesureweareonastarttag.inttype;intdepth=parser.getDepth();while(((type=parser.next())!=XmlPullParser.END_TAG||parser.getDepth()>depth)&&type!=XmlPullParser.END_DOCUMENT){ if(type!=XmlPullParser.START_TAG){ continue;}Stringname=parser.getName();if(name.equals("set")){ anim=newAnimationSet(c,attrs);createAnimationFromXml(c,parser,(AnimationSet)anim,attrs);}elseif(name.equals("alpha")){ anim=newAlphaAnimation(c,attrs);}elseif(name.equals("scale")){ anim=newScaleAnimation(c,attrs);}elseif(name.equals("rotate")){ anim=newRotateAnimation(c,attrs);}elseif(name.equals("translate")){ anim=newTranslateAnimation(c,attrs);}else{ try{ anim=(Animation)Class.forName(name).getConstructor(Context.class,AttributeSet.class).newInstance(c,attrs);}catch(Exceptionte){ thrownewRuntimeException("Unknownanimationname:"+parser.getName()+"error:"+te.getMessage());}}if(parent!=null){ parent.addAnimation(anim);}}returnanim;}}è¿æ ·ï¼ä½¿ç¨OptAnimationLoader.loadAnimationæ¹æ³å°±å¯ä»¥ä»xmlä¸è½½å ¥å å«èªå®ä¹å¨ç»çå¨ç»éäºã