Android Animation(三)

之前编写的一些动画,翻转,平移都是各自独立的,下面我们编写一个混合的,并且我也同时编写了利用xml和java的代码的方案。


跟之前一样,首先在res下创建一个anim文件夹,创建一个xml文件


1.animone.xml




    

    


2.activity_main.xml




    

    


3.MainActivity.java

public class MainActivity extends AppCompatActivity {

    private ImageView imageView;
    private Button btnStartXml;
    private Button btnStartJava;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageView = (ImageView) findViewById(R.id.image_view);
        btnStartXml = (Button) findViewById(R.id.btn_start_xml);
        btnStartJava = (Button) findViewById(R.id.btn_start_java);

        btnStartXml.setOnClickListener(new XmlListener());
        btnStartJava.setOnClickListener(new JavaListener());
    }

    class XmlListener implements View.OnClickListener{

        @Override
        public void onClick(View v) {
            Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.animone);
            imageView.startAnimation(animation);
        }
    }

    class JavaListener implements View.OnClickListener{

        @Override
        public void onClick(View v) {
            AnimationSet animationSet = new AnimationSet(true);
            ScaleAnimation scaleAnimation = new ScaleAnimation(0,0.1f,0,0.1f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
            RotateAnimation rotateAnimation = new RotateAnimation(0,360,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
            rotateAnimation.setDuration(1000);

            animationSet.addAnimation(rotateAnimation);
            animationSet.addAnimation(scaleAnimation);
            imageView.startAnimation(animationSet);
        }
    }

}




你可能感兴趣的:(android开发)