Android color rendering - android

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.

Related

How to make a Blur background depends on the Image color that you are loading in Android

is there any systemical possible way (i mean in code) to make a blur background depends on the image user open, the color must be similar to the image that will open.
for example, the background on this page is grey.
You will need to get the dominant color from the image you are using then create a gradient drawable between a starting color and your dominant color. There are multiple ways to find dominant colors which you can read up on here: Finding the dominant color of an image in an Android #drawable
From there you create a drawable and set the background of your view to that drawable:
// get the drawable from the image view
// could also be pulled from resources if available
Bitmap bm=((BitmapDrawable)imageView.getDrawable()).getBitmap();
int color = getDominantColor(bm);
GradientDrawable gradient = new GradientDrawable(
GradientDrawable.Orientation.TOP_BOTTOM,
new int[] {0xFFF,color});
gradient.setCornerRadius(15f);
contentView.setBackground(gradient);
See this library to make blur image https://github.com/wasabeef/Blurry

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

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 modify the color of a 9patch image Android ?

I don't really understand how to create a 9patch image, but I found an image which is working on my fragment. The problem is that the color outside the border is not the color of the background. I tried changing the color of the pixels from the image to the color of the background but the resulting image doesn't work anymore.
This is the image which is working but has the wrong color:
http://i.stack.imgur.com/cJBfV.png
How can I change the color of the pixels that are outside the border, or how can I create a new 9patch image that looks like that ?
You can use DrawableCompat with supprot v4.
The next code shows how you can change Toast color.
As you know, toast background is a 9patch Drawable named toast_frame.9.png (you can find it in your sdk dir).
Toast toast = Toast.makeText(this, content, Toast.LENGTH_SHORT);
toastView.findViewById(android.R.id.message);
View toastView = toast.getView();
Drawable toastBg = toastView.getBackground();
Drawable drawable = tintDrawable(toastBg, ColorStateList.valueOf(Color.RED));
toastView.setBackground(drawable);
toast.setView(toastView);
toast.show();
public Drawable tintDrawable(Drawable drawable, ColorStateList colors) {
final Drawable wrappedDrawable = DrawableCompat.wrap(drawable);
DrawableCompat.setTintList(wrappedDrawable, colors);
return wrappedDrawable;
}
If you like to learn more, click this:
http://www.race604.com/tint-drawable
You can edit the 9 patch using the Draw 9-patch tool that is provided with the Android SDK. However as your image already includes the 9 patch stretchable area you can just edit the colors in an image editor, such as GIMP or Photoshop. Ensure you rename your image to use the .9.png extension to allow it to be recognized as a 9 patch image.
http://developer.android.com/tools/help/draw9patch.html
Nine-patch are normal png files. They are just interpreted differently by any software able to display nine-patch. In other words, you can edit them with any png editor.
There is also some dedicated tools (for instance: in android sdk /tools/draw9patch) that display the result in different sizes.
How are they interpreted then ?
The first and last rows + the first and last columns of pixels in a nine-patch image contains only black or transparent pixels (and those first/last rows/columns aren't displayed).
The (intersection of) black pixels on left and top edges defines stretchable areas. (you can have more than one)
The (intersection of) black pixels on right and bottom edges define the content area. (you can only have one)

Android: Way around View.setAlpha?

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

Categories

Resources