Android動畫之逐幀動畫(Frame Animation)基礎學習_Android

來源:互聯網
上載者:User

前言

在Android中,動畫Animation的實現有兩種方式:Tween Animation(補間動畫)和Frame Animation(幀動畫)。漸層動畫是通過對情境裡的對象不斷做映像變換(平移、縮放、旋轉等)產生動畫效果。幀動畫則是通過順序播放事先準備好的映像來產生動畫效果,和電影類似。

下面我們就來學習下Android中逐幀動畫的基礎知識。

原理 : 人眼的"視覺暫留"

方式 :

     1.在java代碼中 ( new AnimationDrawable().addFrame(getDrawable(R.drawable.a),200);)

         sdk好像要求最低版本必須>=21

     2.在XML檔案中定義動畫資源

效果圖如下

代碼

1.準備圖片資源

將圖片資源放在drawable-hdpi目錄下

2.在drawable目錄下建立animation-list類型檔案

anim_frame.xml

<?xml version="1.0" encoding="utf-8"?><animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false"> <item android:drawable="@drawable/a" android:duration="100"/> <item android:drawable="@drawable/b" android:duration="100"/> <item android:drawable="@drawable/c" android:duration="100"/></animation-list>

3.在布局檔案中,添加ImageView,將其background屬性設定為動畫資源xml

activity_main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/btn_start" android:text="開始跳舞" android:textSize="25sp" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <Button android:id="@+id/btn_stop" android:text="結束跳舞" android:textSize="25sp" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <ImageView android:id="@+id/image" android:background="@drawable/anim_frame" android:layout_width="match_parent" android:layout_height="match_parent"/></LinearLayout>

4.在java中,擷取動畫資源,調用start( )開啟動畫,stop( )停止動畫

package com.lyp.frameanim;import android.graphics.drawable.AnimationDrawable;import android.graphics.drawable.Drawable;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.ImageView;public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private Button mBtnStart; private Button mBtnStop; private ImageView mImage; private AnimationDrawable mAnim; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);// new AnimationDrawable().addFrame(getDrawable(R.drawable.a),200); initView(); mAnim = (AnimationDrawable) mImage.getBackground(); } private void initView() { mBtnStart= (Button) findViewById(R.id.btn_start); mBtnStop= (Button) findViewById(R.id.btn_stop); mImage= (ImageView) findViewById(R.id.image); mBtnStart.setOnClickListener(this); mBtnStop.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()){  case R.id.btn_start:  mAnim.start();  break;  case R.id.btn_stop:  mAnim.stop();  break; } }}

總結

以上就是這篇文章的全部內容了,希望能對各位Android開發人員們有所協助,如果有疑問大家可以留言交流。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.