自定义AnimationDrawable  添加声…

public class MyAnimationDrawable extends AnimationDrawable 
{      
Handler finishHandler;       // ᅤ￐쐬ᄑ£ᅧ￸샤Handler 
AnimationCallBack callback = null;
public int type = 0;
public String name = null;
SoundPool soundPool = null;
ArrayList<String> soundInfo = null;
ArrayList<Animations> soundList = null;
int soundIndex = 0;
public void setAnimationCallback(AnimationCallBack call)
{
callback = call;
}

        public void addSound(String sound)
{
if(soundInfo == null)
{
soundInfo = new ArrayList<String>();
}
soundInfo.add(sound);
}

public void cancel()
{
if(finishHandler != null)
{
finishHandler.removeCallbacks(runnable);
}
if(soundInfo != null)
{
soundInfo.clear();
soundInfo = null;
}
if(soundPool != null)
{
soundPool.release();
soundPool = null;
}
if(soundList != null)
{
soundList.clear();
soundList = null;
}
}

@Override    
public void start() 
{    
super.start(); 
soundIndex = 0;
Animations animation = null;
if(soundInfo != null)
{
int len = soundInfo.size();
if(len > 0)
{
soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 100); 
soundList = new ArrayList<Animations>();
String []values = null;
int result = -1;
int duration = 0;
animation = null;
for (String str : soundInfo)
                {
if(str != null)
                {
                values = str.split("%");
                if(values != null && values.length == 2)
                {
                result = soundPool.load(values[0], 1);
duration = Integer.parseInt(values[1]);
                if(result >= 0 && duration >= 0)
                {
                animation = new Animations();
                animation.soundID = result;
                animation.duration = duration;
                soundList.add(animation);
                animation = null;
                }
}
              }
                }
}
}
       
finishHandler = new Handler();          
finishHandler.postDelayed(runnable, getTotalDuration());
if(soundList != null && soundIndex < soundList.size())
{
animation = soundList.get(soundIndex);
if(animation != null)
{
finishHandler.postDelayed(soundRunnable, animation.duration);
}
}
}
   
public int getTotalDuration() 
{          
int durationTime = 0;          
for (int i = 0; i < this.getNumberOfFrames(); i++) 
{              
durationTime += this.getDuration(i);          
return durationTime;      
}

private Runnable runnable = new Runnable() 
{
@Override
public void run()
{
if(callback != null)
{
callback.animationEnd();
}
}
};

private Runnable soundRunnable = new Runnable() 
{
@Override
public void run()
{
if(soundList != null && soundIndex < soundList.size())
{

Animations animation = soundList.get(soundIndex);
if(animation != null)
{
soundPool.play(animation.soundID, 1, 1, 0, 0, 1);
System.out.println("===============================soundIndex " + soundIndex);
}

soundIndex += 1;
if(soundIndex < soundList.size())
{
animation = soundList.get(soundIndex);
if(animation != null)
{
finishHandler.postDelayed(soundRunnable, animation.duration);
}
}
animation = null;
}
}
};

public interface AnimationCallBack
{
public   void animationEnd();
}
class Animations 
{
int soundID = -1;
int duration = -1;
}
}
               
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值