在Android中,要为GradientDrawable实现动画效果,可以使用属性动画(Property Animation)。以下是一个简单的示例,展示了如何使用属性动画为GradientDrawable的边框宽度和颜色添加动画效果。
GradientDrawable gradientDrawable = new GradientDrawable(); gradientDrawable.setShape(GradientDrawable.RECTANGLE); gradientDrawable.setCornerRadius(10); gradientDrawable.setStroke(5, Color.BLACK); gradientDrawable.setColor(Color.BLUE); 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秒内无限循环动画。你可以根据需要自定义动画的参数,例如持续时间、重复次数和重复模式等。