Android Transparent View Color Calculation Logic - android

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

Related

Change color of a view dynamically depends on what color of background the view is in

In Android, is there a way to dynamically change the color of the view depending on what kind of background it is in?
For example, if I have a floating TextView with a white text color on a ListView with a black background, the text color will be visible clearly. However, as I scroll down, if the next section of ListView has a white background the texts in the TextView won't be visible anymore since it is white on white.
Is there any way in Android to change the text color of this TextView as it reaches the background with similar/same color?
Well, it depends on the type of background.
If you are talking about a background as in a view(linearlayout, surfaceview, etc) wit ha color, you have to get the position the textview has on the screen, and set a color based on the background color at that point. But it depends on the type of color. A bitmap allows you to get a specific pixel:
bmp.getPixel(x, y);
And then you can determine the color based on that.
However, with a SurfaceView it becomes harder because there is no native method for that.
Further, a gradient-filled background makes it harder to get at a particular point. But if you use a view and have set the backgroundcolor, you can:
if (view.getBackground() == Color.RED){//View = the view with a background set using setBackground or android:background="#color or #drawable or whatever"
tv.setTextColor(Color.WHITE); //then you adjsut the textview color
}
Please be aware that the above example only works if the view has a background defined as android:background or setBackground and there is a color. When you use a bitmap, you can get the pixel and the color based on that pixel
not hard stuff
if view.getBackground() == .RED{
yourStaff.text.setTextColor(Color.WHITE);
}

Fill shape with text with two different color

I tried to achieve this but I am only able to achieve to fill object (shape).
My requirement is to change text color along with shape filling.
Shape can be filled with percentage like till
10% to 50% = Green
51% to 80% = Yellow
81% to 100% = Red
When Yellow color fills background of ":" in shape, it will change color to "White" which is previously "Yellow". Size of this shape is also dynamic.
What I tried and achieved?
I am able to fill shape with percentage but failed to change color when it reaches to edge of text.
I wrote a custom view. You get this double color effect using Path APIs. But for Android 1+ compatibility, you should use Region API and above Kitkat (19+) you can use just Path API.
Let's go through the concept of how to achieve this effect step by step:
There are three shapes we need to draw - Outline Rounded Stroke + Orange Progress Bar + the text itself
We draw the stroke as it is
But for the Progress bar, we need to remove the text that intersects with it and basically make the text intersection transparent. (DIFFERENCE)
Also for the Progress Bar, we have to show only the part of the rectangle that intersects with the outer rounded stroke path. (INTERSECTION)
And similarly, for the text, on the left side we basically chop off the parts that intersects with the progress bar. And we only show the right side of the text that is orange in color. (DIFFERENCE again)
If you are using API 19+, this is how the critical code snippet looks like:
croppedProgressPath.op(progressPath, textPath, Path.Op.DIFFERENCE);
croppedProgressPath.op(progressStrokePath, Path.Op.INTERSECT);
————————————
croppedTextPath.op(textPath, progressPath, Path.Op.DIFFERENCE);
Lines here and here.
I’ve written a Proof of Concept for this project called Diffre on Github. If you wanna test it out first, all the code is in this repo.

How to make opacity of view with transparent, infliction, subtraction

How to make background opacity of the View (for ex. EditField )with transparent, infliction, subtraction "Screen" like it is in Photoshop?
I know if I set background to "#android:color/transparent" for EditField then white background will be visible, but not main backgrounf of the layout
I need to implement this one:
You can't do it simply.. not by any code.. the only way to do that is to get a drawable .png image(you need photoshop to create it) that has this shape(semi tranaparent on the frame ans fully transparent inthe center with rounded corners).. make it with small width .. and use 9patch tool for stretching (so you can get flexible width)...

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/

How to put time delay in COCOS2D Android

Similar to Android unlock pattern where in, initially all the circular cells will be in grey color. During when we draw some pattern, those cells touched will become green colored and if that pattern is not a correct one, then the cells touched will become red colored and after some 3 seconds, they become grey colored. I am doing similar thing using COCOS2D. I am getting difficulty in introducing that 3 seconds delay before making the cells back to grey colored. Any suggestions...Thanks.
You can use CCSequence in that first put CCDelayTime and in second put CCCallFuncN(which contain logic of again color your circle to grey)
Below i am providing you iOs code logic
id action1 = [DelayTime ...];
id action2 = [CallFunc ...];
id seq = [Sequence actions:action1, action2, nil];
[someNode runAction:seq];

Categories

Resources