亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

我的Android進階之旅------>Android之動畫之Fra

系統(tǒng) 1885 0

============================首先看看官網上關于Frame animation的介紹================================

地址: http://developer.android.com/guide/topics/resources/animation-resource.html#Frame

Frame animation

An animation defined in XML that shows a sequence of images in order (like a film).

FILE LOCATION:
res/drawable/ filename .xml
The filename will be used as the resource ID.
COMPILED RESOURCE DATATYPE:
Resource pointer to an AnimationDrawable .
RESOURCE REFERENCE:
In Java: R.drawable. filename
In XML: @[ package :]drawable. filename
SYNTAX:
      
        <?
      
      
        xml version
      
      
        =
      
      
        "1.0"
      
      
         encoding
      
      
        =
      
      
        "utf-8"
      
      
        ?>
      
      
      
      
        <
      
      
        
          animation-list
        
      
      
      
      
        xmlns:android
      
      
        =
      
      
        "http://schemas.android.com/apk/res/android"
      
      
      
      
        android:oneshot
      
      
        =
      
      
        ["true"
      
      
         | 
      
      
        "false"
      
      
        ] 
      
      
        >
      
      
      
      
        <
      
      
        
          item
        
      
      
      
      
        android:drawable
      
      
        =
      
      
        "@[package:]drawable/
      
      
        
          drawable_resource_name
        
      
      
        "
      
      
      
      
        android:duration
      
      
        =
      
      
        "
      
      
        
          integer
        
      
      
        "
      
      
      
      
        />
      
      
      
      
        </animation-list>
      
    
ELEMENTS:
<animation-list>
Required . This must be the root element. Contains one or more <item> elements.

attributes:

android:oneshot
Boolean . "true" if you want to perform the animation once; "false" to loop the animation.
<item>
A single frame of animation. Must be a child of a <animation-list> element.

attributes:

android:drawable
Drawable resource . The drawable to use for this frame.
android:duration
Integer . The duration to show this frame, in milliseconds.


============================下面通過一個小案例來學習Frame animation================================

step1:新建一個Android項目FrameAnimationDemo

我的Android進階之旅------>Android之動畫之Frame Animation實例

step2:準備好該應用使用的圖片,用來做Frame Animation的,并將動畫放入drawable目錄下


step3:新建一個用來描述Frame動畫的xml文件,res/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/p1" android:duration="500" />
	<item android:drawable="@drawable/p2" android:duration="500" />
	<item android:drawable="@drawable/p3" android:duration="500" />
	<item android:drawable="@drawable/p4" android:duration="500" />
	<item android:drawable="@drawable/p5" android:duration="500" />
	<item android:drawable="@drawable/p6" android:duration="500" />
</animation-list>


<!-- android:oneshot指示是否只運行一次,設置為false則意味著循環(huán)播放
	 <item>元素代表一幀動畫,
	 android:drawable指定此幀動畫所對應的圖片資源, 
	 android:druation代表此幀持續(xù)的時間,整數,單位為毫秒。 
-->
  

step4:該應用的布局文件 res/layout/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="fill_parent"
	android:layout_height="fill_parent">
	
	<!-- Frame動畫圖片 -->
	<ImageView android:id="@+id/ImgDance" android:layout_width="fill_parent"
		android:layout_height="fill_parent" android:layout_weight="1" />
	<!-- android:layout_weight="1" 不設置該屬性,下面的兩個按鈕會被覆蓋不顯示出來 -->
	
	<!-- 動畫控制按鈕 -->
	<Button android:layout_width="fill_parent"
		android:layout_height="wrap_content" android:text="開始跳舞"
		android:onClick="runFrame" />
	<Button android:layout_width="fill_parent"
		android:layout_height="wrap_content" android:text="結束跳舞"
		android:onClick="stopFrame" />
</LinearLayout>

  

step5:該應用的主文件,FrameActivity.java

    package cn.oyp.frame;

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

