CardView 不支持 TransitionDrawable?
CardView doesn't support TransitionDrawable?
我试图在 CardView
背景上设置开关颜色动画,但我得到的是:
Cannot resolve method 'setCardBackgroundColor(android.graphics.drawable.TransitionDrawable)'
如果CardView
不支持TransitionDrawable
,那么我们如何实现这样的东西呢?
public void setCardBackground(CardView cardView) {
ColorDrawable[] color = {new ColorDrawable(Color.BLUE), new ColorDrawable(Color.RED)};
TransitionDrawable trans = new TransitionDrawable(color);
//cardView.setCardBackgroundColor(trans);
trans.startTransition(5000);
}
你试过了吗View#setBackground()
?
public void setCardBackground(CardView cardView) {
ColorDrawable[] color = {new ColorDrawable(Color.BLUE), new ColorDrawable(Color.RED)};
TransitionDrawable trans = new TransitionDrawable(color);
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
cardView.setBackground(trans);
} else {
cardView.setBackgroundDrawable(trans);
}
trans.startTransition(5000);
}
azizbekian 的回答会破坏 CardView 的圆角。要设置一个的背景颜色,您应该使用 CardView 特定的方法 setCardBackgroundColor
.
这是一个保留它们的解决方案:
Kotlin
fun setCardBackground(cardView: CardView) {
val colors = intArrayOf(Color.BLACK, Color.RED)
ValueAnimator.ofArgb(*colors).apply {
duration = 5000
addUpdateListener {
cardView.setCardBackgroundColor(it.animatedValue as Int)
}
start()
}
}
Java
public void setCardBackground(CardView cardView) {
int[] colors = {Color.BLACK, Color.RED};
ValueAnimator animator = ValueAnimator.ofArgb(colors);
animator.setDuration(5000);
animator.addUpdateListener(animation ->
cardView.setCardBackgroundColor(((int) animator.getAnimatedValue()))
);
animator.start();
}
我试图在 CardView
背景上设置开关颜色动画,但我得到的是:
Cannot resolve method 'setCardBackgroundColor(android.graphics.drawable.TransitionDrawable)'
如果CardView
不支持TransitionDrawable
,那么我们如何实现这样的东西呢?
public void setCardBackground(CardView cardView) {
ColorDrawable[] color = {new ColorDrawable(Color.BLUE), new ColorDrawable(Color.RED)};
TransitionDrawable trans = new TransitionDrawable(color);
//cardView.setCardBackgroundColor(trans);
trans.startTransition(5000);
}
你试过了吗View#setBackground()
?
public void setCardBackground(CardView cardView) {
ColorDrawable[] color = {new ColorDrawable(Color.BLUE), new ColorDrawable(Color.RED)};
TransitionDrawable trans = new TransitionDrawable(color);
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
cardView.setBackground(trans);
} else {
cardView.setBackgroundDrawable(trans);
}
trans.startTransition(5000);
}
azizbekian 的回答会破坏 CardView 的圆角。要设置一个的背景颜色,您应该使用 CardView 特定的方法 setCardBackgroundColor
.
这是一个保留它们的解决方案:
Kotlin
fun setCardBackground(cardView: CardView) {
val colors = intArrayOf(Color.BLACK, Color.RED)
ValueAnimator.ofArgb(*colors).apply {
duration = 5000
addUpdateListener {
cardView.setCardBackgroundColor(it.animatedValue as Int)
}
start()
}
}
Java
public void setCardBackground(CardView cardView) {
int[] colors = {Color.BLACK, Color.RED};
ValueAnimator animator = ValueAnimator.ofArgb(colors);
animator.setDuration(5000);
animator.addUpdateListener(animation ->
cardView.setCardBackgroundColor(((int) animator.getAnimatedValue()))
);
animator.start();
}