Android: Way around View.setAlpha? - android

So I am trying to adjust the alpha of a view programmatically. As of API 11 there is View.setAlpha(alpha) which works great. My app otherwise supports back to API 4 so is there another way to set the alpha for a view?

Apply an AlphaAnimation to the view.

Try View.setBackgroundColor with a color that includes transparency as the first set of hex values before the RGB values (for fully visible red that would be #FFFF0000 - FF for alpha and FF0000 for RGB).
How to set background color of a View

Related

Change image fill color in Android

I have an ImageView with the source being an ImageAsset.
My image is a circle with a plus and I am looking to colour the inside of the circle only. How do I do that?
Using setBackgroundColor colours the whole of the view thus giving a square background like this:
.
You can apply a color tint to your ImageView. This will not affect your background if it's transparent, just the colored part (which is the circle and the plus)
imageView.setColorFilter(ContextCompat.getColor(context, R.color.COLOR_YOUR_COLOR),
android.graphics.PorterDuff.Mode.MULTIPLY);
Update
Using android.graphics.PorterDuff.Mode.SRC_IN will also work (if you don't need to multiply the source and destination pixels)
Update 2
Since the destination part is not colored than the best solution is to use VectorDrawable.
You can use VectorChildFinder to find inner parts of your SVG resource and change its color.
VectorChildFinder vector = new VectorChildFinder(this, R.drawable.my_vector, imageView);
VectorDrawableCompat.VFullPath path1 = vector.findPathByName("path1");
path1.setFillColor(Color.RED);
imageView.invalidate();
to create your SVG, follow these steps :
Just click right button on folder(drawable for ex.) and choose:
then choose:

Android Transparent View Color Calculation Logic

I am wondering what logic Android uses to calculate the final color of a view given two semi-transparent views being stacked.
What I need to do specifically is take two semi-transparent views (let's say with backgrounds #66000000 and #33000000) and figure out what the equivalent singular view COLOR would be.
I have tried ColorUtils.blendARGB but this does not give me the correct value.
Ok this formula seems to work for what I needed:
C1=[R1,G1,B1] is the foreground pixel color.
C2=[R2,G2,B2] is the background pixel color.
p1 is the opacity percentage of the foreground pixel. (0.4) in my case
p2 is the opacity percentage of the background pixel. (0.2)
NewPixelColor = (p1*c1+p2*c2-p1*p2*c2)/(p1+p2-p1*p2)
NewPixelOpacity = p1+p2-p1*p2

Getting current stroke color programmatically

I am able to set my shape's stroke color by doing this:
GradientDrawable shape = (GradientDrawable) myview.getBackground();
shape.setStroke(strokeWidth, color);
Q: How do I get the current color of my shape's stroke programmatically?
Note that I am using API level 19 (so I cannot use shape.getColor() which anyway would return the filling color of the shape I believe, and not it's stroke's color).
As #pskink mentioned, Checking android sources shows that there is no such API for getting the current stroke color (which is odd and a pity).
Eventually what I did to overcome my problem, is keeping my own variable in code that indicates the current stroke color.

Replace single (black) color in ShapeDrawable with another color in Android

Let's say I have an ShapeDrawable in Android. Not a bitmap. I would like to replace single color (stroke color) in it by another color dynamically. The new desired color is not known at design time and can't be put into drawable resource.
For example, go from picture 1 (where black color represents the color I want to replace, and checkered background is the background not covered by the shape outline):
to image 2, where red is the color I want:
I can build the shape using alpha masks if necessary - i.e. have the white or black colors transparent, if necessary, or make the outline green, for example. The white fill color must remain white in the final result.
Is it possible to achieve that with standard color filters - ColorMatrixColorFilter, or PorterDuffColorFilter ?
If so, I'm having hard time figuring out specific filter.
I assume custom ColorFilter is not possible.
In the specific case of having a black border that you want to set to an arbitrary color, and a white interior, you can use a PorterDuff filter in ADD mode. Since it's saturating it won't have any effect on the white area, and since black is zero adding the color will effectively set all black areas to that color.
e.g. to set it to red:
drawable.setColorFilter(0x00ff0000, PorterDuff.Mode.ADD);
This tutorial may be helpful. According to this one, you can replace the white color portion of an image with desired color.
Changing color of drawable icon programmatically http://takeoffandroid.com/uncategorized/changing-color-of-drawable-icon-programmatically/

Android color rendering

Given:
an #color specified in RGB, no alpha
a png that is filled with pixels at that same RGB value, no alpha
a layout with the color as background and containing the image
Will the rendering always - on a single given device - be flawless? Or is there a possibility of some color difference between the image and the background?
Yes. The color rendered on a device for an image filled with a given RGB value and a layout with the same value specified will be the same. That is, you will not notice a difference between your image and the layout color.

Categories

Resources