tags:

views:

38

answers:

2

How can I do this? There's a setAlpha but no getAlpha.

A: 

Actually, you need to use getOpacity() because get alpha existe only for ColorDrawable, Transformation and a few others. and you need to use it on the drawable of the ImageView not the view itself.

Sephy
Cool, thanks for the tip.
Joren
Hmm. getDrawable() is null if I set the ImageView src through XML instead of through setImageResource at run time. Any ideas?
Joren
Woops, I was setting background instead of src. getOpacity doesn't do anything useful though, it just returns one of 4 constants, I need something which returns the actual transparency value.
Joren
A: 

There is no easy way to do this. This is because an ImageView might have been set with a Bitmap, a StateListDrawable, a ColorDrawable, or something else entirely. Only one of those classes has a single color; on any other drawable the alpha might be different for each pixel (pixels are in ARGB format, w/ 1 byte each for alpha, red, green, and blue). I'm pretty sure the setAlpha() method only works on drawables that support it, as mentioned by Sephy above.

What exactly do you need to know about the image's transparency? If you know what the ImageView will be filled with ahead of time, then perhaps you can extract the alpha beforehand. If you don't have direct access to the alpha, but you are able to determine the color, then the alpha will be equal to color >>> 24.

Neil Traft
I'm animating some ImageViews using an AlphaAnimation and I want to check if their alpha is 1 to determine if I should run my fadeIn animation on them.
Joren
Is this just to know the difference between which views were already animated and which views weren't? You should probably come up with some other way of figuring that out, for example if `imgView.getAnimation() != fadeIn` then you assume that the animation has yet to be applied. Alternatively, you could subclass `ImageView` and override the `onSetAlpha()` method; that's the method that will be called when alpha changes due to animation.
Neil Traft