类微信播放音频帧动画实现

 

package com.example.frame_animation;

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;


/**
 * 帧动画示例
 * 出自:http://blog.csdn.net/aminfo/article/details/7847761
 * 
 * @author chenwenbiao
 * @date 2013-12-2 下午7:37:07
 * @version V1.0
 */
public class MainActivity extends Activity {
	
	private  String TAG = "MainActivity";
	
	private ImageView animationIV;
	private Button playButton , stopButton;
	private AnimationDrawable animationDrawable;
	
	/**
	 * 指定播放时长(单位:秒)
	 */
	private EditText timeEditText ;
	
	/**
	 * 等播放的图片
	 */
	private int[] images = { R.drawable.audio_one, R.drawable.audio_two ,R.drawable.audio_three};
	
	/**
	 * 设置了新播放时间
	 */
	private boolean newTime = true;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
       
        
        
        animationIV = (ImageView) findViewById(R.id.animationIV);
        playButton = (Button) findViewById(R.id.buttonA);
        stopButton = (Button) findViewById(R.id.buttonB);
        timeEditText = (EditText) findViewById(R.id.timeEditText);
        timeEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
 

			@Override
			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
				if (actionId == EditorInfo.IME_ACTION_GO) {
					 Log.d(TAG , "heheh");
                }
				
				
				Log.d(TAG , "actionId :" + actionId);
 
                return false;
			}
 
        });


		animationIV.setImageResource(R.anim.animation3);
		animationDrawable = (AnimationDrawable) animationIV.getDrawable();
		
		
		animationDrawable.setOneShot(true);
		
		
		
        playButton.setOnClickListener(new OnClickListener()
        {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				if(newTime){
					int runTime = Integer.parseInt(timeEditText.getText().toString()) * 1000;//转为秒
					Log.d(TAG , "runTime:" + runTime);
					
					int loop = 0;
					if(runTime > 600){
						runTime -= 600;//默认完整动画播放时间是0.6秒,
						loop = runTime / 600 + 1;//计算还需要多少播放多少次动画
					}
					
					Log.d(TAG , "loop:" + loop);
					for(int i = 0 ; i < loop ; i++){
						for(int j = 0 ; j < 3 ; j++){
							animationDrawable.addFrame(getResources().getDrawable(images[j]), 200); 
						}
					}
					
					newTime = false;
				}
				
				
				animationDrawable.stop();
				animationDrawable.start();
			}
        	
        }); 
        
        stopButton.setOnClickListener(new OnClickListener()
        {
			@Override
			public void onClick(View v) {
				animationDrawable.stop();
			}
        	
        });
        
        
    }
    
   
}

 

完整代码见附件。

 

你可能感兴趣的:(android)