补间动画/View动画_代码


转载请注明出处(万分感谢!): 

https://blog.csdn.net/weixin_40790006/article/details/80017039


package com.example.f405.animtest;

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.AnimationSet;
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 MainActivity extends AppCompatActivity implements View.OnClickListener{

    private Button btn_alpha;
    private Button btn_rotate;
    private Button btn_scale;
    private Button btn_trans;
    private ImageView iv;
    private Button btn_group;
    private AlphaAnimation alpha;
    private RotateAnimation rotate;
    private ScaleAnimation scale;
    private AnimationSet set;
    private TranslateAnimation translate;

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

        initView();
    }

    private void initView() {
        btn_alpha = (Button) findViewById(R.id.btn_alpha);
        btn_rotate = (Button) findViewById(R.id.btn_rotate);
        btn_scale = (Button) findViewById(R.id.btn_scale);
        btn_trans = (Button) findViewById(R.id.btn_trans);
        iv = (ImageView) findViewById(R.id.iv);
        btn_group = (Button) findViewById(R.id.btn_group);

        btn_alpha.setOnClickListener(this);
        btn_rotate.setOnClickListener(this);
        btn_scale.setOnClickListener(this);
        btn_trans.setOnClickListener(this);
        btn_group.setOnClickListener(this);

    }

    // TODO 补间动画/View动画;不会改变控件的真实坐标
    @Override
    public void onClick(View v) {
        switch (v.getId()){

            case R.id.btn_alpha:
                alpha = new AlphaAnimation(1.0f,0.0f);
                // 时间长度
                alpha.setDuration(2000);
                // 重复次数
                alpha.setRepeatCount(1);
                // 模式
                alpha.setRepeatMode(Animation.REVERSE);
                // 设置动画
                iv.startAnimation(alpha);
                break;

            case R.id.btn_rotate:
                // 图片旋转
//                RotateAnimation rotate = new RotateAnimation(0,360);
                // 图片中心旋转;0.5中心点
                rotate = new RotateAnimation(0.0f,360.0f, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
                rotate.setDuration(2000);
                // 重复次数
                rotate.setRepeatCount(1);
                // 模式
                rotate.setRepeatMode(Animation.REVERSE);
                // 设置动画
                iv.startAnimation(rotate);
                break;

            case R.id.btn_scale:
                // 缩放比,缩放参照物
                scale = new ScaleAnimation(1.0f,2.0f,1.0f,2.0f, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
                scale.setDuration(2000);
                // 重复次数
                scale.setRepeatCount(1);
                // 模式
                scale.setRepeatMode(Animation.REVERSE);
                // 设置动画
                iv.startAnimation(scale);
                break;

            case R.id.btn_trans:
                // 位移;x轴
                translate = new TranslateAnimation(Animation.RELATIVE_TO_PARENT,0,Animation.RELATIVE_TO_PARENT,0.3f,Animation.RELATIVE_TO_PARENT,0,Animation.RELATIVE_TO_PARENT,0);
                translate.setDuration(2000);
                // 位移点,停止
                translate.setFillAfter(true);
                // 设置动画
                iv.startAnimation(translate);
                break;

            case R.id.btn_group:
                set = new AnimationSet(true);
                // 图片中心旋转;0.5中心点
                rotate = new RotateAnimation(0.0f,360.0f, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
                rotate.setDuration(2000);
                // 重复次数
                rotate.setRepeatCount(1);
                // 模式
                rotate.setRepeatMode(Animation.REVERSE);

                // 位移;x轴
                translate = new TranslateAnimation(Animation.RELATIVE_TO_PARENT,0,Animation.RELATIVE_TO_PARENT,0.3f,Animation.RELATIVE_TO_PARENT,0,Animation.RELATIVE_TO_PARENT,0);
                translate.setDuration(2000);
                // 位移点,停止
                translate.setFillAfter(true);
                // 添加多个动画
                set.addAnimation(rotate);
                set.addAnimation(translate);

                iv.startAnimation(set);
                break;
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值