android opengl api sample: Spritetext - android

I want to draw a green point with the text in the sample code of android opengl: spritetext(the code please visit the following site: https://gitorious.org/0xdroid/development/source/9ca6f2c0573e7c6e76515be664e633f03a1a4358:samples/ApiDemos/src/com/example/android/apis/graphics/spritetext/LabelMaker.java).
I added the following code in the function: public int add(GL10 gl, Drawable background, String text, Paint textPaint,
int minWidth, int minHeight) of the LakerMarker.java:
Paint p = new Paint();
p.setColor(Color.GREEN);
p.setStrokeWidth(20);
if (drawText) {
mCanvas.drawText(text,
u + padding.left + centerOffsetWidth,
vBase + padding.top + centerOffsetHeight,
textPaint);// in the sample code, just this.
mCanvas.drawPoint( u + padding.left + centerOffsetWidth,
vBase + padding.top + centerOffsetHeight,
p);// this code is to draw the green point
}
Then I run, get the following image result.I want to only the left green point with each text, but there were two points(one is in left, another one is in right) with each text, How to deal with this problem? Thanks in advance!

Related

Android: drawTextOnPath multiline

I current have a custom view that I override the onDraw and draw an arc. I want to draw text within this arc. To do this, I use drawTextOnPath and this display curved text at the top of the arc. However, sometimes the text is quite long, so I want to allow it to go on to multiple lines.
I currently use code like this to draw on to multiple lines: -
textView.getPaint().getTextBounds(s, 0,
s.length(), r);
int yOffset=r.height() + textSpacing;
int textStart=0;
int numberOfLines= (int) (r.width()/arcWidth) + 1;
for (int i=0; i < numberOfLines; i ++) {
canvas.drawTextOnPath(s.substring(textStart, textStart + s.length() / numberOfLines),
childHolder.path, 0, yOffset, paint);
yOffset+=r.height() +textSpacing;
textStart=s.length()/numberOfLines;
}
However, this obviously doesn't take into account how wide the text is further down the arc. Is there a way of doing this with using something like staticlayout/dynamiclayout (text does change a lot).
If anyone could point me in either something in android SDK I can use, or the maths to calculate the available width
This bit of code solved my issue: -
PathMeasure pm = new PathMeasure(path, false);
layout = new DynamicLayout(spannableText, spannableText, paint, (int) arcPathMeasure.getLength(), Layout.Alignment.ALIGN_CENTER, 1, 0, true);
Thanks pskink!

Android DrawArc SweepGradient over 360°

I'm trying to make a wheel of colours which allows the user to select between Solid color or Gradients.
The user can also move those colours around the wheel.
The problem is with the gradient. When the angle is over 360° the colour displayed is going back to the first colour instead of continue the gradient.
I'm using a custom view and the code I will paste is called from the onDraw method.
int start = Color.rgb((int)previous.startRed, (int)previous.startGreen, (int)previous.startBlue);
int end = Color.rgb((int)previous.endRed, (int)previous.endGreen, (int)previous.endBlue);
int[] colors = {start, end, end};
float from = from_angle / 360.0f;
float to = (from_angle + to_angle) / 360.0f;
float[] positions = {from,to, to};
Log.v("Print", "print positions " + positions[0] + " " + positions[1]);
Shader gradient = new SweepGradient (oval.centerX(), oval.centerY(),colors, positions);
paint.setShader(gradient);
canvas.drawArc(oval, from_angle, to_angle + 1, false, paint);
Here is a picture to show the problem I face.
Any hint would be appreciated. Thank you.

Android drawArc not drawing properly

I'm working on some kind of circular view where the Arc is drawn on every onDraw() call. Whole onDraw method looks like this
#Override
public void onDraw(Canvas canvas) {
float beg = 0;
float end;
if (items != null && items.size() > 0 && getSum() != 0)
for (Map.Entry<Object, ItemDescriptor> item : items.entrySet()) {
end = (item.getValue().getScore() / getSum()) * 360;
Log.d("jano", "drawing from " + beg + " to " + (beg + end));
canvas.drawArc(mBounds, beg, end, false, item.getValue().getPaint());
beg += end;
}
else {
canvas.drawArc(mBounds, beg, 360, false, defalutPaint);
}
}
Paint for drawing:
Paint paint = new Paint();
paint.setStyle(Paint.Style.STROKE);
paint.setColor(color);
paint.setStrokeWidth(outerWidth);
ItemDescriptor holds score based on which i can compute how to display circle (composed of some Arcs). Log output is always OK, but Arcs are most of time but not always correct. For example see green part on image below.
Can somebody of you give me some inspiration how to display it always correct? Whole project is on GitHub as Android Studio poject
Hmm I'm sorry, it just looks like the Genymotion not displaying this correctly. Real device and Android emulator displays the view without errors.

Painting text over background image is dim

I'm painting text over a background image on a canvas. I move the image interactively (like a Ouija board pointer). I've set the canvas to black, the pointer is red and I want to write white text over it so that the pointer has a player's name on it.
In Android 2.3.4 it appears as solid white text on top of the red pointer which is pretty clear, but I'd like to use any color. In Android 4.1.2 I can barely see the white text. Here's my code:
public Pointer(Context context) {
super(context);
paintBg = new Paint();
paintBg.setColor(Color.BLACK);
paintName = new Paint();
paintName.setColor(Color.WHITE);
paintName.setTextSize(50); // set text size
paintName.setStrokeWidth(5);
paintName.setTextAlign(Paint.Align.CENTER);
this.setImageResource(res); // pointer.png in res/drawable folder
Drawable d = getResources().getDrawable(res);
h = d.getIntrinsicHeight();
w = d.getIntrinsicWidth();
canvas = new Canvas();
canvas.drawPaint(paintBg);//make background black
// float imageScale = width / w; // how image size scales with screen
}
#Override
public void onDraw(Canvas canvas) {
y = this.getHeight() / 2; // center of screen
x = this.getWidth() / 2;
int left = Math.round(x - 0.8f * w);
int right = Math.round(x + 0.8f * w);
canvas.save();
canvas.rotate((direction + 180) % 360, x, y); // rotate to normal
canvas.drawText(s, x, y + 20, paintName); // draw name
canvas.restore();
canvas.rotate(direction, x, y); // rotate back
super.onDraw(canvas);
}
What changed in 4.1.2 that would affect this, or am I doning something incorrectly? Thanks for your help with this as it's driving me crazy.
Edit to include screen shots:
Android 2.3.4
Android 4.1.2
Note how the white text appears to be on top in 2.3.4 while it appears below or muddy in 4.1.2.
As free3dom pointes out it is related to alpha. I do change alpha because if I don't, the text does not appear on top of the arrow. It appears that the ImageView having the pointer image is always on top - could this be what's going on?
Here is how I handle setting alpha:
public static void setAlpha(View view, float alpha, int duration) {
if (Build.VERSION.SDK_INT < 11) {
final AlphaAnimation animation = new AlphaAnimation(alpha, alpha);
animation.setDuration(duration);
animation.setFillAfter(true);
view.startAnimation(animation);
} else //for 11 and above
view.setAlpha(alpha);
}
Maybe it has something to do with using this.setImageResource(res) to set the image resource? According to android developer guide, I can only set alpha to the single view and everything in the view is changed. Yet if I lower the alpha, the arrow image seems to become transparent enough to allow me to see the text.
You set a stroke width, but never indicate that stroke should be used for the Paint.
Try adding
paintName.setStyle( FILL_AND_STROKE );

Android canvas drawText from right to left

I have an app that handels Arabic too, but my Arabic users have a problem that the drawText flip the word .. Arabic must be from right to left. How do I make the canvas drawText from right to left?
See in the picture the highlighted text is the right text its a textView and it's fine. But the canvas DrawText the one in a circle is wrong. It must be from right to left, how do I make the canvas drawText from right to left?
On the canvas just create two points on sides where you want to draw text, and then create path between them. use this method it will work fine
Path path = new Path();
Paint paint = new Paint();
path.moveTo(p2.x, p2.y);
path.lineTo(p1.x, p1.y);
canvas.drawTextOnPath(String.valueOf(txt), path, (float) (c.getWidth() / (2.3)), (float) (c.getHeight()/2 + paint.getTextSize()/1.5), paint);
you can get subString from your string and draw in your canvas:
Paint textPaint = new Paint();
textPaint.setColor(Color.BLACK);
textPaint.setStyle(Paint.Style.FILL_AND_STROKE);
textPaint.setTextSize(20);
textPaint.setTypeface(Typeface.DEFAULT_BOLD);
textPaint.setStrokeWidth(1);
String subString = mString;
float textWidth = textPaint.measureText(mString);
int endOffset = Math.round(rectWidth * (mString.length() - 1) / textWidth);
if (textWidth > rectWidth) {
endOffset =endOffset - 2;
subString = mString.substring(0, endOffset);
subString = subString + "..";
}else{
for(int j=mString.length();j<endOffset+1;j++){
subString+=" ";
}
}
canvas.drawText(subString, padding , (float) (startHeight + eachHeight / 3 + textPaint.getTextSize() / 1.5), textPaint);
in this way we have a same result even in RTL or LTR string .
Make sure that Android emulator that contains the Arabic language, I had the same problem but when I tried the application on an actual mobile device,It solved.
There are no problems in your application in the language, make sure Android emulator supports the Arabic language
If your target device is api level greater than 11 you can use rotateY=180 attribute in TextView element. Also the parent view should set to rotateY = 180.

Categories

Resources