首頁(yè) 收藏 QQ群
 網(wǎng)站導(dǎo)航

ZNDS智能電視網(wǎng) 推薦當(dāng)貝市場(chǎng)

TV應(yīng)用下載 / 資源分享區(qū)

軟件下載 | 游戲 | 討論 | 電視計(jì)算器

綜合交流 / 評(píng)測(cè) / 活動(dòng)區(qū)

交流區(qū) | 測(cè)硬件 | 網(wǎng)站活動(dòng) | Z幣中心

新手入門 / 進(jìn)階 / 社區(qū)互助

新手 | 你問(wèn)我答 | 免費(fèi)刷機(jī)救磚 | ROM固件

查看: 11279|回復(fù): 0
上一主題 下一主題
[教程]

Android游戲開發(fā)之使用AnimationDrable實(shí)現(xiàn)Frame動(dòng)畫(三十一)

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
發(fā)表于 2013-8-28 16:28 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
     
Android開發(fā)中在制作2D幀動(dòng)畫中提供了使用XML配置動(dòng)畫文件的方式繪制,也就是說(shuō)Android底層提供了動(dòng)畫播放的接口,那么我們分析一下如何調(diào)用它的接口來(lái)繪制動(dòng)畫。首先在工程res資源文件夾下創(chuàng)建anim動(dòng)畫文件夾,在這個(gè)文件夾中建立一個(gè)animation.xml文件, 這樣它的路徑就為re/anim/animation.xml。   
   
   
看看內(nèi)容應(yīng)該是很好理解的,<animation-list>為動(dòng)畫的總標(biāo)簽,這里面放著幀動(dòng)畫 <item>標(biāo)簽,也就是說(shuō)若干<item>標(biāo)簽的幀 組合在一起就是幀動(dòng)畫了。<animation-list > 標(biāo)簽中android:oneshot="false" 這是一個(gè)非常重要的屬性,默認(rèn)為false 表示 動(dòng)畫循環(huán)播放, 如果這里寫true 則表示動(dòng)畫只播發(fā)一次。 <item>標(biāo)簽中記錄著每一幀的信息android:drawable="@drawable/a"表示這一幀用的圖片為"a",下面以此類推。  android:duration="100" 表示這一幀持續(xù)100毫秒,可以根據(jù)這個(gè)值來(lái)調(diào)節(jié)動(dòng)畫播放的速度。   
  1. <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" />   
      <item android:drawable="@drawable/d" android:duration="100" />   
      <item android:drawable="@drawable/e" android:duration="100" />   
      <item android:drawable="@drawable/f" android:duration="100" />   
      <item android:drawable="@drawable/g" android:duration="100" />   
      <item android:drawable="@drawable/h" android:duration="100" />   
      <item android:drawable="@drawable/i" android:duration="100" />   
      <item android:drawable="@drawable/j" android:duration="100" />   
      </animation-list>
復(fù)制代碼
下面這個(gè)例子的內(nèi)容為 播放動(dòng)畫 與關(guān)閉動(dòng)畫 、設(shè)置播放類型 單次還是循環(huán)、拖動(dòng)進(jìn)度條修改動(dòng)畫的透明度,廢話不多說(shuō)直接進(jìn)正題~~   
   
     
  1. <?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"   
        >   
    <LinearLayout   
        android:orientation="horizontal"   
        android:layout_width="wrap_content"   
        android:layout_height="wrap_content"   
        >   
    <Button   
       android:id="@+id/button0"   
       android:layout_width="wrap_content"   
       android:layout_height="wrap_content"   
       android:text="播放動(dòng)畫"   
    />   
       
      <Button   
       android:id="@+id/button1"   
       android:layout_width="wrap_content"   
       android:layout_height="wrap_content"   
       android:text="停止動(dòng)畫"   
    />   
    </LinearLayout>   
          
    <RadioGroup android:id="@+id/radiogroup"   
                android:layout_width="wrap_content"   
                android:layout_height="wrap_content"   
               android:orientation="horizontal">   
       <RadioButton   
               android:id="@+id/checkbox0"   
               android:layout_width="wrap_content"   
               android:layout_height="wrap_content"   
               android:checked="true"   
               android:text="單次播放"   
       />   
      <RadioButton   
               android:id="@+id/checkbox1"   
               android:layout_width="wrap_content"   
               android:layout_height="wrap_content"   
               android:text="循環(huán)播放"   
       />   
       </RadioGroup>   
          
        <TextView   
            android:layout_width="wrap_content"   
            android:layout_height="wrap_content"   
            android:text="拖動(dòng)進(jìn)度條修改透明度(0 - 255)之間"   
            />   
      <SeekBar   
            android:id="@+id/seekBar"   
            android:layout_width="fill_parent"   
            android:layout_height="wrap_content"   
            android:max="256"   
            android:progress="256"/>   
      <ImageView   
       android:id="@+id/imageView"   
       android:background="@anim/animation"   
       android:layout_width="wrap_content"   
       android:layout_height="wrap_content"   
    />   
    </LinearLayout>
