Android中动画的使用

本文主要讲解Android中帧动画的使用,首先准备好需要的图片资源,本文转载自:http://blog.csdn.net/jwzhangjie/article/details/18323101,
其中的图片资源在:http://download.csdn.net/download/jwzhangjie/6852981。

首先在res目录下建立anim目录,添加3个xml文件:load_animation_1, load_animation_2, load_animation_3,文件内容分别如下:

load_animation_1:

    
   
    
    
    
    
    
    
    
    

    


load_animation_2:



    

    

    
    
    
    
   
    
    
    
    
    


wifi_animation_1.xml:

    
    
    
    
    
    


然后在MainActivity中通过setBackgroundResource(R.anim.load_animation_1)来为ImageView设置背景资源,并通过getBackground()方法来获得背景动态图片,返回的是一个AnimationDrawable对象,通过该对象的start()方法和stop()方法可以启动或者停止动态图片的效果,具体代码如下:

public class MainActivity extends AppCompatActivity { 
    private Button button1, button2, button3;    
    private ImageView animationIV;    
    private ImageView animationIV2;    
    private ImageView animationIV3;    
    private AnimationDrawable AniDraw, AniDraw2, AniDraw3;    
    @Override    
    protected void onCreate(Bundle savedInstanceState) {            
        super.onCreate(savedInstanceState);       
        setContentView(R.layout.activity_main);  
        button1 = (Button) findViewById(android.R.id.button1);  
        button1.setOnClickListener(new  View.OnClickListener() {            
            @Override            
            public void onClick(View v) {      
                if(AniDraw.isRunning()) {   
                    AniDraw.stop();               
                } else {                    
                    AniDraw.start();    
                }            
            }        
        });        
        button2 = (Button) findViewById(android.R.id.button2);    
        button2.setOnClickListener(new View.OnClickListener() {            
            @Override            
            public void onClick(View v) {    
                if(AniDraw2.isRunning()) {                   
                    AniDraw2.stop();                
                } else {                    
                    AniDraw2.start();                
                }
            }        
        });        
        button3 = (Button) findViewById(android.R.id.button3);    
        button3.setOnClickListener(new View.OnClickListener() {            
            @Override            
            public void onClick(View v) {        
                if(AniDraw3.isRunning()) {                    
                    AniDraw3.stop();                
                } else {                    
                    AniDraw3.start();                
                }
             }        
        });        
        animationIV = (ImageView) findViewById(R.id.animationIV);  
        animationIV.setBackgroundResource(R.anim.load_animation_1);        

        AniDraw = (AnimationDrawable) animationIV.getBackground();        
        animationIV2 = (ImageView)findViewById(R.id.animationIV2);   
        AniDraw2 = (AnimationDrawable)animationIV2.getDrawable();    
        animationIV3 = (ImageView)findViewById(R.id.animationIV3);   
        animationIV3.setImageResource(R.anim.wifi_animation_1);        
        AniDraw3 = (AnimationDrawable)animationIV3.getDrawable();    
        }
  }

总结:

  1. 在anim文件夹下定义动画xml文件
  2. 通过标签来定义帧动画,其中oneshot代表着是否只展示一遍,设置为false会不停的循环播放动画
  3. 通过设置ImageView的setImageResource()来设置ImageView的动画效果,并且通过getDrawable()来获得ImageView的动画图片
  4. 通过设置ImageView的setBackgroundResource()方法来设置
    ImageView的动画效果,并且通过getBackground()来获得
    ImageView的动画图片
  5. 通过设置AnimationDrawable的start()和stop()方法可以启动或停止动画效果

你可能感兴趣的:(Android中动画的使用)