简体   繁体   English

Imageview 图像改变时改变

[英]Imageview changes when Image changes

I would like to somehow attach imageview to another image and make it so that when this image changes the imageview changes too.我想以某种方式将 imageview 附加到另一张图片上,这样当这张图片发生变化时,imageview 也会发生变化。 I have a "home" controller with an avatar imageview我有一个“家” controller 头像 imageview

    user = ViewFactory.getInstance().getUser();

    if(user.getAvatar() != null) {
        imgPreview.setImage(user.getAvatar());
    }

and another "settings" controller which allows to change avatar和另一个允许更改头像的“设置” controller

    ViewFactory.getInstance().getUser().setAvatar(new Image(avatar.getAbsolutePath()));

and I would like to make it so that when I change avatar in settings it also changes it in home, a kind of listener for home imageview that changes when the value in user.getAvatar() is changed, but i dont know how to archive it我想这样做,当我在设置中更改头像时,它也会在家里更改它,这是一种家庭 imageview 的侦听器,当 user.getAvatar() 中的值更改时会更改,但我不知道如何存档它

Make the avatar in your User model object an ObjectProperty<Image> (adding appropriate get/set and property accessors);使您的User model object 中的头像成为ObjectProperty<Image> (添加适当的获取/设置和属性访问器);

ObjectProperty<Image> avatar;

Bind the image property in the ImageView to it.ImageView中的image属性绑定到它。

imageView.imageProperty().bind(user.avatarProperty());   

Then, when you set the avatar image property to a new value,然后,当您将头像图像属性设置为新值时,

user.setAvatar(newImage);

the image displayed in the image view will also change.图像视图中显示的图像也会发生变化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM