I tried to use the same color used in iphone to my android device
This is the color for iphone.
(UIColor *)navBarColor {
return [UIColor colorWithRed:0.329f green:0.584f blue:0.898f alpha:1.0f];
}
I tried to convert the above using the below lines of code to use for android
String colorString =
String.format("%s%s%s%s",
Integer.toString(Math.round((1.0f*255)), 16),
Integer.toString(Math.round((102.0f/255.0f*255)), 16),
Integer.toString(Math.round((255.0f/255.0f*255)), 16),
Integer.toString(Math.round((204.0f/255.0f*255)), 16));
I got the hexadecimal like this ff5394e4 . But i am getting very different color in android can anyone help me in this?.
IPhone Color:
Android Color
My first guess would be that you need to round the converted values instead of just casting to an int.
For example, for a number 6.5, rounding would get you 7, but casting to an int gets you 6. This may be the source of your problem.
I believe the java code to round a number is Math.round(int).
Related
By code, I can make a button that inserts these 3 emojis into the text: ⚽️😈🐺
On many phones when the user clicks the button, though, the problem is that ⚽️😈🐺 displays as [X][X][X]. Or even worse, it displays only three empty spaces.
I would like to disable and hide my own built-in emoji-keypad on Android devices that do not display emojis correctly. Does anyone knows or have a tip on how to detect in code if a device has emoji support?
I have read that emoji is supported from android 4.1, but that is not my experience....
I just implemented a solution for this problem myself. The nice thing with Android is that it is open source so that when you come around problems like these, there's a good chance you can find an approach to help you.
In the Android Open Source Project, you can find a method where they use Paint.hasGlyph to detect whether a font exists for a given emoji. However, as this method is not available before API 23, they also do test renders and compare the result against the width of 'tofu' (the [x] character you mention in your post.)
There are some other failings with this approach, but it should be enough to get you started.
Google source:
https://android.googlesource.com/platform/packages/inputmethods/LatinIME/+/master/java/src/com/android/inputmethod/keyboard/emoji/EmojiCategory.java#441
https://android.googlesource.com/platform/packages/inputmethods/LatinIME/+/master/java/src/com/android/inputmethod/keyboard/KeyboardLayoutSet.java
Based on Jason Gore answer:
For example create boolean canShowFlagEmoji:
private static boolean canShowFlagEmoji() {
Paint paint = new Paint();
String switzerland = "\uD83C\uDDE8\uD83C\uDDED"; // Here enter Surrogates of Emoji
try {
return paint.hasGlyph(switzerland);
} catch (NoSuchMethodError e) {
// Compare display width of single-codepoint emoji to width of flag emoji to determine
// whether flag is rendered as single glyph or two adjacent regional indicator symbols.
float flagWidth = paint.measureText(switzerland);
float standardWidth = paint.measureText("\uD83D\uDC27"); // U+1F427 Penguin
return flagWidth < standardWidth * 1.25;
// This assumes that a valid glyph for the flag emoji must be less than 1.25 times
// the width of the penguin.
}
}
And then in code whenever when you need to check if emoji is available:
if (canShowFlagEmoji()){
// Code when FlagEmoji is available
} else {
// And when not
}
Surrogates of emoji you can get here, when you click on detail.
An alternative option might be to include the Android "Emoji Compatibility" library, which would detect and add any required Emoji characters to apps running on Android 4.4 (API 19) and later: https://developer.android.com/topic/libraries/support-library/preview/emoji-compat.html
final Paint paint = new Paint();
final boolean isEmojiRendered;
if (VERSION.SDK_INT >= VERSION_CODES.M) {
isEmojiRendered = paint.hasGlyph(emoji);
}
else{
isEmojiRendered = paint.measureText(emoji) > 7;
}
The width > 7 part is particularly hacky, I would expect the value to be 0.0 for non-renderable emoji, but across a few devices, I found that the value actually ranged around 3.0 to 6.0 for non-renderable, and 12.0 to 15.0 for renderable. Your results may vary so you might want to test that. I believe the font size also has an effect on the output of measureText() so keep that in mind.
The second part was answerd by RogueBaneling here how can I check if my device is capable to render Emoji images correctly?
I am attempting to draw a line (with line()) with square endings, but can not find any documentation telling me how to do it. So far all my lines end in little triangles.
Can this be done? Is its something to do with lineType?
EDIT: An example of my usage...
line(ptr_to_mat, Point(10,25), Point(30,25), Scalar(255,0,0,0),4, 8, 0);
EDIT: I should have mentioned, this is running on an Android device.
According to OpenCV docs, function line() will draw thick lines with rounding endings.
That said, you cannot directly get over this. But, you can draw it several times with thickness=1 or draw a filled rectangle instead to achieve your goal (both ugly though :():
line(ptr_to_mat, Point(10,23), Point(30,23), CV_RGB(255,0,0), 1, 8, 0);
line(ptr_to_mat, Point(10,24), Point(30,24), CV_RGB(255,0,0), 1, 8, 0);
line(ptr_to_mat, Point(10,25), Point(30,25), CV_RGB(255,0,0), 1, 8, 0);
line(ptr_to_mat, Point(10,26), Point(30,26), CV_RGB(255,0,0), 1, 8, 0);
You will get:
Also from the Docs - "The line is clipped by the image boundaries." - maybe you can clip the round edges out of the line by drawing the line inside of a Mat's Region Of Interest (ROI, a.k.a. submat). Basically you have to set the dimensions of the ROI with the size of the line minus the round tips. If this sound too complex, it is simpler than that.
It is not very elegant but considering that the clipping is done for free within the line drawing algorithm this probably more efficient than drawing several lines / rectangles.
Another thing, try to change the lineType parameter, it might change the tip rendering.
lineType:
8 (or omitted) - 8-connected line.
4 - 4-connected line.
CV_AA - antialiased line.
Or you can always implement your own line renderer?
I never said it would be pretty :D
the topic is a bit old but I faced this problem recently and I found a solution by using the library wxpython. Maybe it could help, here is a code example:
import wx
app = wx.App()
frame = wx.Frame(None, title="Draw on Image")
imgBit = wx.Bitmap(width=512, height=512, depth=1)
dc = wx.MemoryDC(imgBit)
pen = wx.Pen(wx.RED, 3)
pen.SetCap(wx.CAP_BUTT)
dc.SetPen(pen)
dc.DrawLines(((32, 32), (64, 32)))
dc.SelectObject(wx.NullBitmap)
imgBit.SaveFile("bitmap.png", wx.BITMAP_TYPE_PNG)
Hope it will help someone in the future
I am porting a cocos2d iOS game to cocos2d-x Android and the cocos2d game code has this method sizeWithFont, but I do not have this method to use in Android. So what method can I use to replace sizeWithFont when using Cocos2d-x for Android
Unfortunately like Ant said that is a iOS specific feature, and you want to avoid using anything with UI or NS in front of it.
That being said there is a way to calculate the bounds of text with a font and a size
CCLabelTTF *label = CCLabelTTF::create("my text", "Arial", 48);
CCLog("label size: %f,%f", timeLabel->boundingBox().size.width, timeLabel->boundingBox().size.height);
You could create a function like this
CCSize sizeWithFont(const char *string, const char *fontName, float fontSize) {
CCLabelTTF *label = CCLabelTTF::create(string, fontName, fontSize);
CCSize size = label->boundingBox().size;
delete label;
return size;
}
The method you are referring to is [UIFont fontWithSize], which is a UIKit method and has nothing to do with Cocos2d.
Since this is a feature of iOS and not Cocos2d (as is, presumably, the rest of your menu if you are using UIKit), you will find that you will need to build your menus using Cocos2d-x itself, instead.
There is a tutorial on working with labels here:
http://www.cocos2d-x.org/projects/cocos2d-x/wiki/Text_Labels#Creating-labels-Simple-way
I want to load .png file via asset manager which is provided by android sdk. AssetManager manager; /........./ BitmapFactory.decodeStream(manager.open(path));
It returns BGR format data but opengl es 2.0 uses RGB format so , Blue seems red , red seems blue, how odd.
Is there any solution for it?
I use Nvıdia Tegra 2 (Android 2.2) device for test the application along with c++ via JNI.
You must know the number of bits for colors, let's say n bit is for a color, so the first n bit represents BLUE, the second n bits represent GREEN and the final n bits represent RED in the input. You need to swap these bit groups into the correct order, like this:
output = (input << (2 * n)) + (input << n >> n) + (input >> (2 * n));
To be able to use this solution you need to find out how much is n.
Recent versions of OpenGL, also provide BGR input formats; OpenGL-ES not, unfortunatly. Since you're on Android you have to deal with OpenGL-ES.
If you're using a fragment shader it is also trivial to apply a rgb→bgr swizzle, which if probably the easiest way to overcome this problem.
I want to display two Unicode characters in TextView, but I get squares:
- ℏ (reduced Planck constant/PLANCK CONSTANT OVER TWO PI http://www.fileformat.info/info/unicode/char/210f/index.htm)
- ℞ (PRESCRIPTION TAKE http://www.fileformat.info/info/unicode/char/211e/index.htm)
I know that not all Unicode characters are supported by default Android font, but reduced "h-bar" is Latin character and it's one of the fundamental physical constants. Can anyone confirm that I making everything right? And if, how to solve this problem (third-part font is the only solution)?
view = new TextView(this);
int[] codePoint = { 0x210f, 0x211e };
String hhh = new String(codePoint, 0, codePoint.length);
view.setText(hhh);
According to the character set page the Droid fonts don't support U+210F (ℏ), but they do support U+0127 (ħ), so you might consider using an italic font and U+0127 instead.
add font containing characters to /system/fonts,
modify /system/etc/fallback_fonts.xml so android finds font then
reboot.
Everything should be working now