[ Android ] SVGでアニメーション(xmlで定義)

SVGでアニメーションをxmlで付ける。
やり方は色々あるようで、簡単そうな実装方法でやってみた。
Android Studio 3.0
Android 7.1

0.SVG画像を用意
前に作った丸を使います。中身はこんな感じになっています。
(回転を考えてなかったなぁ・・・)

<vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="24dp"
        android:height="24dp"
        android:viewportWidth="24.0"
        android:viewportHeight="24.0">
    <group android:name="circle">
        <path android:fillColor="#FF000000" android:pathData="M6,18c0,0.55 0.45,1 1,1h1v3.5c0,0.83 0.67,1.5 1.5,1.5s1.5,-0.67 1.5,-1.5L11,19h2v3.5c0,0.83 0.67,1.5 1.5,1.5s1.5,-0.67 1.5,-1.5L16,19h1c0.55,0 1,-0.45 1,-1L18,8L6,8v10zM3.5,8C2.67,8 2,8.67 2,9.5v7c0,0.83 0.67,1.5 1.5,1.5S5,17.33 5,16.5v-7C5,8.67 4.33,8 3.5,8zM20.5,8c-0.83,0 -1.5,0.67 -1.5,1.5v7c0,0.83 0.67,1.5 1.5,1.5s1.5,-0.67 1.5,-1.5v-7c0,-0.83 -0.67,-1.5 -1.5,-1.5zM15.53,2.16l1.3,-1.3c0.2,-0.2 0.2,-0.51 0,-0.71 -0.2,-0.2 -0.51,-0.2 -0.71,0l-1.48,1.48C13.85,1.23 12.95,1 12,1c-0.96,0 -1.86,0.23 -2.66,0.63L7.85,0.15c-0.2,-0.2 -0.51,-0.2 -0.71,0 -0.2,0.2 -0.2,0.51 0,0.71l1.31,1.31C6.97,3.26 6,5.01 6,7h12c0,-1.99 -0.97,-3.75 -2.47,-4.84zM10,5L9,5L9,4h1v1zM15,5h-1L14,4h1v1z"/>
    </group>
</vector>

イラレからSVG追加したxmlにgroupを追加します。アニメーションするグループです。

1.アニメーションの定義

res/animatorの下に、svg_anim_trans_y.xmlとか適当につくる。

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <objectAnimator
        android:name="circle"
        android:propertyName="translateY"
        android:duration="5000"
        android:valueFrom="0"
        android:valueTo="100"
        android:valueType="floatType"
        android:repeatMode="restart"
        android:repeatCount="-1"
        android:interpolator="@android:anim/linear_interpolator" />
</set>

name:適当にわかるようにでも

propertyName:アニメーションの種類

translationX、translationY、rotation、scaleX、scaleY、alphaなどいろいろ

duration:時間(ミリ秒)

valueFrom:始点(アニメーションの種類で変わる)

valueTo:終点(アニメーションの種類で変わる)

valueType:始点~終点までの計算の部分

float、intなど

repeatMode:繰り返しの時再生位置

restart、reverseとか

repeatCount:繰り返し回数(-1は無限な感じ)

interpolator:アニメーションの流れ、グラフとかそんなの

(@android:anim/linear_interpolatorと書けばデフォであるのを使う、カスタムも可能)

ここが図入りで分かり易い

3.SVGの定義

元のSVGのxmlに書くと使いまわせない?ので、別ファイルを作成する。

res/drawableに、svg_circle.xmlをつくる。

<?xml version="1.0" encoding="utf-8"?>
<animated-vector
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:ignore="NewApi"
    android:drawable="@drawable/ic_maru">

    <target
        android:animation="@animator/svg_anim_trans_y"
        android:name="circle" />

</animated-vector>

「tools:ignore=”NewApi”」バージョンによってはエラーが出るので追加

「android:drawable=”@drawable/ic_maru”」使用するSVG

targetにアニメ用xmlを指定、nameはcircleに。

4.layoutの編集

activity_main.xmlにImageViewを作って、drawable/svg_circleを表示

このまま実行で丸が表示されます。自動で開始すること出来ないのかな?

5.コードの追加

MainActivityにアニメーションを実行するコードを追加

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ImageView imageView = (ImageView)findViewById(R.id.imageView);
        Animatable animatable = (Animatable)imageView.getDrawable();
        animatable.start();
    }
}

実行すると丸が下に落ちて、ImageView内から消えるを繰り返す。