阅读量:3
在Android中,要为GradientDrawable实现动画效果,可以使用属性动画(Property Animation)。以下是一个简单的示例,展示了如何使用属性动画为GradientDrawable的边框宽度和颜色添加动画效果。
- 首先,创建一个GradientDrawable对象并设置其属性:
GradientDrawable gradientDrawable = new GradientDrawable();
gradientDrawable.setShape(GradientDrawable.RECTANGLE);
gradientDrawable.setCornerRadius(10);
gradientDrawable.setStroke(5, Color.BLACK);
gradientDrawable.setColor(Color.BLUE);
- 创建一个属性动画对象,用于修改GradientDrawable的属性:
ObjectAnimator strokeAnimator = ObjectAnimator.ofInt(gradientDrawable, "strokeWidth", 5, 15);
strokeAnimator.setDuration(2000);
strokeAnimator.setRepeatCount(ObjectAnimator.INFINITE);
strokeAnimator.setRepeatMode(ObjectAnimator.RESTART);
ObjectAnimator colorAnimator = ObjectAnimator.ofArgb(gradientDrawable, "color", Color.BLUE, Color.RED);
colorAnimator.setDuration(2000);
colorAnimator.setRepeatCount(ObjectAnimator.INFINITE);
colorAnimator.setRepeatMode(ObjectAnimator.RESTART);
- 将两个动画对象添加到动画集合中,并启动动画:
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(strokeAnimator, colorAnimator);
animatorSet.start();
这样,GradientDrawable的边框宽度和颜色将在2秒内无限循环动画。你可以根据需要自定义动画的参数,例如持续时间、重复次数和重复模式等。
以上就是关于“android gradientdrawable怎样实现动画效果”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm