Android - Change only the alpha TextView's text - android

Say I have this code:
int color = tv.getCurrentTextColor();
How would I change only the Alpha on this color?
For Example:
if color is 0x00ffffff, how would I change it to 0xffffffff ?
Is there a method for that or I need to do some hex/int manipulations?

Well, the straight answer to your question of "if color is 0x00ffffff, how would I change it to 0xffffffff ?" to simply to use compound bitwise or:
color |= 0xff000000;.
But I reckon maybe some co-workers would rather you didn't do this kind of thing on the java-side of Android development: you have a wonderful helper class called graphics.Color provided by Android! It does just the same things underneath, but'll really help your code readibility, especially considering that an int type color in android isn't necessarily a hex color value but could also point to an artbirary id of a resource in the color xml. Argh.
An eg would be:
int color = tv.getCurrentTextColor();
int newColor = Color.argb(yourNewAlphaVal, Color.red(color), Color.green(color), Color.blue(color))
Not fast, but I'm guessing you don't need it to be.
Of course, this is a personal opinion about what to use here, and I hope this helps!

Related

How to get desired color when using bitmap tint multiply?

I have a bitmap which base color is #EBEBEB, I tint it using multiply mode with color #55AABB and as a result I get #4EAC9D color but I want to have #55AABB as a result color. I've seen that Cout = Csrc * Cdst on the documentation but I'm not able to get #55AABB as a result. Could someone help me?
Here is what is going on. Although it is pretty well explained in the docs. What you need is, I think, DST_IN.

How to make a color brighter

I have a color that I get in runtime. I need to find a way to make it a bit brighter. I found a few examples on line, none worked for me.
The only thing that made any difference to the color was changing to HSV. But I couldn't understand how it worked.
Use RGB values manually:
final int brighter = 25;
Color.rgb(Color.red(color) + brighter, Color.green(color) + brighter, Color.blue(color) + brighter);
You of course would have to make something that checks that any value does not get over 255.
EDIT Palette won't work with a single color.

RemoteView background tranparency

I've been trying to use remoteView.setFloat(R.id.remote_background, "setAlpha", (float) 0.7); to set background transparency/alpha, but I'm getting "Error loading widget". I've read that you can set transparency by adding a hex value in front of a color. But since I'm getting the color from ColorPicker and it's an integer value (example: -13890612), I don't know how to use it.
You should use hex notation to see the color as it is in format RRGGBB, so your -13890612 should be rather peeked as 2C0BCC which makes more sense as you can easily say what are values of each components of the color. And to add alpha channel (which is value from 0 - (full transparent) to 0xff (full opaque)), just OR the right value with your color and use. I.e. to make it semi transparent OR 0x80:
int rgb = 0x2C0BCC;
int argb = 0x80000000 | rgb;
USe setInt(R.id.remote_background, "setBackgroundColor", colorInt); instead.

How do I use access colours and gradients on Android?

So i'm having a problem with using decimal colors in android.
I'm getting color codes from an external database.
For Example:
16777215 is white
16711680 is red
Now I want to use this colors to create a GradientDrawable.
int color = myDbReader.getColor(); //returns the decimal color code
new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT, new int[] {color, 0 });
The code example produces always a completley white gradient object.
I tried to google how to convert the decimal colors in the right way.. But i didn't find anything.
Can anyone give me a hint how to use the decimal colors in the right way?
You should read about android colours.
Android colors are 32bit unsigned integers, not signed ones!
Also Android colors are using Alpha bit, please read the article and the solution will be clear.
Even if SO is extremely good source of information you should google it before asking, it was the first link on Google when I entered "colour android".
BTW, I'm not an android developer.
Android colors are ARGB units, so your color should actually consist of 4 bytes with 1 of them for the alpha value. In order to convert you can have something like this
0xFF000000 & yourColor
which will set the opacity of your color to 100%(alpha byte is set). Another option is to use Color.parseColor after converting your integer to a hexadecimal string. Also do not refer to colors by some magic constants. The static fields of the Color class are designed so that you don't have to do that.
So based on your suggestions I spent last night with reading about the different types of Color codes and finally found the solution:
The given numbers are access color codes so i only have to:
int color = myDbReader.getColor(); //returns the decimal color code
gradient = new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT, new int[] {Color.rgb((value/256/256)%256, (value/256)%256, value%256), 0 });
Thanks four your help!

Get the alpha of an ImageView

How can I do this? There's a setAlpha but no getAlpha.
Here ya go View.ALPHA.get(View)
You can use this with an if statement to get a Boolean value and check for the current alpha state of a view.
In case anyone just need get and set a controls visibility, getVisibility and setVisibility may be an alternative you can use:
if(vw.getVisibility() == View.VISIBLE)
{
// do something
}
and you can set the visibility:
vw.setVisibility(View.INVISIBLE);
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.
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.
You can't directly. You can workaround, provided you're setting the alpha programmatically, is to keep the alpha value when you set it. E.g.:
private int mCurrentAlpha;
private void setAlpha(int newAlpha){
mCurrentAlpha=newAlpha;
ImageView imageView=...
imageView.setAlpha(mCurrentAlpha);
}

Categories

Resources