Android動畫示例
Android提供了大量用于動畫開發的類和接口。愛掏網 - it200.com大部分類和接口都包含在 android.animation 包中。愛掏網 - it200.com
Android動畫使您能夠在運行時更改對象的屬性和行為。愛掏網 - it200.com在Android中有多種方式可以進行動畫操作。愛掏網 - it200.com
AnimationDrawable 類提供了啟動和停止動畫的方法。愛掏網 - it200.com甚至可以使用基于時間的動畫。愛掏網 - it200.com
讓我們來看一個簡單的Android動畫示例。愛掏網 - it200.com
activity_main.xml
只需要一個視圖。愛掏網 - it200.com
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<View
/>
</RelativeLayout>
只有圖片查看器。愛掏網 - it200.com
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/anm"
>
</ImageView>
主Activity類
package com.javatpoint.animation;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.widget.ImageView;
public class MainActivity extends Activity {
ImageView anm;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.logo);
anm = (ImageView)findViewById(R.id.anm);
anm.setBackgroundResource(R.drawable.animation);
// the frame-by-frame animation defined as a xml file within the drawable folder
/*
* NOTE: It's not possible to start the animation during the onCreate.
*/
}
public void onWindowFocusChanged (boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
AnimationDrawable frameAnimation =
(AnimationDrawable) anm.getBackground();
if(hasFocus) {
frameAnimation.start();
} else {
frameAnimation.stop();
}
}
}
您需要在res/drawable-hdpi目錄下創建animation.xml文件。愛掏網 - it200.com
您需要有很多圖片。愛掏網 - it200.com在這里,我們使用了14個圖片,這14個圖片都位于res/drawable-mdpi目錄中。愛掏網 - it200.com
<?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/frame0" android:duration="120" />
<item android:drawable="@drawable/frame1" android:duration="120" />
<item android:drawable="@drawable/frame2" android:duration="120" />
<item android:drawable="@drawable/frame3" android:duration="120" />
<item android:drawable="@drawable/frame4" android:duration="120" />
<item android:drawable="@drawable/frame5" android:duration="120" />
<item android:drawable="@drawable/frame6" android:duration="120" />
<item android:drawable="@drawable/frame7" android:duration="120" />
<item android:drawable="@drawable/frame8" android:duration="120" />
<item android:drawable="@drawable/frame9" android:duration="120" />
<item android:drawable="@drawable/frame10" android:duration="120" />
<item android:drawable="@drawable/frame11" android:duration="120" />
<item android:drawable="@drawable/frame12" android:duration="120" />
<item android:drawable="@drawable/frame13" android:duration="120" />
<item android:drawable="@drawable/frame14" android:duration="120" />
<item android:drawable="@drawable/frame14" android:duration="120" />
<item android:drawable="@drawable/frame13" android:duration="120" />
<item android:drawable="@drawable/frame12" android:duration="120" />
<item android:drawable="@drawable/frame11" android:duration="120" />
<item android:drawable="@drawable/frame10" android:duration="120" />
<item android:drawable="@drawable/frame9" android:duration="120" />
<item android:drawable="@drawable/frame8" android:duration="120" />
<item android:drawable="@drawable/frame7" android:duration="120" />
<item android:drawable="@drawable/frame6" android:duration="120" />
<item android:drawable="@drawable/frame5" android:duration="120" />
<item android:drawable="@drawable/frame4" android:duration="120" />
<item android:drawable="@drawable/frame3" android:duration="120" />
<item android:drawable="@drawable/frame2" android:duration="120" />
<item android:drawable="@drawable/frame1" android:duration="120" />
<item android:drawable="@drawable/frame0" android:duration="120" />
</animation-list>
輸出:
聲明:所有內容來自互聯網搜索結果,不保證100%準確性,僅供參考。如若本站內容侵犯了原著者的合法權益,可聯系我們進行處理。