Android 動畫基礎重點整理

Ref:Android應用程式開發實戰(第三版)


Android系統提供2D絵圖庫(自Drawable包取得)和OpenGLES 1.0 高性能 3D絵圖

(自KhronosOpenGL ES包及其它絵圖相關取得)。


2D絵圖和動畫

主要在 android.graphics.drawable android.view.animation 有相關的類可以絵制

二維圖和創作二維空間動畫。


實現2D絵圖和動畫的方法

一、將圖或動畫絵制到ImageView上。此種方式只是將靜態圖放進圖像顯示控件。

二、直接在Canvas上絵制。如此便可控制動畫,定期去重複絵制本身圖形。


Drawable-任何可絵圖東西的抽象類

目前已有許多實現子類,如:BitmapDrawable,ShapeDrawable, PictureDrawable等等。

定義和實現一個Drawable有三種方法:

一、圖像儲存在Project的資源。 (將圖像放到/res/drawable下)

  一般我們常在JAVA代碼中用到的

mImageView.setImageResource(R.drawable.xxx);

  又或是xml佈局中用到的

<ImageView
    android:height=”wrap_content”
    android:width=”wrap_content”
    android:src=”@drawable/xxx”/>


二、使用xml檔案定義Drawable屬性。

可定義成xml的 Drawable 子類

以 TransitionDrawable爲例:

fade_practice.xml:

<transition xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/ic_launcher" />
    <item android:drawable="@drawable/fa" />
</transition>

在 MainActivity.java 中使用該 xml 的相關代碼:

        // 利用 xml <transition> 定義好的
        tran = (TransitionDrawable) getResources().getDrawable(R.drawable.fade_practice);
        ImageView1.setImageDrawable(tran);
        mCurrentPic = getResources().getDrawable(R.drawable.fa);
        tran.startTransition(2000);


當然我們也可以用動態的方式去實現一樣的效果:

 @Override
    public void onClick(View v) {
        mPreviousPic = mCurrentPic;
        int id = v.getId();
        if (id == R.id.imageButton1) {
            mCurrentPic = getResources().getDrawable(R.drawable.fa);
        } else if (id == R.id.imageButton2) {
            mCurrentPic = getResources().getDrawable(R.drawable.xadf);
        } else if (id == R.id.imageButton3) {
            mCurrentPic = getResources().getDrawable(R.drawable.xxx);
        }

        //動態的判斷加入 Drawable數組
        tran = new TransitionDrawable(new Drawable[]{mPreviousPic,mCurrentPic});
        tran.startTransition(2000);
        ImageView1.setImageDrawable(tran);
    }


三、DrawableConstructor

    也就是上面提到的,使用動態的實現方式


完整代碼:https://github.com/shanwu/shanwu_coding_base/tree/transition_drawable_practice/ch16-3

To be Continued...


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章