1. 程式人生 > >自己定義View時,用到Paint Canvas的一些溫故,簡單的幀動畫(動畫一 ,"掏糞男孩Gif"順便再提提onWindowFocusChanged)

自己定義View時,用到Paint Canvas的一些溫故,簡單的幀動畫(動畫一 ,"掏糞男孩Gif"順便再提提onWindowFocusChanged)

eat android 內容 rri generated 簡單的 fadein spl onclick

轉載請註明出處:王亟亟的大牛之路

之前在繪畫的過程中提到了靜態的旋轉啊,縮放啊,平移等一些效果。那麽自己定義的View當然也有動態的效果也就是我們的Animation。經常使用的有三種

View Animation
Drawable Animation
Property Animation

這一篇講一下簡單的Drawable Animation。

Drawable Animation實現好是什麽個樣子呢?

幀動畫,一張一張的圖片。一次播形成動畫的景象。

技術分享

效果就是如此,把一幀一幀的圖品相機播放,形成動畫的效果。

Who to do?

簡單的看你能夠用handle然後諸如runOnUIThread來做這些事,可是view既然提供了這種實現。那為什麽不用呢?

首先,定義一個動畫的xml(java代碼執行,這裏以XML聲稱為例),而且放在drable文件夾下

技術分享

這裏用的是back和positive做正放,倒放的事。

然後定義一下詳細播放的圖片內容以及持續時間

以下以倒播為例

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="true">
    <item
        android:drawable="@drawable/draw9"
        android:duration="500"
/>
<item android:drawable="@drawable/draw8" android:duration="500"/> <item android:drawable="@drawable/draw7" android:duration="500"/> <item android:drawable="@drawable/draw6" android:duration="500"/> <item android:drawable
="@drawable/draw5" android:duration="500"/>
<item android:drawable="@drawable/draw4" android:duration="500"/> <item android:drawable="@drawable/draw3" android:duration="500"/> <item android:drawable="@drawable/draw2" android:duration="500"/> <item android:drawable="@drawable/draw1" android:duration="500"/> </animation-list>

再之後用button把動畫開啟,結束就OK了 十分簡單

    @Override
    public void onClick(View v) {
        int flag = v.getId();
        anim = (AnimationDrawable) animImageView.getDrawable();
        switch (flag) {
            case R.id.startAnim:
                animImageView.setImageResource(R.drawable.positive_anim);
                anim.start();
                break;
            case R.id.toppleAnim:
                animImageView.setImageResource(R.drawable.back_anim);
                anim.start();
                break;
            case R.id.stopAnim:
                anim.stop();
                break;
        }
    }

順道再講講onWindowFocusChanged,在之前測畫圖片大小的時候已經看到了他的身影。那麽他是詳細做什麽的呢?

事實上在我們Activity執行的初期像onStart, onResume, onCreate事實上我們的整個頁面並沒有完畢渲染,所以這也就是我們為什麽無法獲取控件的長寬。

當你的Activity一獲得就會調用這種方法

@Override

public
 void onWindowFocusChanged(boolean hasFocus) { 
    // TODO Auto-generated method stub 
    super.onWindowFocusChanged(hasFocus);
}

我們知道onResume也是Activity獲取屏幕焦點時會被調用。那麽2者又有什什麽差別呢?

我們通過實驗,用代碼的Log來解釋二者的差別

  @Override
    protected void onResume() {
        super.onResume();
        Log.d("--->onResume", "onResume");
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        Log.d("-->onWindowFocusChanged", "--->AnimActivity onWindowFocusChanged");
        if (hasFocus) {
            anim.start();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d("--->onPause", "onPause");
    }

進入Activity時

01-04 16:57:24.131 5129-5129/paintcanvasdemo.pro.wjj.paintcanvasdemo D/--->onResume: onResume
01-04 16:57:24.161 5129-5129/paintcanvasdemo.pro.wjj.paintcanvasdemo D/-->>onWindowFocusChanged: --->AnimActivity onWindowFocusChanged

Home退出時

01-04 16:58:33.581 5129-5129/paintcanvasdemo.pro.wjj.paintcanvasdemo D/--->onPause: onPause
01-04 16:58:33.601 5129-5129/paintcanvasdemo.pro.wjj.paintcanvasdemo D/-->>onWindowFocusChanged: --->AnimActivity onWindowFocusChanged

當然,當中還參雜著onStop,onRestart,onStart。可是僅僅要我們的Activity產生了焦點層面的變化他都能夠監聽到。關鍵是,他已經做100%交互式的邏輯了,而部分內容在onResume這裏還無法做到(沒有全然渲染結束)。

源代碼地址:https://github.com/ddwhan0123/BlogSample/tree/master/PaintCanvasDemo

技術分享

記得點個贊哦。

自己定義View時,用到Paint Canvas的一些溫故,簡單的幀動畫(動畫一 ,&quot;掏糞男孩Gif&quot;順便再提提onWindowFocusChanged)