propertyAnimator(属性动画)之objectanimator(动画执行类)

本文介绍如何在Android应用中实现属性动画,包括直接定义属性动画、自定义实现及使用PropertyValuesHolder等方法,并提供完整代码示例。

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

1:获取一张图片,用系统图片也可以

<ImageView
    android:id="@+id/image"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:src="@mipmap/ic_launcher"/>
<Button
    android:id="@+id/bt_begin"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:text="开始动画"/>
2:接下来就在代码中实现属性动画

public class MainActivity extends AppCompatActivity {
    private ImageView image;
    private Button bt_begin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        image= (ImageView) findViewById(R.id.image);
        bt_begin= (Button) findViewById(R.id.bt_begin);
        //1:直接定义实现属性动画
        /*bt_begin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ObjectAnimator.ofFloat(image,"rotationY",0.0f,60.0f).setDuration(3000).start();
            }
        });*/
        //自定义实现属性动画
        /*bt_begin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ObjectAnimator animator = ObjectAnimator.ofFloat(image, "lianglei", 1.0f, 0.5f).setDuration(3000);
                animator.start();
                animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        float animatedValue = (float) animation.getAnimatedValue();
                        image.setAlpha(animatedValue);
                        image.setScaleX(animatedValue);
                        image.setScaleY(animatedValue);
                    }
                });
            }
        });*/
        //3: 通过PropertyValuesHolder实现属性动画
        bt_begin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f, 0.5f);
                PropertyValuesHolder rotationX = PropertyValuesHolder.ofFloat("rotationX", 0.0f, 30.0f);
                ObjectAnimator.ofPropertyValuesHolder(image,alpha,rotationX).setDuration(3000).start();
            }
        });
    }
}
3:推荐博客   http://blog.youkuaiyun.com/lmj623565791/article/details/38067475/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值