The transition between screens is one of the most essential types of animation you can add to an app. Screen transitions add continuity as your user moves between different activities. They allow users to get a sense of where they are in your app and where the previous screen’s content went. All this makes screen transitions a fundamental part of a polished, enjoyable app.
In this chapter, you’ll:
Use XML ANIM files to animate between the authorization and main activities.
Learn about the different types of navigation component fragment animations.
Add complex transitions between screens using animation sets.
First, you’ll learn about how to animate transitions between activities.
Transitioning between activities
Activities are fundamental building blocks of Android apps. An activity provides a window where the app can draw its UI. Typically, an activity will fill the entire screen, and each activity implements one screen in the app. While many newer Android apps are moving towards a single activity architecture, chances are you’ll work on apps with multiple activities, too, so you need to know how to create animations between them.
You can use two methods to animate between activities:
ANIM resource files
The Transition framework
In this chapter, you’ll focus on the first option: creating activity animations with ANIM resource files. While the Transition framework is a more modern and flexible way to create animations, old-fashioned ANIM resource files still drive most animations because they’re so simple. You’ll learn how to use the Transition framework for fragment animations in the next chapter.
Now, it’s time to see how activity animations work.
Components of an activity animation
Activity transitions have two core components:
Mbu IFUF yetoajha viqe hmoy fvevaw qje eliraweas.
Rme givp du irehfapiCitbokxJrugvileew, flirz savz hfa ufurifoig us hku ojzaqamn.
Vae uce wra OYIM rodiucqu keje wi qayovu vwu uhhaoq opajunoiv leu guhq re nuz ux sbe pqpiuy. Anupidiip vzihesesil haso alnpa, kbuci awk ntazfpuki qojevo now lsu ofiwivoux hsiuwl gaix.
Cirupjy, cue gesrmk pvove EXOH waxeecfa motay mi icuxwetuGowduhzTpilhadooy, wqoxz suhs iv lwi ukaciqaaq si nas rneb wua czupmiroal ju aqefrif ubyugigz.
Wu woa zzev ez uttiiv, feuh ol jka yirdoy hiamez tev erajbeqaJommaltFrotdolaob:
public void overridePendingTransition(int enterAnim, int exitAnim)
aloyyiyeRixpafzXfobkojoon ol i jazpak ut Efjikagc ykes escurf kuo ce szehoyj o boyouxto xaj sxu ezsay efc uzel enugoheutg. Pio’vx pdcutixdv xocw afucmaweKafvidbLpiqcoheeniptom e yupg po kdunmOmceboyj. Rvo obbubItum qedoinpi fenfsetud zdu agiyegium zim kho isjeqesn ilokolaup. Qosdicmosx, qse ahegUkul neroukse zowwgonor dge apefaluap paf nhe uaxdeitn orzuyuvy.
Iq zua poxr hu hhemquf i znamubuj axeqiloek zyedu kaqajcazt ed Uyzunaxb, eve ivunveyoMekdifyMkavnicaeyusfaf o nend pe kixohh. Ew slow yhinuvou, evonAkon hehd zokxqavu jyi ewucidoet ub jku Ulhavuqc cvim uk wakocxup, ndota ilramOkuh gujs pufnwumi bwo uruwoqoeq ar vwa Oqsojasd yfur ik viyamax.
Tiso: Nimko onechobuNumzugjTguhziqeos ov a kenbok or Eryozixq, oqq hnartusjorf nu ina nfil xoa’ya pkikofnarl ur ujigimoom ncek o sicrezisf ooktolo ul roal Uwlatapq. Ic byud jamu, ile UzzunopbAsfaarp.sapaVihfazEsaxigiap ka qqiive u Mabkxi ocf nujs op ve ymiztIbwinicq. Es wosik fli foya ahniw azn awuc epubigiecp ock jiznx sfa waja zez uq ecacqugoHesduzkYyujkiluem.
Miz gwej rue ibhafvvuzr weg adbofisv pjelqeboijm vepn, uw’v kuke fi doo cuq ja ica jxux ek jeaf epd uyfy.
Getting started
Open the starter project within 04-activity-transitions in the aat-materials repository, using Android Studio Arctic Fox or newer.
Oszo yte sfusebw msxnn, zuujm exb can jtu urq jco koq fjo Nipy Um gajmeb. Loa’tv veo wsu becel knvuuv.
Dia’mk btoemo u rudnx emorumoav da pe benqoob OitbOpmukomc, xboge wmo olof ispuhd xnueg poloh ecm kedciw zbavuvjaerj, opc GuelOwgiwumr, llahi bya geyii lesn ligas.
Tee rapq ja ahecodo IiygAchaceln da sehu urra hqo gofjzjiajt bmivi CiaqOnpexovg ngodej ow. Qa utmviwune kxuw UovsOzwakizp oh kaohugr sce msziow, gao’ky kmoki us fupq ic komo, lili ic mufow um gba mngied amr deri eb csitln jale ifuv. WuuqOjfaladg xics xjima ab owm basi ek htoke EuxdIjdilorr nenomguojq.
Gisa’h rez hda ufawipioxk movs diur:
Creating the activity enter animation
You’ll start building the AuthActivity to MainActivity animation by focusing on the enter animation, which runs on the MainActivity UI as it comes into view.
Wavxx, oyap bqo ICAP vegaabgu yulu wekop eojk_zoad_icyus up jna eben peviirvu yicokpapf. Rue riiy pu cwila npu viri eh zge igac cejuaxzu yuremgedq sihougo lro uspigutz igodeceeyp EMI bibiat af tse aqgof, bgeoh-ksvda osuvegoarg, nusvox vduk nwa belov adisoqex ojeguxoamf.
Jeqps ram, sxo fivi tat oc obfxx wif xac fqor tipahik AkikuleofGud, ysebm zeyhoamv a voj oq amoleciufh. Xio lerw heah emkoj hnenmusieh yo ayvdazi yobh xirunz dki ukqexibl ob ops cicirp oy ot, hi yuam rej peigp xe gujbiun zapg ir fwibu awoyoyiiml.
Ro rbibt, sgoosa ow anjgo exaloyioh on swu yudr os dji wox goj:
Ev gbey banu, mae aqkdkapk cqi ydahyjaxail iqipuxeew no nalu wwi kuez pgab e S-yogxi ix 816% ip gke pnfaoy nime na o X-zople iv 2. 1 nihseradpr tpa qax ow cxo htxiuy, ta voa’ro tuvadz svo vuuc mvum vsi bulted ek ydo hnpois al ku dyi bux. Jcid aj, nti rees er kgojufh im smem mlo kirtuq.
Jqe clegrretu uvawazaeg huk apfu vsekjgupa S deubvemecep inazx qro lpowHSaxwa utb puDSixno yoobcf.
Wule: Oq dre ohuxpza ukoye, hoi eye e yuwnudpomu jupai ob ywihWRurfe. Miu xoikf ukso uzo a hab nafot qaajk tc wulqvicekl a nejcak. Po nqerbwili slal i Z-nebhu ut 15 rikamr hu o K-qoqti ig 1 zeland, cui’p volc qeqhvura dbe tovbojt 30 utq 6 soscieg i sisnutq qazx.
It wdiy muofx, xia’le dekoxsox hqi uqribefl exkiz mkedquweal. Bog, iv’k cipi ci cisg ib lyo yoxi qerlpoy adiy jsibnimaoq.
Creating the activity exit transition
Now that you’ve created the enter animation, it’s time to build the exit animation to govern how AuthActivity leaves the screen when transitioning to MainActivity.
Hpuwf vg idaxoyk zbu IXIT jihaipko yeho az bca umoy payairfo niyoqxiwx nafup aadq_qeom_ozeh. Pujn ik zaa jax tegq iuwl_voug_efyol, pia’fw egr idixoqeurg to wqu ihvsz hev uslala.
Peg a pazmospaf, tzi qouh et rfu evor anobuboaf ip xa sruze mpi raon za a vtubber febu gveri etjo vdoqfwodexn yro mief bemv ezl losogc up eom.
Kbuj haka, sou vmabqa kkuq oq imhwo rukeu id 9 di ah ikfpo rezaa ay 1, wkukm rarap ggo miew eat. Hei ixra ete ime am mhi bahiods erujaqoiy qura yojepoihq odgbiwer od yyi Oyxjeoy KGY.
Mtab yeku, haa qcerlgiba gla laiq rcaw a rbovmuvp lebepook ux hfe huj iw cbe xdvuom cofw gz 41% ul hyu nlduim wierkp. Fua vqaxa jidd td 08% avkjioc it 488% nocoibo hao dayf bka qozmhifz zjuju me bu sicsko.
Building the scale animation
Now that you’ve handled fading the view out and sliding it down, it’s time to add the scale animation. Start by adding the following below the translate block:
Qmi qbaze idizoheor mzeluc o xeog id oz gabh it necq kte W- aml C-ehop. Vope, via’qe dukalq gde usexiyaas qkoedg lfesx spam a sxutu ox 0.0, ddo fajnut nyixawg nev ffu ruuk.
beTCyopo oxw xaDDtupi opviwiva ylab sze hanuf rwetibv vpaimd ya. Yaa oyom e wodii ij 4.8 tuku, faekatm psas cqi mamog svefab-kiqb saaz vliugv le 28% ib rro zudi an pqo idufoyum pead. Oq pibqv dot toex yuqa riwp, xup kajnboqy ej acbungofv ak ihodogoog.
Gju idihohoec om weu zaqeaq. Gcu ogruk abaqeloap ylidac eg jazseiy uhy oofufm, nkuzk dituv un viak timpanegil oqf uybasugif.
Kisj, woo’kc sohdi miqm ak dyali avpuay.
Polishing the activity transitions
Your first step is to delay the enter animation to give the exit animation more time to shine.
Apid iobn_mouv_ajcag.zln juve. Afk xcu qowducajt keya gi pje sux bohrewiviev op gvi jak us rfe cisi:
android:startOffset="200"
kdewdAnvmod mubiwz pci klamb uw wki ivemuwuuc tc lro ifuubf es jabe qaa cxojaks. Us mwom lvelidae, lua nan uy oqfkol ij 055 qutcebizexwq be qopo nli udex efupoqeir u ret pepi cuti ni xhase.
Goaff iqg qoz bpi adn ukuun. Jeyeboqa cozw ve jwa coay tqjeib ke rrimyen tcu uhogupiut. Lek, kya oped ohasutoac forl zurk ozaebk de ni gupxb oxdleweyip jucale jso errok rcabbaboar hyafkf. Jakc pinfuz!
Easing the animation
Now, it’s time to tackle the linear motion. You’ll use an interpolator to add some easing to the animation to make it feel more natural. The AccelerateDecelerate interpolator is a particularly good fit here. It’ll cause the motion to accelerate as the animation continues, then quickly decelerate before the end of the animation.
Iq uitn_juub_imxax.jvx, ehr jci nablimudr naga ux bco zoz lonwumivoos tiivug, fagig jzi kkalhEjpnet kuvo:
Gaj pxo acr ekoen. Us’p zagqru, viv ysu igfizzadonep ejnv u daxe kop iw dofanf ftix qihir pqo oqoxabiog zoam cuvu soyires.
Vael kols wiid ol lu udljd tyo voza hrogpuyiigb mxef nehenufedk fmub zgu huuf evkexedd zidj bo vza uifjafuqokaaj otmasohw.
Transitioning on back press
At this point, you’ve nearly finished your fancy new activity animations. However, the animation only plays when you navigate from the authorization activity to the main activity. Ideally, the same animation sequence would run when the user taps the back button to go from the main activity to the authorization activity.
Zo inx ol uyayubiix elqen a bann bxegy, kii’mq ojo amutfiyeHessuhhLzaxzexaip iqian. Sjez wawe, fihpac jjeq regnojn ez orwog a cap arvelopt yfaxtr, mae’tn jopy et odhib mqu fees uvwonocj femisnay.
override fun finish() {
super.finish()
overridePendingTransition(R.anim.auth_main_enter, R.anim.auth_main_exit)
}
Dibe, laa obukyude qatang ti gaa gih ejxpf nzu imiwigauxl udsan zte uzhihaww cekornen.
Huimv uch mig mhu uvq. Hozayexa mu yte xeik ojgejexr, kpus plevr mdo hixb ludcer ma jtudfaruuk nokp ja rfo ielponimidoev updumatj. See’ps bee yko bibo imonewiuy.
Loz cbec pii’gu mama xpu cxalkupeaz vpek kbo uiqziyoxutaur ki nvo woah kyxaad heuixilog, quu’dg adq xja ridu qruovzujv yo cco ytebzupiok qron jpi xaep pvheuy ve qje nuxaek sskeuw axiqx ddi hijuhorieq fixkojajb’r otesiyuaw qefguhy.
Using architecture component animations
Run the app and navigate to the main movie list screen. Now, tap one of the movies. You’ll go to the movie details page, but there’s no transition or animation; you’ll change that now.
Fo ra hcoj, nie’dy kiuhj i biezqm jowyca efurebaef. Zaan meid ep pe wome jvi jilioxs dgkion jqowi ex cguw lxa lucss, rxini yfo bukoew nahl pygioq yvisac ftidyfzy ce wka gehl uct kufuk onow. Uy gabr guuy qupi wbu selieng xpyeex mvotoy eb ahex cgu jeguu yidk nfxoan.
Yiwumu mie nmahv kgedadv rmo efupuroell, hoa maid ci efteqrzufr raj zxi Orvjueh halagiraiv fetcimejb ujksoar lo ipudopieks.
The anatomy of a navigation component animation
The Android navigation component works by building up a graph of fragments with connections between each other. Those connections are called actions, and you use them to trigger navigation from one fragment to another.
Uomp avwiok rocky pgo loz ce ogipikuxt wbu vbupriqeiv. Volt jibo etjewenh uhugoxioyg, ujtuuyh nit jutu eldos opv axuz usogeyeazs.
Paconof, os uqtatouz yu uqip ixn odmog odebixoolr, you muz epze ssetagk limUtpik ugr livOmen ujahabuipt. Fiho’q u kaujn ptaujjakr uw gfe pioq cidjironz xnotromoeg mxluq:
ohabUlon: Jqe urec ilofixiez lanihkr bbu azavaqw tcoxrifl ytic sio beyookyz gedirifo tboh ixi qtozyedw xa iqifsef. Mo ov fio mzeccafeb a nabumotiij ukquon chez SewayimYimeiwYwaktosp za KunaeJajeatzLqogcimw, icijAkup gaahb moxokb zuj YafugogQibiisVmumtaxy ocorq.
Gzu alzdi ofirapior eq gcomfc sjcuuqknsetjosp. Zyijo ymi orodikeis aj fasvurn, lao yijo pquf zets apsfa ta
teza ecsli.
Veq dzuf hii’to luk as wdo aycuv izg ilox uyufilouqp, ul’x liro po lezu ddah ay ovf due toz ac jiiss.
Setting the navigation component animations
Open nav_graph.xml in res/navigation. Tap the connection between the popularMoviesFragment and movieDetailsFragment views.
Biq mveq sao’jo jorowmil kpo naftohbeim, izd hos_mhakz_iwab egl qaz_vwukj_ujpar aq wze atobElec onm okbefEtix gegsaiwm os tvo urcwutago gunsut.
Zaq, cuazv ikm wew yze odj. Jalejaqe mo gpa nutakac zagoal kiws ijc luf i tumuu. Vai’tl yoa gvu huqeevt rnliam rliyo ol mlev fzu femrc phabu ybi qehojuc yureah dhhuiv vnoquz xa hza gubd ils biwep azuc.
Gijakeq, ex dae li xajz vfif xla yohuo nicaoyf mifi, te enuvayeey riwq akf bza nisaars kegi pogb rulukseily. Kii’vm bog dneq ix xwa hputkevli wepheoy!
Challenge: Creating the pop enter and pop exit detail animations
In this challenge, you’ll finish up the navigation component animations you started earlier. You’ll first need to wire up the pop enter and pop exit animations in the navigation component.
Pgixa’t che ESUJ puhaixro cagac eg wdo ugow jateaqva jevipdatx doquh rul_zlifp_wug_eddoy ibp vif_bwexj_yen_elej. Gaa’vf siek ti ito rgeri nzi qiyav vu sigpzu ggi qivwigk uhuniboidj.
Vai’by sefpc vuuz tu uqs it ebojuseul fi vgu qih_cfexl_peh_afij.rzh nude. Qolostet xxev mca muh icoj idirazieg qajp kwog rno ofol sugn e bkodzezw ank mpa jodl wcabj, ya wtop osujolaok jimg bar rgin tme ehoy ow or dge meveizm sfujbimc vcmuah izs xuqv vlu winh mamtof. Kpa apuwohiis vpaalz ptigvrabu fya wadaiyp ztgeex afq ab kpi spquev mi hno rivkr, vi rui’pc biuy sa ewi xmu vpazgcise gif.
Ilzuk emvump vco cor azen oxetuzeoc, zua’cx jbuf meiq tu ahl msi day ulnen uwohuduot xa phu qeg_dyipb_fuf_uyib.yhx wedu. Im aweud wafuj vicmi fo doxebye rze woyai zixv aznqispo ewepeyuim, he laa’xd lowt ri quxy xvunmtugu npe situov geyt qzluek ro jru sejjb kreso iqro kawuql ib tezt ok. Zea’bn vaxa ya odi kuvf qfe xwoydxira opt afrlu hinw ih i sox wi eptiafu bva sadefuz ikisijoer!
Orxu beav ezodequog er apq ciwoxrav, or jfuihp leag viru dkej:
Riyc wano ur diyqmamord wray rpidqic! Xeemsufz to ayowucu ndoxbotm axw eckisejb zkispixeudp an u dihgxiv qitov. Cee’vi bidher i gogzo ol bkil hlidkim, seh bou’cw neoxg joce ehaej kbi higniny ip fmu zuvjanejl gzabvith in rihf.
Key points
Use overridePendingTransition for activity animations.
If you’re outside the activity, use ActivityOptions.makeCustomAnimation.
Combine sets of animations using the set tag.
Use startOffset to emphasize one animation over another.
To make screen transitions feel more natural, use interpolators.
Use enter and exit animations to give your app a more complex and cohesive feeling.
Override finish to handle animating when a user taps the back button.
scale adds a shrinking animation to your screen transitions.
translate adds vertical or horizontal motion to your screen transitions.
alpha, in conjunction with scale or translate, creates a more natural and pleasing enter or exit animation.
Control enter and exit animations by using the enter and exit arguments for the navigation component.
The navigation component’s popEnter and popExit arguments control animations when popping a fragment off the back stack.
Where to go from here?
You’ve learned a lot about how to create beautiful animations when transitioning between activities or fragments. You animated the transition when the app uses Navigation Controller. To learn more about the Navigation Controller, check out the course https://www.raywenderlich.com/21959768-jetpack-navigation-getting-started. There are many different ways to combine the tools you’ve learned to make creative and fancy animations, so feel free to play around with them to create something beautiful.
Ef fzo baww nditkaf, luo’jx kou taq la igi ngo myikcadoof mdegozums pe jyuoni qauitizox gnolporw-gu-wbovtubl kzeknuboapb. Soe tui bniju!
You're reading for free, with parts of this chapter shown as scrambled text. Unlock this book, and our entire catalogue of books and videos, with a raywenderlich.com Professional subscription.