復(fù)制代碼
這是一個(gè)比較簡(jiǎn)單的布局文件,應(yīng)該都能看懂吧。  我主要說(shuō)一下 最后的這個(gè) ImageView, 它就是用來(lái)顯示我們的動(dòng)畫。 這里使用android:background="@anim/animation"設(shè)置這個(gè)ImageView現(xiàn)實(shí)的背景為一個(gè)動(dòng)畫,動(dòng)畫資源的路徑為res/anim/animation.xml   ,當(dāng)然 設(shè)置background同樣也可以在代碼中設(shè)置。   
  1.         imageView.setBackgroundResource(R.anim.animation);
復(fù)制代碼
通過(guò)getBackground方法就可以拿到這個(gè)animationDrawable對(duì)象。   
LG">
  1.         /**拿到ImageView對(duì)象**/   
            imageView = (ImageView)findViewById(R.id.imageView);   
            /**通過(guò)ImageView對(duì)象拿到背景顯示的AnimationDrawable**/   
            animationDrawable = (AnimationDrawable) imageView.getBackground();
復(fù)制代碼
AnimationDrawable 就是用來(lái)控制這個(gè)幀動(dòng)畫,這個(gè)類中提供了很多方法。   
   
animationDrawable.start(); 開始這個(gè)動(dòng)畫   
animationDrawable.stop(); 結(jié)束這個(gè)動(dòng)畫   
animationDrawable.setAlpha(100);設(shè)置動(dòng)畫的透明度, 取值范圍(0 - 255)   
animationDrawable.setOneShot(true); 設(shè)置單次播放   
animationDrawable.setOneShot(false); 設(shè)置循環(huán)播放   
animationDrawable.isRunning(); 判斷動(dòng)畫是否正在播放   
animationDrawable.getNumberOfFrames(); 得到動(dòng)畫的幀數(shù)。   
   
   
將這個(gè)例子的完整代碼貼上   
  1. import android.app.Activity;   
    import android.graphics.drawable.AnimationDrawable;   
    import android.os.Bundle;   
    import android.util.Log;   
    import android.view.View;   
    import android.view.View.OnClickListener;   
    import android.widget.Button;   
    import android.widget.ImageView;   
    import android.widget.RadioButton;   
    import android.widget.RadioGroup;   
    import android.widget.SeekBar;   
    import android.widget.SeekBar.OnSeekBarChangeListener;   
       
    public class SimpleActivity extends Activity {   
       
        /**播放動(dòng)畫按鈕**/   
        Button button0 = null;   
         
        /**停止動(dòng)畫按鈕**/   
        Button button1 = null;   
          
        /**設(shè)置動(dòng)畫循環(huán)選擇框**/   
        RadioButton radioButton0= null;   
        RadioButton radioButton1= null;   
        RadioGroup  radioGroup = null;   
         
        /**拖動(dòng)圖片修改Alpha值**/   
        SeekBar seekbar = null;   
         
        /**繪制動(dòng)畫View**/   
        ImageView imageView = null;   
          
        /**繪制動(dòng)畫對(duì)象**/   
        AnimationDrawable animationDrawable = null;   
        @Override   
        public void onCreate(Bundle savedInstanceState) {   
            super.onCreate(savedInstanceState);   
            setContentView(R.layout.simple);   
       
            /**拿到ImageView對(duì)象**/   
            imageView = (ImageView)findViewById(R.id.imageView);   
            /**通過(guò)ImageView對(duì)象拿到背景顯示的AnimationDrawable**/   
            animationDrawable = (AnimationDrawable) imageView.getBackground();   
               
               
            /**開始播放動(dòng)畫**/   
            button0 = (Button)findViewById(R.id.button0);   
            button0.setOnClickListener(new OnClickListener() {   
                   
                @Override   
                public void onClick(View arg0) {   
                    /**播放動(dòng)畫**/   
                    if(!animationDrawable.isRunning()) {   
                        animationDrawable.start();   
                    }   
                }   
            });   
               
            /**停止播放動(dòng)畫**/   
            button1 = (Button)findViewById(R.id.button1);   
            button1.setOnClickListener(new OnClickListener() {   
                   
                @Override   
                public void onClick(View arg0) {   
                    /**停止動(dòng)畫**/   
                    if(animationDrawable.isRunning()) {   
                        animationDrawable.stop();   
                    }   
                }   
            });   
            /**單次播放**/   
            radioButton0 = (RadioButton)findViewById(R.id.checkbox0);   
            /**循環(huán)播放**/   
            radioButton1 = (RadioButton)findViewById(R.id.checkbox1);   
            /**單選列表組**/   
            radioGroup = (RadioGroup)findViewById(R.id.radiogroup);   
            radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {   
                   
                @Override   
                public void onCheckedChanged(RadioGroup radioGroup, int checkID) {   
                    if(checkID == radioButton0.getId()) {   
                        //設(shè)置單次播放   
                        animationDrawable.setOneShot(true);   
                    }else if (checkID == radioButton1.getId()) {   
                        //設(shè)置循環(huán)播放   
                        animationDrawable.setOneShot(false);   
                    }   
                      
                    //發(fā)生改變后讓動(dòng)畫重新播放   
                    animationDrawable.stop();   
                    animationDrawable.start();   
                }   
            });   
               
            /**監(jiān)聽(tīng)的進(jìn)度條修改透明度**/   
            seekbar = (SeekBar)findViewById(R.id.seekBar);   
            seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {   
                @Override   
                public void onStopTrackingTouch(SeekBar seekBar) {   
                      
                }   
                @Override   
                public void onStartTrackingTouch(SeekBar seekBar) {   
                      
                }   
                @Override   
                public void onProgressChanged(SeekBar seekBar, int progress, boolean frameTouch) {   
                    /**設(shè)置動(dòng)畫Alpha值**/   
                    animationDrawable.setAlpha(progress);   
                    /**通知imageView 刷新屏幕**/   
                    imageView.postInvalidate();   
                }   
            });   
               
        }   
    }
