Android 資源簡(jiǎn)介(五) AnimationDrawable

AnimationDrawable 代表動(dòng)畫資源。Android 既支持逐幀動(dòng)畫,也支持補(bǔ)間動(dòng)畫。

定義補(bǔ)間動(dòng)畫的 XML 資源文件以 <set> 元素作為根元素,該元素可以設(shè)定如下 4 個(gè)元素:

  1. alpha:透明度;
  2. scale:縮放;
  3. translate:位移;
  4. rotate:旋轉(zhuǎn)。

定義動(dòng)畫的 XML 資源文件應(yīng)該放在 /res/anim 路徑下。

<set>、<alpha>、<scale>、<translate>、<rotate> 都可以指定 android:interpolator 屬性,該屬性指定動(dòng)畫的變化速度,可以實(shí)現(xiàn)勻速、正加速、負(fù)加速、無規(guī)則變加速等,Android 系統(tǒng)的 R.anim 對(duì)象中包含了定制常量,它們定義了不同的動(dòng)畫速度,例如:

  1. linear_interpolator: 勻速
  2. accelerate_interpolator: 加速
  3. decelerate_interpolator: 減速

如果程序需要讓 <set> 元素下的所有變換效果使用相同的動(dòng)畫速度,則可以指定 android:shareInterpolator="true"。

我們可以通過兩張方式訪問動(dòng)畫資源文件:

  1. 在 XML 中
@[<package_name>:]anim/file_name
  1. 在 Java 中
[<package_name.>]R.anim.file_name

下面是一個(gè)簡(jiǎn)單的使用示例:

首先是自定義的 test_anim01.xml 文件的代碼:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator"
    android:duration="5000"
    >

    <scale android:fromXScale="1.0"
        android:toXScale="1.4"
        android:fromYScale="1.0"
        android:toYScale="0.6"
        android:pivotX="50%"
        android:pivotY="50%"
        android:fillAfter="true"
        android:duration="2000"
        />

    <translate android:fromXDelta="10"
        android:toXDelta="130"
        android:fromYDelta="30"
        android:toYDelta="-80"
        android:duration="2000"
        />
</set>

之后是主布局文件的代碼:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorGray"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    >

    <ImageView
        android:id="@+id/imageAnim"
        android:layout_width="400dp"
        android:layout_height="400dp"
        android:scaleType="fitCenter"
        android:src="@drawable/img07"
        android:layout_marginTop="20dp"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="@string/start"
        android:onClick="start"
        />

</LinearLayout>

接下來是主程序文件的源代碼:

package com.toby.personal.testlistview;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    final private static String TAG = "Toby_Test";

    private ImageView imageView;
    private Animation animation;

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

        imageView = (ImageView) findViewById(R.id.imageAnim);
        animation = AnimationUtils.loadAnimation(this, R.anim.test_anim01);
        animation.setFillAfter(true);

    }

    public void start(View view) {
        imageView.startAnimation(animation);
    }
}

本示例的運(yùn)行效果,各位可以自行運(yùn)行測(cè)試,如果出現(xiàn)運(yùn)行不了等問題,可以給我留言。本文參考文獻(xiàn):《瘋狂Android講義(第2版)》

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,813評(píng)論 25 708
  • 1 背景 不能只分析源碼呀,分析的同時(shí)也要整理歸納基礎(chǔ)知識(shí),剛好有人微博私信讓全面說說Android的動(dòng)畫,所以今...
    未聞椛洺閱讀 2,735評(píng)論 0 10
  • android 動(dòng)畫資源Tween動(dòng)畫 tween動(dòng)畫 實(shí)現(xiàn)目標(biāo)對(duì)象的變換,如移動(dòng)、旋轉(zhuǎn)、色彩變換、拉伸等。 xm...
    天空下天的月亮閱讀 378評(píng)論 0 1
  • 作者:雪野 一箸入口,三春不忘。本出自汪曾祺《豆腐》篇中的“拌豆腐”小節(jié)。 然而這句話并非是形容豆腐,而是拌豆腐中...
    雪野閱讀 1,162評(píng)論 12 14
  • 1 今天生產(chǎn)線休息,所以感覺像是放假一般,整個(gè)人是心情也是很放松的,可以稍微輕輕松松的上班了今天。 2 和企業(yè)社會(huì)...
    冬冬Steven閱讀 203評(píng)論 0 0