Android 4种补间动画基础使用。

package com.example.thinkpad.animation;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class Main2Activity extends AppCompatActivity implements View.OnClickListener {
    ImageView imageView;
    TranslateAnimation translateAnimation;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        Toast.makeText(this,"hello",Toast.LENGTH_SHORT).show();

        imageView=(ImageView) findViewById(R.id.image);
        Button button0 =(Button) findViewById(R.id.bt_translation);
        Button button1=(Button) findViewById(R.id.bt_rotate);
        Button button2 =(Button) findViewById(R.id.bt_scale);
        Button button3 =(Button) findViewById(R.id.bt_alpha);

        button1.setOnClickListener(this);
        button0.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
    }
    public  void onClick(View v){
        switch (v.getId()){
            case R.id.bt_translation://平移
                translateAnimation =new TranslateAnimation(Animation.RELATIVE_TO_PARENT,0,
                        Animation.RELATIVE_TO_PARENT,1.0f,Animation.RELATIVE_TO_PARENT,0,Animation.RELATIVE_TO_PARENT,
                        1.0f);

                translateAnimation.setDuration(3000);
                translateAnimation.setRepeatCount(Animation.INFINITE);
                translateAnimation.setFillAfter(true);
                translateAnimation.setRepeatMode(Animation.REVERSE);

                imageView.startAnimation(translateAnimation);

            break;
            case R.id.bt_rotate://旋转
                RotateAnimation rotateAnimation=new RotateAnimation(0,360,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
                rotateAnimation.setRepeatCount(Animation.INFINITE);
                rotateAnimation.setDuration(3000);
                imageView.startAnimation(rotateAnimation);
                break;
            case R.id.bt_scale://缩放
                ScaleAnimation scaleAnimation=new ScaleAnimation(Animation.RELATIVE_TO_SELF,0,1.5f,Animation.RELATIVE_TO_SELF,0,1.5f);
                scaleAnimation.setDuration(3000);
                scaleAnimation.setRepeatCount(3);
                imageView.startAnimation(scaleAnimation);
                break;
            case R.id.bt_alpha://透明度
                AlphaAnimation alphaAnimation=new AlphaAnimation(0,1);
                alphaAnimation.setDuration(3000);
                imageView.startAnimation(alphaAnimation);


        }
    }
}

注意:需使用view类的startAnimation()开启动画而不是Animation类的start()或者startNow(),否则有可能不会及时播放动画。

你可能感兴趣的:(Android,java)