public class FrameActivity extends Activity {
	// 顯示動畫的組件
	private ImageView imgDance;
	// Frame動畫
	private AnimationDrawable animDance;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		// 實例化組件
		imgDance = (ImageView) super.findViewById(R.id.ImgDance);
	}
	
	/**
	 * 如果在onCreate()中調用AnimationDrawable的start()方法,則它只停留在第一幀,并沒有出現我們期望的動畫,
	 * 這是因為窗口Window對象還沒有完全初始化,AnimationDrawable不能完全追加到窗口Window對象中。
	 * 而onWindowFocusChanged是在onCreate之后被調用的,當Activity展示給用戶時,onWindowFocusChanged方法就會被調用, 
	 * 所以在這兒調用AnimationDrawable的start()方法可以實現動畫效果。
	 */
	@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		super.onWindowFocusChanged(hasFocus);
		// 將動畫資源文件res/anim/frame.xml設置為ImageView的背景
		imgDance.setBackgroundResource(R.anim.frame);
		// 獲取ImageView背景,此時已被編譯成AnimationDrawable
		animDance = (AnimationDrawable) imgDance.getBackground();
		animDance.start();
	}

	/**
	 * 按鈕:停止‘跳舞’動畫
	 */
	public void stopFrame(View view) {
		animDance = (AnimationDrawable) imgDance.getBackground();
		if (animDance.isRunning()) { // 如果正在運行,就停止
			animDance.stop();
		}
	}

	/**
	 * 按鈕:開始‘跳舞’動畫
	 */
	public void runFrame(View view) {
		// 完全編碼實現的動畫效果
		animDance = new AnimationDrawable();
		for (int i = 1; i <= 6; i++) {
			// 根據資源名稱和目錄獲取R.java中對應的資源ID
			int id = getResources().getIdentifier("p" + i, "drawable",
					getPackageName());
			// 根據資源ID獲取到Drawable對象
			Drawable drawable = getResources().getDrawable(id);
			// 將此幀添加到AnimationDrawable中
			animDance.addFrame(drawable, 500);
		}
		animDance.setOneShot(false); // 設置為loop
		imgDance.setBackgroundDrawable(animDance); // 將動畫設置為ImageView背景
		animDance.start(); // 開始動畫
	}

}
  

效果如下:

我的Android進階之旅------>Android之動畫之Frame Animation實例 我的Android進階之旅------>Android之動畫之Frame Animation實例

我的Android進階之旅------>Android之動畫之Frame Animation實例 我的Android進階之旅------>Android之動畫之Frame Animation實例

我的Android進階之旅------>Android之動畫之Frame Animation實例 我的Android進階之旅------>Android之動畫之Frame Animation實例



=================================================================================================

作者:歐陽鵬 歡迎轉載,與人分享是進步的源泉!

轉載請保留原文地址 http://blog.csdn.net/ouyang_peng

==================================================================================================


我的Android進階之旅------>Android之動畫之Frame Animation實例


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 5060网午夜一级毛片在线看 | 国产午夜视频在线观看第四页 | 国产成人综合自拍 | 羞羞色男人的天堂伊人久久 | 国产成年网站v片在线观看 国产成人 免费观看 | 欧美一级毛片日韩一级 | 欧美成人aa大片拍拍拍 | 国产精品久久久一区二区三区 | 国产色资源| 青青青爽视频在线观看 | 亚洲免费美女视频 | 九七影院97影院理论片 | 91手机在线视频 | 午夜影院私人 | 久久这里只有精品首页 | 五月天激情亚洲婷婷在线 | 欧美特欧美特级一片 | 久久久99精品久久久久久 | 久久国产精品系列 | 亚洲精品综合欧美一区二区三区 | 国产精品美女 | 另类尿喷潮videofree | 国产精品成aⅴ人片在线观看 | 日本不卡高清免费v日本 | 黄色成人在线 | 欧美久久影院 | 亚洲欧美日韩综合在线 | 日日日视频 | 91美女啪啪| 久草视频免费播放 | 日本一区二区中文字幕 | 亚洲国产97在线精品一区 | 鲁鲁狠色综合色综合网站 | 成人黄色在线 | 在线观看成人小视频 | 国产99久久九九精品免费 | 成人国产免费 | 欧美3区| 亚洲精品久荜中文字幕 | 日韩 欧美 国产 亚洲 中文 | 亚洲一区二区三区在线播放 |