復(fù)制代碼
拖動(dòng)進(jìn)度條設(shè)置Alpha值的時(shí)候 一定要使用     imageView.postInvalidate(); 方法來(lái)通知UI線程重繪屏幕中的imageView  否則會(huì)看不到透明的效果 。這里切記切記~~   
   
     
   
   
   
總的來(lái)說(shuō)這章內(nèi)容還是比較簡(jiǎn)單的。老規(guī)矩每篇文章都會(huì)附帶源代碼,最后如果你還是覺(jué)得我寫的不夠詳細(xì) 看的不夠爽 不要緊我把源代碼的下載地址貼出來(lái) 歡迎大家一起討論學(xué)習(xí)第三十一講 fram游戲動(dòng)畫.rar(255.38 KB, 下載次數(shù): 378)[/I]2011-9-6 20:36 上傳點(diǎn)擊文件名   下載積分: 下載豆 -2   

上一篇:Androidr 登錄框
下一篇:在Ubuntu上為Android系統(tǒng)編寫Linux內(nèi)核驅(qū)動(dòng)程序

本版積分規(guī)則

Archiver|新帖|標(biāo)簽|軟件|Sitemap|ZNDS智能電視網(wǎng) ( 蘇ICP備2023012627號(hào) )

網(wǎng)絡(luò)信息服務(wù)信用承諾書 | 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證:蘇B2-20221768 丨 蘇公網(wǎng)安備 32011402011373號(hào)

GMT+8, 2024-10-20 11:46 , Processed in 0.058685 second(s), 13 queries , Redis On.

Powered by Discuz!

監(jiān)督舉報(bào):report#znds.com (請(qǐng)將#替換為@)

© 2007-2024 ZNDS.Com

快速回復(fù) 返回頂部 返回列表