日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

我的Android進(jìn)階之旅------>Android之動(dòng)畫之Fra

系統(tǒng) 2152 0

============================首先看看官網(wǎng)上關(guān)于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.


============================下面通過(guò)一個(gè)小案例來(lái)學(xué)習(xí)Frame animation================================

step1:新建一個(gè)Android項(xiàng)目FrameAnimationDemo

我的Android進(jìn)階之旅------>Android之動(dòng)畫之Frame Animation實(shí)例

step2:準(zhǔn)備好該應(yīng)用使用的圖片,用來(lái)做Frame Animation的,并將動(dòng)畫放入drawable目錄下


step3:新建一個(gè)用來(lái)描述Frame動(dòng)畫的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指示是否只運(yùn)行一次,設(shè)置為false則意味著循環(huán)播放
	 <item>元素代表一幀動(dòng)畫,
	 android:drawable指定此幀動(dòng)畫所對(duì)應(yīng)的圖片資源, 
	 android:druation代表此幀持續(xù)的時(shí)間,整數(shù),單位為毫秒。 
-->
  

step4:該應(yīng)用的布局文件 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動(dòng)畫圖片 -->
	<ImageView android:id="@+id/ImgDance" android:layout_width="fill_parent"
		android:layout_height="fill_parent" android:layout_weight="1" />
	<!-- android:layout_weight="1" 不設(shè)置該屬性,下面的兩個(gè)按鈕會(huì)被覆蓋不顯示出來(lái) -->
	
	<!-- 動(dòng)畫控制按鈕 -->
	<Button android:layout_width="fill_parent"
		android:layout_height="wrap_content" android:text="開(kāi)始跳舞"
		android:onClick="runFrame" />
	<Button android:layout_width="fill_parent"
		android:layout_height="wrap_content" android:text="結(jié)束跳舞"
		android:onClick="stopFrame" />
</LinearLayout>

  

step5:該應(yīng)用的主文件,F(xiàn)rameActivity.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 {
	// 顯示動(dòng)畫的組件
	private ImageView imgDance;
	// Frame動(dòng)畫
	private AnimationDrawable animDance;

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

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

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

}
  

效果如下:

我的Android進(jìn)階之旅------>Android之動(dòng)畫之Frame Animation實(shí)例 我的Android進(jìn)階之旅------>Android之動(dòng)畫之Frame Animation實(shí)例

我的Android進(jìn)階之旅------>Android之動(dòng)畫之Frame Animation實(shí)例 我的Android進(jìn)階之旅------>Android之動(dòng)畫之Frame Animation實(shí)例

我的Android進(jìn)階之旅------>Android之動(dòng)畫之Frame Animation實(shí)例 我的Android進(jìn)階之旅------>Android之動(dòng)畫之Frame Animation實(shí)例



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

作者:歐陽(yáng)鵬 歡迎轉(zhuǎn)載,與人分享是進(jìn)步的源泉!

轉(zhuǎn)載請(qǐng)保留原文地址 http://blog.csdn.net/ouyang_peng

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


我的Android進(jìn)階之旅------>Android之動(dòng)畫之Frame Animation實(shí)例


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

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

【本文對(duì)您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦?。?!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 团风县| 通州区| 吉林市| 黄石市| 昌都县| 卢氏县| 团风县| 苍山县| 高淳县| 珲春市| 南汇区| 石首市| 乌海市| 米脂县| 出国| 磴口县| 荆门市| 且末县| 平度市| 纳雍县| 内黄县| 西充县| 洛川县| 永胜县| 屏边| 贵阳市| 鱼台县| 石林| 平阴县| 涞源县| 乌苏市| 成都市| 泽普县| 页游| 雅江县| 钟祥市| 高唐县| 弥勒县| 林西县| 禹州市| 安国市|