溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么在Android中利用animation-list實(shí)現(xiàn)一個幀動畫效果

發(fā)布時間:2021-01-21 16:00:49 來源:億速云 閱讀:254 作者:Leah 欄目:移動開發(fā)

本篇文章給大家分享的是有關(guān)怎么在Android中利用animation-list實(shí)現(xiàn)一個幀動畫效果,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

順序顯示動畫文件:animation1.xml

<?xml version="1.0" encoding="utf-8"?> 
<!--  
  根標(biāo)簽為animation-list,其中oneshot代表著是否只展示一遍,設(shè)置為false會不停的循環(huán)播放動畫 
  根標(biāo)簽下,通過item標(biāo)簽對動畫中的每一個圖片進(jìn)行聲明 
  android:duration 表示展示所用的該圖片的時間長度 
 --> 
<animation-list 
 xmlns:android="http://schemas.android.com/apk/res/android" 
 android:oneshot="true" 
 > 
  <item android:drawable="@drawable/icon1" android:duration="150"></item> 
  <item android:drawable="@drawable/icon2" android:duration="150"></item> 
  <item android:drawable="@drawable/icon3" android:duration="150"></item> 
  <item android:drawable="@drawable/icon4" android:duration="150"></item> 
  <item android:drawable="@drawable/icon5" android:duration="150"></item> 
  <item android:drawable="@drawable/icon6" android:duration="150"></item> 
</animation-list>

倒序顯示動畫文件:animation2.xml

<?xml version="1.0" encoding="utf-8"?> 
<!--  
  根標(biāo)簽為animation-list,其中oneshot代表著是否只展示一遍,設(shè)置為false會不停的循環(huán)播放動畫 
  根標(biāo)簽下,通過item標(biāo)簽對動畫中的每一個圖片進(jìn)行聲明 
  android:duration 表示展示所用的該圖片的時間長度 
 --> 
<animation-list 
 xmlns:android="http://schemas.android.com/apk/res/android" 
 android:oneshot="true" 
 > 
  <item android:drawable="@drawable/icon6" android:duration="150"></item> 
  <item android:drawable="@drawable/icon5" android:duration="150"></item> 
  <item android:drawable="@drawable/icon4" android:duration="150"></item> 
  <item android:drawable="@drawable/icon3" android:duration="150"></item> 
  <item android:drawable="@drawable/icon2" android:duration="150"></item> 
  <item android:drawable="@drawable/icon1" android:duration="150"></item> 
</animation-list>

布局文件

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:layout_width="fill_parent" 
  android:layout_height="fill_parent" 
  android:orientation="vertical"> 
   
  <ImageView android:id="@+id/animationIV" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:padding="5px" 
      android:src="@drawable/animation1"/>  
       
  <Button android:id="@+id/buttonA" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:padding="5px" 
    android:text="順序顯示" /> 
   
  <Button android:id="@+id/buttonB" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:padding="5px" 
    android:text="停止" /> 
   
  <Button android:id="@+id/buttonC" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:padding="5px" 
    android:text="倒序顯示" /> 
 
</LinearLayout>

Activity文件

package org.shuxiang.test; 
 
import android.app.Activity; 
import android.graphics.drawable.AnimationDrawable; 
 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.Window; 
import android.widget.Button; 
import android.widget.ImageView; 
 
public class Activity10 extends Activity 
{ 
  private ImageView animationIV; 
  private Button buttonA, buttonB, buttonC; 
  private AnimationDrawable animationDrawable; 
  @Override 
  public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.test10); 
     
     
    animationIV = (ImageView) findViewById(R.id.animationIV); 
    buttonA = (Button) findViewById(R.id.buttonA); 
    buttonB = (Button) findViewById(R.id.buttonB); 
    buttonC = (Button) findViewById(R.id.buttonC); 
     
    buttonA.setOnClickListener(new OnClickListener() 
    { 
      @Override 
      public void onClick(View v) { 
        // TODO Auto-generated method stub 
        animationIV.setImageResource(R.drawable.animation1); 
        animationDrawable = (AnimationDrawable) animationIV.getDrawable(); 
        animationDrawable.start(); 
      } 
       
    });  
     
    buttonB.setOnClickListener(new OnClickListener() 
    { 
      @Override 
      public void onClick(View v) { 
        // TODO Auto-generated method stub 
        animationDrawable = (AnimationDrawable) animationIV.getDrawable(); 
        animationDrawable.stop(); 
      } 
       
    }); 
     
    buttonC.setOnClickListener(new OnClickListener() 
    { 
      @Override 
      public void onClick(View v) { 
        // TODO Auto-generated method stub 
        animationIV.setImageResource(R.drawable.animation2); 
        animationDrawable = (AnimationDrawable) animationIV.getDrawable(); 
        animationDrawable.start(); 
      }       
    });     
  } 
}

以上就是怎么在Android中利用animation-list實(shí)現(xiàn)一個幀動畫效果,小編相信有部分知識點(diǎn)可能是我們?nèi)粘9ぷ鲿姷交蛴玫降摹OM隳芡ㄟ^這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI