I am trying to find the right approach in setting the placeholder image/resource (VectorDrawable resource in my case) for an ImageView that gets set, with the target image bitmap when it is available.
There are two approaches I saw in guides. I can either set a background using the background property on XML or programmatically using setBackground(). Then, I would set the target image using setImageDrawable().
The second approach is to use setImageDrawable() for the placeholder and later use setImageDrawable() again for the target image.
Both approaches work but I have noticed some UI lag when using the first approach. I am not sure if it is caused by the approach or not.
So, I want to know. What is the correct approach in using placeholders and why?
Thank you.
The second approach is always the best for this task.
But still, if you want to go with the first approach then the correct way to use it is like this
int sdk = android.os.Build.VERSION.SDK_INT;
if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
setBackgroundDrawable();
} else {
setBackground();
}
when load image in ImageView changed src value, it's mean not changing background.
change background is expensive for android, It gets worse when we want to load a lot of images (like ListView, RecyclerView, ...).
for having better performance should be changed image src, not changing image background.
for changing image src you can use
image.setImageResource()
or
image.setImageDrawable()
change background suitable for a single image
In addition, all image loader support placeholder if using that's the same doesn't need set manually.
Related
I'd like to create a layout displaying a music album's tracks, with the text and background colors being taken from the album cover's color palette. I know how to retrieve the color palette, but I am unsure how to easily apply it programmatically to an entire layout, without manually finding each individual view and changing the colors appropriately. I have only found solutions where themes are changed before creating the views, but those themes are already predefined in the resources, not dynamically generated on the album cover.
Is there some way for me to write a layout in XML referencing colors like "color1" and "color2", and then at runtime assigning an actual value to these colors, such that I don't need to painstakingly modify every view programmatically myself?
I'm not sure it's possible to do that through XML normally, since you're referring to resource IDs and those resources are fixed at runtime. The Theme class also links attributes to resources, so you can't just inject the values you want (as far as I know) into a Theme and then apply that to the Activity. (I don't know a lot about the styling/theming system though so it's possible you can do some wild stuff with it!)
There's a couple of things you could look into though. If you're happy to use the new Material 3 system, that has dynamic colour baked into it, including theming parts of the app with colours derived from content like album art.
But from that outline, it looks like it pulls a single colour from the image, and derives a couple of complementary, neutral colours from it - so it's not pulling multiple colours from the image like the Palette library does. The Material 3 stuff has a particular tone to it (limited colour variation, not too much contrast, consistent look) and if that works for you, great! But if you want a more vibrant palette, you might need to use Palette instead (that link up there shows how you're meant to use it, the examples are basically "apply colour to each view/component in code")
The other thing you could try is using data binding, and instead of using resource IDs and attributes in your XML, you could reference properties on a ViewModel. So you could create LiveDatas for things like primaryColour, secondaryColour etc, and bind to those when setting the colour attributes in your XML. And then by updating a value in the ViewModel, any views bound to that property will see the change. And that binding is defined in the XML, you don't need to know which View is using which colour in your code.
I've never actually used data binding, never mind for colour updates, so I'm not sure how well it would work or if you need to give things a kick to get them to refresh visually - I'd imagine it just works like setting a new value through code, but I'm not sure so I just want to be clear about that!
But if you want smooth colour transitions (e.g. switching albums fades the colours) then I'm not sure there's anything that does that out of the box - maybe the Material 3 stuff does it. You could probably do it with the ViewModel approach though - when you set a new colour, kick off a coroutine that interpolates between the current value and the new one over 1 second or whatever, updating the LiveData every tick.
I'm using ScalaFX, which is currently proving itself to be thoroughly useless and bugging out anytime I try to use css. Is there any way to change the fill color of a Slider without using css, or at least without separate css files?
I've managed to change the track color with slider.setStyle("-fx-control-inner-background: #f08080;"), but can't get the thumb working.
You could use the -fx-base property, which sets the component colors palette according to the value you specify.
Example:
slider.setStyle("-fx-base: #f08080;");
Result:
However why don't you consider using a CSS file? It simplifies everything and allows you to make a lot more customizations. Take a look at the Slider JavaFX CSS reference, I think you will find what you're looking for.
I want to compare the background resource of the TextView to another drawable, but this does not work.
Most of answers of this question said that I should use getConstantState() but this doesn't work.
How can I solve this problem?
Below is my code.
JAVA
TextView selected = findViewById(textViewIds.get((9*((9-i)))+(j-1)));
if (selected.getBackground().getConstantState()
.equals(AppCompatResources.getDrawable(Activity4.this, R.drawable.normal_light).getConstantState()))
selected.setBackground(AppCompatResources.getDrawable(Activity4.this, R.drawable.blue));
else
selected.setBackground(AppCompatResources.getDrawable(Activity4.this, R.drawable.normal_light));
getConstantState() does not work good in android and it isn't reliable.
Try using a different approach by exploiting the Tags of the element / background.
You could set the Tag of the background to any value you want and later comparing the Tags to another. You would use
if (selected.getBackground().getTag().toString().equals("background1")) {
...
}
I am using a Vaadin 10 (Flow) Grid to stream data to new rows, and I am trying to add a custom image icon to my TemplateRenderer.
I have a TemplateRenderer setup like this:
TemplateRenderer<TradeUni> sizee = TemplateRenderer.<TradeUni>
of("<div class$=\"[[item.class]]\">[[item.size]]</div>")
.withProperty("class", cssClassProvider)
.withProperty("size", TradeUni::getSize);
Which displays my numbers correctly in the grid cell like this:
I would like to have my image rendered inside the same cell, to the left of the numbers.
This was my (very crude) attempt to import it using html:
TemplateRenderer<TradeUni> sizee = TemplateRenderer.<TradeUni>
of("<div class$=\"[[item.class]]\"><img src=\"i.imgur.com/3LQBglR.png\">[[item.size]]</div>")
.withProperty("class", cssClassProvider)
.withProperty("size", TradeUni::getSize);
Which give's me this:
I think that I might have to wrap the image and numbers into a HorizontalLayout with the image being a separate component - I think I could handle that, but I am unsure of how to do the actual rendering of the image. I can probably accomplish it simply with the internal Vaadin Icons set, but I need to use my own little images.. all icons that I am going to use will be at or less than 25 x 25px.
Thank you so much in advance!
I think you're on the right track, but there's one small detail that causes you trouble. The URL i.imgur.com/3LQBglR.png doesn't define a protocol, which means that the entire string will be treated as a path relative to the location of the hosting page. It will thus try to find a file in a directory named i.imgur.com on your own server.
To fix this, you need to also include the protocol in the image URL, i.e. https://i.imgur.com/3LQBglR.png.
I can also offer a small suggestion for how to make the code easier to read. HTML also supports using ' for enclosing attribute values. This is more convenient to use from Java string since you don't need to use \ to escape ' characters. Your template string could thus be "<div class$='[[item.class]]'><img src='https://i.imgur.com/3LQBglR.png'>[[item.size]]</div>".
I have an app that I want to have an ImageView element in the FXML file display either an image, or a blank section dependant on user input. I was thinking either to somehow surround the FXML imageview element in an if/else block, or to pass either the image filepath, but both of these feel "off" (the former adding back end loging to front end code, the latter having no image path if set to not display and having to handle that somehow).
Would anyone know of a better way to perform an operation like what I'm describing above?
control are initialized in fxml are static, so it not possible to be created imageview directly...
you can use another way i think its work...
1.take a anchor pane,vbox,hbox etc any container.
2.and set image on runtime in this container.
3.like example below.
AnchorPane pne = new AnchorPane();
if(flag==1)
pne.getChildren.add(new ImageView(new Image(getClass().getResourceAsStream("image.png"))));