开发者

CGColorGetComponents equivalent in Android

I'm doing porting app from Iphone to Android related to RGB colors.

Is there any equivalent for CGColorGetCompon开发者_开发知识库ents in Android?


I usually get my argb values manually by using bit level operations. You can wrap this in a helper method that returns it as an array if it's more convenient.

public int[] getColorComponents(int color)
{       
    int alpha = (color >> 24) & 0xff;
    int red = (color >> 16) & 0xff;
    int green = (color >> 8) & 0xff;
    int blue = color & 0xff;
    return new int[] { alpha, red, green, blue };
}

If you need floats, you can do something like this

public float[] getColorComponents(int color)
{       
    int alpha = (color >> 24) & 0xff;
    int red = (color >> 16) & 0xff;
    int green = (color >> 8) & 0xff;
    int blue = color & 0xff;
    float a = (float) alpha / 255;
    float r = (float) red / 255;
    float g = (float) green / 255;
    float b = (float) blue / 255;
    return new float[] { a, r, g, b };
}

If you're doing this many times (such as looping through the pixels in a Bitmap), you probably want to use a single array and pass it in, replacing the values in the method instead of instantiating a new array.


In Android there's this way, without using (the sometime annoying Hex values) :

public static float[] CGColorGetComponents(CGColorRef color) {

    float[] components = new float[4];

    int currentRed = Color.red(color);
    components[0] = currentRed / 255f;

    int currentGreen = Color.green(color);
    components[1] = currentGreen / 255f;

    int currentBlue = Color.blue(color);
    components[2] = currentBlue / 255f;

    int currentAlpha = Color.alpha(color);
    components[3] = currentAlpha / 255f;

    return components;
}

For iOS to android equivalent elements I suggest you refer to http://docs.myappconverter.com/search.html?q=CGColor

PS: The MyAppConverter service is also free to use to convert an app from iOS to Android native code source, and at least you will have all components of your app ready to use in android.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