android幀動畫詳解

學習使我快樂使我開心
安卓的動畫大體分爲三部分 請看圖:
在這裏插入圖片描述

由上圖可以知道安卓中動畫主要分爲這三大類 分別爲視圖動畫(View Animation) ,幀動畫(Drawable Animation),屬性動畫(PropertyAnimation)
根據上圖我們就可以知道這章我要講什麼了 哈哈 那就是幀動畫
柿子要挑軟的捏 難題嘛我們就先從簡單的着手
幀動畫:圖片按照順序一幀一幀組合而成的動畫
廢話不多說直接看代碼:
文件存放路徑: main/res/drawable

<?xml version="1.0" encoding="utf-8"?>

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"

    android:oneshot="false" >
    
<!--android:oneshot="false"true表示只播放一次,false則相反-->

   <item
     android:drawable="@drawable/first"
     android:duration="500"/>
   <item
     android:drawable="@drawable/second"
     android:duration="500"/>
  <item
     android:drawable="@drawable/third"
     android:duration="500"/>
  <item
     android:drawable="@drawable/fourth"
     android:duration="500"/>

</animation-list>

第一種方法

佈局中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <ImageView
        android:id="@+id/imgview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

activity中:

  imageView = findViewById(R.id.imgview);
        AnimationDrawable animationDrawable = (AnimationDrawable) getResources().getDrawable(R.drawable.zhendonghua);
        imageView.setImageDrawable(animationDrawable);
        animationDrawable.start();//開啓動畫
        animationDrawable.stop();//關閉動畫

第二種

佈局中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/imgview"
        android:background="@drawable/zhendonghua"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

activity中:

 AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getBackground();
        //判斷是否在運行
        if(!animationDrawable.isRunning()){
            //開啓幀動畫
            animationDrawable.start();
        }

好啦 關於幀佈局就到這裏了 因爲幀佈局很簡單所以就不更多的演示了 我會在後面更新中將安卓的動畫慢慢學習更新玩的
喜歡的話請給我來個小贊吧 你的支持是我最大的動力!!!

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