write text vertically on canvas - android

I want to display text vertically on Canvas.
Please help me.

You want to create a Path object that defines the verticle line you want your text to follow. Then use a canvas.drawTextOnPath() method.

Have you tried this:
Canvas canvas;
canvas.rotate(90);
canvas.drawText("The text", 0, 0, new Paint());

Related

How to create a date drawable like google calendar app

I don't know how to create a drawable and add it to toolbar like the image above (red circle). The number is corresponding to today's date.
If you have any ideas, please help me.
UPDATE:
Thanks for your ideas. This what I come up with:
Create my custom Drawable (eg: TextDrawable extends Drawable)
Override the draw(Canvas canvas) method
Programmatically add my custom Drawable to my Toolbar:
inside: onCreateOptionsMenu(Menu menu)
menu.add(0, 0, 1, getString(R.string.jump_to_today)).setIcon(new TextDrawable("22")).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
For now, it's display a text date like I want (haven't has the calendar background). I think I will have to write it on my draw method.
Thanks for your help!
Trying writing text on an image...
Firstly, you need a calendar image without text ("22" in this case). Then, you write text "22" on the image. Something like this:
Bitmap calendarBitmap = ... // Load your calendar image here
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setTextSize(10);
canvas.drawText("22", x, y, paint);
Which x, y are the coordinates of the text's position on the image. You also need to change Color.BLACK to the image's color for consistency.
Finally, you get calendarBitmap as a result.
I think the best way is to get the image you want as the calendar (maybe you can find it inside android resources). Then transform it into a nine patch and use it as the background drawable of your TextView.
You can read google documentation about creating nine patch. You will be able to define in wich region the text should be by defining the content area.

Android: Draw circle of alphabets

I am working in android, I want to draw a circle of Alphabets, in this every alphabets A to Z must show. and this each alphabet must be clickable or touchable.
My view should look like following one:-
I do not have any idea related to this, how should I start ? please give me some suggestion so I can go ahead.what things should I use to do this.
you may provide me some suitable links.
Thank you in advance.
You will probably want to use the Android Canvas API.
Here's an example of drawing text on the Canvas: Android Canvas.drawText .
Bitmap b = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(b);
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setTextSize(20);
canvas.drawText("Some Text", 10, 25, paint);
Just write a loop to iterate where to draw the text and iterate through a-z, like so:
for(char ch='a'; ch<='z'; ch++)
{
System.out.print(ch);
}

Draw text in circular view?

I want to draw a string say "stackoverflow" in circular view like below image can any one suggest how to do it. And also i need click event on each characer.
You need to make a customized view for this. in onDraw method, create a path object, add circle to that object, and then use Canvas Object to draw text on that path.
Path path = new Path();
path.addCircle(x, y, radius, Path.Direction.CW);
myCanvas.drawTextOnPath(myText, path, offset, 0, myPaint);
Edit:
use this line of code when using os 4.0 and above:
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
Take a look at both Path.addCircle and Canvas.drawTextOnPath API.

Android Canvas.drawLine not smooth, not consistent

I am trying to use Canvas.drawLine method to draw a polygon
Here's the code that I am using
Canvas canvas = new Canvas(cache);
Paint paint = new Paint();
paint.setStrokeWidth(16);
paint.setColor(this.currentDrawing.getColor());
paint.setFlags(Paint.ANTI_ALIAS_FLAG);
systemCanvas.drawBitmap(cache, 0, 0, paint);
paint.setStrokeCap(Cap.ROOUND);
canvas.drawLine(from.getLeft(), from.getTop(), to.getLeft(), to.getTop(), paint);
And this is the output that I am getting:
Notice the way the lines render, they break on the round shapes and don't join smoothly. I understand why is it happening but I don't know how to make it smooth and consistent.
Any help is appreciated.
You may also want to do this
mPaint.setAntiAlias(true);
Please check this answer out because it is similar to your question:
Android How to draw a smooth line following your finger
Please let me know if this helps!
You need to activate hardware acceleration. If you don't activate this, you can not use method of antialias, cap, join and etc.

how to display images dynamically on dynamic positions

i have rectangles and images and i want to show that images on that positions that rectangle holds in it...it is all dynamic. and i am using bitmap class to display but now i have to set positions according to that rectangle...
please help.
thanks a lot.
After much googling I finally got my result, so I thought I'd share it with others. Override onDraw() method and put this code in it:
Paint paint = new Paint();
this.setbMap(BitmapFactory.decodeByteArray(imageTile, 0, imageTile.length));
Bitmap adapt = Bitmap.createBitmap(bMap);
canvas.drawBitmap(adapt, rect.left, rect.top, paint);

Categories

Resources