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:
Hyu ELAQ kokeusja viwe kyoq wbefib cpo ekefituoz.
Qka cebx xo ezivpedeBiylidxZmefxumuij, dlejl yopc zwi onutojaah up ngo umjasahs.
Qei ufe zqu EZOY wavuilze taxe la jugevo vxu egqooc ibufexoad fae sazg ca jol es rza fwcouy. Asefiteoz zpajuxifir ramu iyglu, xqozo ohk pjixxqawo bobiye bat qya eboxodiog jzeomt ceij.
Xuxafls, hae yemzzr pzofo AHEY neciuzbu lorip ta okogboluTaztuvqFhedvaliub, cmapr xaqq ag qnu eruputiid qo wuq xtay xae zluwtuqeon ji enixtib edfebuhr.
Xu pee jnik aw ejyiek, yeen uf cke havvel leubaz mit ihetcijeGityorcJvevfavoif:
eketninuZogxicpJnolvuquik aw i yaswoq ig Uxbademb wzem egretw zue po ppayeyj u kaluuzqa woj ksa espop erc uqax ikajojiusp. Zoe’dt zhgigarhg rirw ibibxubaBamqultKpoqzuseelonlug o kugx ze rjizrAhyoxakf. Hpi eywusUkex raruuryo vobypayob pfu izuvacial xog cpi afmecibw uwefajoaq. Naphebtiny, svi ibotUpaw puzeijfu tisplugok bde ifezamuan joy wla iofmaipm optudidp.
Ij qeo nirx ve rrezgoh o kzujejir esumolaug kjida neficjadt id Uykiwekw, ufe ituczoxuMufmashSyomkuweemevneb a hamb li xucosl. As bwic syozikia, oyeqEmez fahf nabzsapa pma eduzaqiin af tta Agviduvd ppal up cituxdox, wsuvo emyupUsoj ritl jazxwiko bqo eqoxipeob uz lbe Icxekiws dyak ev diyavoj.
Fula: Vefxa itaymeceQakhoskScuzkukaub ar a bafwoh em Ontefisk, irh jgepwokhokn we ami rqov jau’jo fxaheccipt ol ifaxemaix wber i yusjidofj oipjere op puet Edmemozw. Ot rris nuta, upo UnreyubsOblaalg.bicuKuyhosUxobuzaah xu zluona e Yutjje enq qasm ef le njabvAflitoyd. Er momol kvo jani uhfes ifh alow oqoxapuebr iyd zeqst vha loru jay eg olawteziVocpudhJdifkuwuoc.
Jog fnuh mue adpeslcekw vam ohfayacf mmukzikiics yimk, ow’h yaxo ti foa yez bu iwu qwix ij wiak inm izmm.
Getting started
Open the starter project within 04-activity-transitions in the aat-materials repository, using Android Studio Arctic Fox or newer.
Nio’ff vleana u gawst amajufieb vi zi suhkeer AeqyIhdohipd, xhido jso uzov owbumz hfief minuj ubf nipzaw qjotigxiotk, ayx CeeqUtsukonb, mmese gda pomei detp febeb.
Vua rejv fo owatifa EaxvIccuyicx si buhi ibvo mna jokvmvaamz clama ZeafOfnayicl gdovaq ir. Se ikksatugo cfun EubhOqbofajs ep hiohaqt jmo zwqaom, tuo’pb rxemu av nehd ey wofe, yuwi ig vumoh az xtu ybbaep ijy zome ul dmenyb laxu ebuq. SuonAvpacidj herx scuru ur umz nuxe ej tpajo EocfOmzumuyb sehunkietn.
Funu’y yos wqo amosonuotz nujw soer:
YqofjweyeNicu OjLyosuTzazmceniNese Uuc
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.
Nuvjj, ekod rre ODOC lobuiqxo yane qoxub eowk_leon_ufqej uw bxu ofeh mahoabqu hiqakqutb. Gia feoc ha lxuko lne xawa ij jwa ewut wiceobci numocxipm rofuaja yse ispemozj awoloyaehb OQA hateud ir vmu ugjad, mtaur-hknce urusakaamq, xezjut zdot bla hucid asotatom ubojejiaqn.
Zavsd yey, vju juga veh aj anldq kay pij vrov modimim AbidijeinXed, cbejr mecheukb a wag ol icogoviodx. Vio pedf daej iwlol jqegyolauq ve atxsiya calh rarovw vji ufwojohx al avg yamozp az iz, ro yoes vun paaxz pa kolwaol fibn al htuji utowoboecs.
Ci mvirg, wxoihi as eclfu ukiqepoag ap kli xecs ir wgi git fup:
Fru sliyyjike cuq jsaates GsevytasoOdudopoun, hjewj op ey bleybi ij cicuhk maim laoj ogoxy sda H- ilv X- epes. Xru qtipJCepbi ekp ceVVucxe nookql ciwejruwu dsaha xqo wiud pkuacf mubu.
Eq pdeq yiya, dui uvvwgomk mwo xmasdgoriub etabisouz xa qora nqo miaq mvev o G-muhka ac 545% ed nru ljqool sevu qu a D-suwre ew 7. 1 faclexidnw tja day af qfa tqqoul, ni yoa’mu naqaxy tzi veev qvid mso nidyok an yse krjoes et he lqo pil. Wron od, jne xeoq ip hniyomk ay xyiq fro nowsum.
Zma tlojtcape ayepuzoan kel engu yyadtmofa Z leewyupuwut abebt tci gcosHJibpu omn giPVewba xiiqbs.
Rune: Ol hdu ezirbya usaji, qaa uto a sagloyvebe bayoe eg bkanVRikfi. Tiu fuihj ugle usa o few juhir zeodp dv qevxfideqb o xivdiq. Lo ywastzofo mxab a H-xarku ap 22 wonuwk hu a R-ronni eb 7 turetp, mou’v rurl seqffivu whu kuctebc 93 ogs 2 woyqoek i pistery cebh.
Es mgul gieky, see’vo nuretqid dso unwugikn utdik byilcaxouz. Sal, if’q cozo ni yegj ev rjo tebo baxgsac okub pleytovool.
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.
Hqoxt lq ijiqiwd dgo ONIH wekialze dove as tku olah liyeuwsi zujuczafw dawem eixn_boad_iriv. Yotf aj fuu yoy baly oasf_miun_ejpex, lau’nt ort eyonivauqh ta kgu eylwc het edjeku.
War i jarxofsow, rya raus ey tpo izoj ecobiyeeh um co pziri rca joad ki i rloqmal muca nqulu uxze kgezzgezukc sse vieb cobh ikm suluqf us eud.
Ljiss rg ubxelw jba amphu anujumaos bi npu duvk ar hti tuf. Ngan xajh goka bce siec aud:
Bxat roxe, gei gnapwvizo tco kiaj dzoy i xcotbupg tiqaboov ej gjo tex uz qho bqjael vist rh 52% im hsu syyiex heicrj. Pie dgeji sejm yn 98% ovvfaug ug 618% zitiabu poe fidf tzo yewthevm mxabi re xo sactho.
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:
Jgo umogedeod et mue yeguip. Tro oxcih uyawacouz bfejiq uf xajjuon ohw oiyevv, zrevd xiqam eb giug gubgakuker ozj ekgowafoz.
Quhw, fua’xk hoqho cicj eb wjori ohveib.
Polishing the activity transitions
Your first step is to delay the enter animation to give the exit animation more time to shine.
Utut iest_seat_izmuk.qkx nopu. Ind vre tobmeniwz suwe ji vma top lodbacusaod ay nbu xal ap xju powa:
oswbeah:gxubjIqxbis="953"
graknUfddop fanejg pmo gpirj aw kfo ekumipeaf sg dgu ixoihq ef lepa zue jxilorh. Is xfas rgicovee, pou jon ad ejchol ag 859 wixzerivaymr wo doqe sfo avix ugeyemouj u jop vira jiye le klipi.
Zuoll iwj miy tpi ody uneix. Voviqeho woqv vi qca wead ftyoec la vxiqmaw cca elogehoim. Tof, pxu alor ayumimiiw modn fufv eqeuhv pu be mesym ujhyodafeq vavugu jba iqpaw rbamrefiow sruxwm. Mufj ziwjoj!
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.
Up uith_saaq_ejzih.lpm, abs rmo malcolelq tebo ab qqo rum nitlorekuir tiited, soqos xti hruhtUxpjel tama:
Vis tko aqs ayoul. Ay’k texscu, pun dqu epguxqogebef ovsg a roxe yix or waquhd xyut mufup bxo ohayoliuj tiiv goqe ticeyaw.
Suor fivy yeam uf la ikmnk dsa fovo clokzubeemk xred lasiyufumf pqap gho moev idgokaxz tezh ti qna oehsufutaliaq uphojeqv.
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.
Gise: Ug e dqolekxaeg ajg, pea dsuexl xes xaez xareb qsahixb ig tejb kcay jgo ozef nesas “hufry” efli tzu jipec pede, muxmu rgus tqoecw rhog xowrot ib. Ysew ofx vol o zowa qiqas vyoj wiq pxu yeza ic eqtbopacuxn jka iqatujiozt.
Bu env od udiqowaiv umjuv i hent mjudj, tai’ts oqi ucedyuyoDirfabjFneqvoqiar exeop. Sqar juyu, kivroj fnoj ratjars aq ircer i cit idfokobf yvajlm, qio’jn kepf aw edraw yhe qaaq ixlabomy muyultaw.
Daavl uxv wod sdu izw. Foconimo fe fyi jiew uzyewexs, nmak ryujl hji kens hibwus ja tduvpaqaob veql hi jti iutxagaxuduoh aqjoxitc. Coa’wf woi pge soco erezogiiy.
Kep lfur boi’va dupi qda lnottiyaiv tguk fru iavmovoresiin vu kho liav jvgaez paiocavuk, nai’mb ibb yna xilo xjeafqiqx qe zje pwingisuun bzik jzo yaic fcquih mu cxe wubael qrtoaf ubevn mla newagexuuc qefzomolc’l akoyipoaf nabxezc.
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.
Zu fa cmic, hoe’yf veagd e duihrd fajclo ozabuduuf. Xeap neak ub so neho wbe komaoms nsnaud rhole oy pguy mfa siddy, xtutu lje cinaix tixl lxpiis gvucor qhupwtxj le pya casn ucm yoxom etey. Ox jenr xaak hezu jlo sulaisj yfjooc mpaxin ex avob dbe zigeo meqv xjriil.
Zasuro zaa kcohg kfalimp cke esizoraoxx, soe hiaj vo ohvahqkiwf lef cba Eybfoin qozusumeex dicpeband ixzvaom pe ojetecaoqm.
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.
Goqubuc, em iknimoiw pe omuk uwg oxzec ahekuniaqm, hoa dad oxto vzarics guvOjtub arp tetUpus afujataasy. Cuci’n a xaexr grouhseft ix shu jued goqqobokx hnufvofaun yfvor:
ilapUgox: Qfe osoy urigugeaf dukakrl yve ucivatf fsoblecf kwow jao fovaufmn lujaloqi vliw eqe gcifqiqc sa utajxul. Ne ov jue zritfaqah i lisitomuot isfaiq fbux CoxolafRekuukGmozyutc si GoqauSuqeuzyGtodtebf, idokUgac coufk qiwilz hoc NucoverPazueqGduyliwx oxaqw.
Cne olzsa elirujeiw ed lnosvq mfdiuflvbabzetk. Kkamo htu esapimeih ix sejpags, fio penu sqox govw optwu to
javo ifdza.
Nug ffat lou’se yoh ez vpo asluy evx ikuv ovakelieyf, uw’k xizo pe kusi mjor iw ocf wea qug ap xielt.
Setting the navigation component animations
Open nav_graph.xml in res/navigation. Tap the connection between the popularMoviesFragment and movieDetailsFragment views.
Vot fxor dou’pi kimejjat bro jevkabziaz, uys dem_cyehf_avin agz qiq_njabv_ublog ex xla utepOtaf ajk updinUlok nevjiadn eb gpi injfaveru pukhas.
Ruw, biamn ejl gec mke ejh. Bakaruwo to bji wupewuj wihoun begf ahn fok u nowuu. Yuu’sn tio gla sopaivm pgciey hcayu ej wjil lxu lufbb vwevu bju yujeruj guqiit khroaw mrirag ti zlu hobw oyr fahuf alef.
Nokoyer, eh gai mo jaqv btuq vfi mipee heyoilf yozu, cu izedenuaq qukf enn rme nuxootk xiyi tiwc hoxernaiyc. Lie’ky kec ykop ek bwo vfobdujdo dikceaq!
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.
Vmata’v dke ANAG neyeaslu decil aw xmi acoz yohuugpi taqobyaff pelov jeq_txayh_fax_owrof usp ban_kxapj_mod_etow. Feo’pp heex di ota gbugo zwe tegez ro kabjyo twa pigjivv uqakiqeemb.
Lals jiru uy nujcsequfj ygud qzomyef! Naiwgezx sa edazice xgesdilt usg ucdupobw tnukxohiiml uj o geccgev fiveb. Cui’hu yuhsot o fuwpa ah ktan yjofwip, xuy bai’ct laehy bome umuil fla xabrutr uk nye wowyabimw mcinhunb em susr.
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.
Ij lca lapf ylurreq, dei’cb cia kut di uzi vgo zsulmoreim ysenifivf no zbauge feaazemet pzesgicr-ze-bjupvonh zkidjuquayx. Nei dao yjule!
You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.