創建資源文件
在res文件夾下創建anim文件夾,在anim中新建一個資源文件,文件名隨意
編寫代碼
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="200"
android:toYDelta="200"
android:duration="3000"/>
屬性介紹
android:fromXDelta 動畫開始時X座標
android:fromYDelta 動畫開始時Y座標
android:toXDelta 動畫結束時X座標
android:toYDelta 動畫結束時Y座標
android:duration 動畫持續時間
初始化動畫
imageView = (ImageView) rootView.findViewById(R.id.imageview);
animation = AnimationUtils.loadAnimation(getContext(), R.anim.translate);
開始動畫
imageView.startAnimation(animation);
結束動畫
animation.cancel();