属性动画

本文介绍了一个使用Android视图动画的实例,展示了如何通过代码实现按钮点击触发不同类型的动画效果,包括透明度变化、旋转、位置移动及组合动画等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageButton imageButton;
    private Button dan;
    private Button zhuan;
    private Button yi;
    private Button zuhe;
    private Button suo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Animator animator = AnimatorInflater.loadAnimator(this, R.animator.objectanimator);
        animator.setTarget(imageButton);
        animator.start();

        initView();

    }

    private void initView() {
        imageButton = (ImageButton) findViewById(R.id.imageButton);
        dan = (Button) findViewById(R.id.button);
        zhuan = (Button) findViewById(R.id.button2);
        yi = (Button) findViewById(R.id.button3);
        zuhe = (Button) findViewById(R.id.button4);
        suo = (Button) findViewById(R.id.button5);

        dan.setOnClickListener(this);
        zhuan.setOnClickListener(this);
        yi.setOnClickListener(this);
        zuhe.setOnClickListener(this);
        suo.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.button:
                ObjectAnimator animator1 = ObjectAnimator.ofFloat(imageButton, "alpha", 1f, 0f, 1f);
                animator1.setDuration(5000);
                animator1.start();
                break;
            case R.id.button2:
                ObjectAnimator animator2 = ObjectAnimator.ofFloat(imageButton, "rotationX", 0f, 360.0f);
                animator2.setDuration(5000);
                animator2.start();
                break;
            case R.id.button3:
                ObjectAnimator animator3 = ObjectAnimator.ofFloat(imageButton, "translationY", 0f, 500f);
                animator3.setDuration(5000);
                animator3.start();
                break;
            case R.id.button4:
                AnimatorSet set = new AnimatorSet();
                ObjectAnimator animator22 = ObjectAnimator.ofFloat(imageButton, "rotationX", 0f, 360.0f);
                ObjectAnimator animator33 = ObjectAnimator.ofFloat(imageButton, "translationX", -500f, 0,500f,0f);
                set.playTogether(animator22,animator33);
                set.setDuration(5000);
                set.start();
                break;
            case R.id.button5:
                ObjectAnimator scaleX = ObjectAnimator.ofFloat(imageButton, "scaleX", 1f, 0f,1f);
                scaleX.setDuration(5000);
                scaleX.start();
                break;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值