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

本文详细介绍了在Android中实现四种基本动画的方法:平移、旋转、缩放和改变透明度。通过实例展示了如何使用TranslateAnimation、RotateAnimation、ScaleAnimation和AlphaAnimation类来创建动画效果,并解释了使用View类的startAnimation()方法的重要性。
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(),否则有可能不会及时播放动画。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值