Upgrade to Pro — share decks privately, control downloads, hide ads and more …

VectorDrawables & AnimatedVectorDrawables: Trab...

VectorDrawables & AnimatedVectorDrawables: Trabalhando com vetores de forma eficiente no Android

--
Slides of a talk held in GDG DevFest Nordeste 2016, that happened on October 22th, in Maceió, AL. The talk was about how to work with VectorDrawables and AnimatedVectorDrawables on Android in an efficient way.

Walmyr Carvalho

October 22, 2016
Tweet

More Decks by Walmyr Carvalho

Other Decks in Technology

Transcript

  1. anim: resources de animação, transições etc drawable: vetores, selectors, drawables

    etc layout: layouts de activities, fragments, views etc mipmap: somente ícones de launcher values: strings, booleans, plurals, etc
  2. "Desenho vetorial é o uso de primitivas geométricas como pontos,

    linhas, curvas e formas ou polígonos para representar imagens em computação gráfica."
  3. O suporte a SVG no Android sempre foi meio nebuloso,

    algumas libs “resolviam”, mas… ¯\_(ツ)_/¯
  4. cross.svg <svg class="icon icon—plus" viewBox="0 0 5 5” xmlns=“http://www.w3.org/2000/svg"> 


    <path d="M2 1 h1 v1 h1v1 h-1 v1 h-1 v-1 h-1 v-1 h1 z" />
 </svg>
  5. O VectorDrawable é uma classe que surgiu no Lollipop e

    nos permite criar um Drawable através de um vetor (como o SVG, por exemplo).
  6. O jeito mais simples de se criar VectorDrawables é com

    a ajuda do Vector Asset Studio, ferramenta built-in no Android Studio!
  7. res/drawable/ic_close.xml <?xml version="1.0" encoding="utf-8"?>
 <vector xmlns:android="http://schemas.android.com/apk/res/android"
 android:width="24dp"
 android:height="24dp"
 android:viewportHeight="24"
 android:viewportWidth="24">


    
 <path
 android:fillColor="#FFFFFF"
 android:pathData="M19 6.41L17.59 5 12 10.59 6.41
 5 5 6.41 10.59 12 5 17.59 6.41
 19 12 13.41 17.59 19 19 17.59
 13.41 12z" />
 <path
 android:fillColor="#00FFFFFF"
 android:pathData="M0 0h24v24H0z" />
 </vector>
  8. <animated-vector xmlns:android="http://schemas.android.com/apk/res/android" >
 <aapt:attr name="android:drawable">
 <vector
 android:height="64dp"
 android:width="64dp"
 android:viewportHeight="600"
 android:viewportWidth="600"

    >
 <group
 android:name="rotationGroup"
 android:pivotX="300.0"
 android:pivotY="300.0"
 android:rotation="45.0" >
 <path
 android:name="v"
 android:fillColor="#000000"
 android:pathData="M300,70 l 0,-70 70,70 0,0 -70,70z" />
 </group>
 </vector>
 </aapt:attr>
 
 <target android:name="rotationGroup"> *
 <aapt:attr name="android:animation">
 <objectAnimator
 android:duration="6000"
 android:propertyName="rotation"
 android:valueFrom="0"
 android:valueTo="360" />
 </aapt:attr>
 </target>
 
 <target android:name="v" >
 <aapt:attr name="android:animation">
 <set>
 <objectAnimator
 android:duration="3000"
 android:propertyName="pathData"
 android:valueFrom="M300,70 l 0,-70 70,70 0,0 -70,70z"
 android:valueTo="M300,70 l 0,-70 70,0 0,140 -70,0 z"
 android:valueType="pathType"/>
 </set>
 </aapt:attr>
 </target>
 </animated-vector>
  9. A versão 23.2 da Support Library trouxe o primeiro suporte

    a vetores, através das bibliotecas de suporte support-vector-drawable e animated-vector-drawable
  10. app/build.gradle defaultConfig {
 applicationId "com.example.vectortest"
 minSdkVersion 15
 targetSdkVersion 23 


    versionCode 1
 versionName "1.0"
 
 vectorDrawables.useSupportLibrary = true
 }