动态改变shape color

根据服务器返回的颜色值来动态改变界面的效果:

Android里面经常会使用shape来定制一些View的背景

可以修改View的背景颜色,形状等属性

  1. View.setBackgroundColor(color);  
  2. View.setBackgroundDrawable(R.drawable.shape);  
不能实现我想要的需求,每设置一次Background,Background色就会被替换掉

  1. GradientDrawable myGrad = (GradientDrawable)view.getBackground();  
  2. myGrad.setColor(color);  


实例代码:
holder.rlRoot.setBackgroundResource(R.drawable.shape_special); // 圆角矩形
GradientDrawable myGrad = (GradientDrawable)holder.rlRoot.getBackground();
myGrad.setColor(Color.parseColor(String.valueOf(map.get("color"))));



在JavaFX中,动态改变颜色可以通过`Color`类的实例和`Timeline`或者`TimelineTransition`类来实现。JavaFX的`Color`类代表了颜色本身,而`Timeline`用于创建动画序列,可以用来控制颜色的变化过程。 以下是一个简单的例子,展示如何使用`Timeline`改变一个`Rectangle`的颜色: ```java import javafx.application.Application; import javafx.animation.ColorTransition; import javafx.animation.KeyFrame; import javafx.animation.Timeline; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.layout.StackPane; import javafx.scene.paint.Color; import javafx.scene.shape.Rectangle; import javafx.stage.Stage; import javafx.util.Duration; public class ColorChangeExample extends Application { private Rectangle rectangle; private Timeline colorTimeline; @Override public void start(Stage primaryStage) { rectangle = new Rectangle(100, 100); rectangle.setFill(Color.BLUE); colorTimeline = new Timeline( new KeyFrame(Duration.seconds(1), event -> rectangle.setFill(getNewColor())), new KeyFrame(Duration.seconds(2), event -> rectangle.setFill(getNewColor())) ); colorTimeline.setCycleCount(Timeline.INDEFINITE); colorTimeline.play(); StackPane root = new StackPane(); root.getChildren().add(rectangle); root.setAlignment(Pos.CENTER); Scene scene = new Scene(root, 400, 400); primaryStage.setScene(scene); primaryStage.show(); } private Color getNewColor() { // 这里你可以定义颜色变化的逻辑,例如从蓝色到红色 return Color.RED; } public static void main(String[] args) { launch(args); } } ``` 在这个例子中,`getNewColor`方法可以根据需要返回不同的颜色,`Timeline`会按照定义的时间序列来改变`Rectangle`的颜色。你可以在`getNewColor`方法中添加更复杂的颜色变换逻辑,比如使用`Color.interpolate()`方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值