帧动画

   前面讲了三大动画的视图动画,现在就来简单介绍一下帧动画。

   现在先来看一下效果:

             帧动画_第1张图片

   一.使用xml布局的形式

   1.在drawable目录下新建一个xml



    
    
    
    
    
    

   2.布局xml



    


   3.Java代码

public class Main3Activity extends AppCompatActivity{

    private ImageView image;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        initView();
    }

    private void initView() {
        image= (ImageView) findViewById(R.id.main3_image);
        image.setImageResource(R.drawable.my_frame_animation);
        AnimationDrawable animationDrawable= (AnimationDrawable) image.getDrawable();
        animationDrawable.start();

    }
}
   二.使用代码的形式

public class Main3Activity extends AppCompatActivity{

    private ImageView image;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        initView();
    }

    private void initView() {
        AnimationDrawable animationDrawable=new AnimationDrawable();
        Drawable drawable=getResources().getDrawable(R.drawable.icon1);
        Drawable drawable2=getResources().getDrawable(R.drawable.icon2);
        Drawable drawable3=getResources().getDrawable(R.drawable.icon3);
        Drawable drawable4=getResources().getDrawable(R.drawable.icon4);
        Drawable drawable5=getResources().getDrawable(R.drawable.icon5);
        Drawable drawable6=getResources().getDrawable(R.drawable.icon6);
        //添加图片
        animationDrawable.addFrame(drawable,300);
        animationDrawable.addFrame(drawable2,300);
        animationDrawable.addFrame(drawable3,300);
        animationDrawable.addFrame(drawable4,300);
        animationDrawable.addFrame(drawable5,300);
        animationDrawable.addFrame(drawable6,300);

        animationDrawable.setOneShot(false);
        image.setImageDrawable(animationDrawable);
        animationDrawable.start();
    }
}



  

你可能感兴趣的:(学习交流,Frame动画)