Draw text in circular view? - android

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.

Related

How to make circle drawable in Android custom canvas?

I want to make in my app possibilty to draw circles by user. The drawing is quite simple - user just press on canvas somewhere and then predefined circle
The difficult part here is to draw it with some drawable (picture) as a fill. It is quite simple when it is about rectangle. Then you just need to write:
Drawable drawable = getResources().getDrawable(R.drawable.my_background_picture);
drawable.setBounds(myRectangle);
drawable.draw(myCanvas);
Everything is done on onDraw() method of my custom view.
Unfortunatelly there isn't such simple method to make it with circle. The one that I've found is slight modification from Vogella's tutorial:
InputStream resource = getResources().openRawResource(R.drawable.sand);
Bitmap bitmap = BitmapFactory.decodeStream(resource);
BitmapShader shader;
shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP,Shader.TileMode.CLAMP);
paint = new Paint();
paint.setAntiAlias(true);
paint.setShader(shader);
myCanvas.drawRoundRect(myRectangle, 120, 120, paint);
At first sight it looks ok, but it's not. This commands make just something like frame on a picture below, so you move hollow circle on a picture and that's all. Not as with rectangle where you actually move rectangular bitmap.
So, my question is - Is there a way to make circle drawable that can be also moved/resized?
Why make a drawable? You can easily draw a circle via the canvas.drawCircle command. You can also easily make one via a Path object.
Edit:
If you need a drawable, try making a ShapeDrawable based off an OvalShape.

How to remove the drawing from a bitmap of a particular region in Android

I have a bitmap object.
I have a Region object that represents a small portion of the bitmap;
I want to remove drawing from the bitmap object of that particular region and make that portion transparent..
How to do it? any help....
I am using android api-level 8..
You can simply make a pixel transparent by using mBitmap.setPixel (100,100,Color.TRANSPARENT);, so basic idea is to iterate over all the pixel to make it transparent, but if you have to iterate over too many pixels, it might be slow.
OR
You can use PorterDuffXferMode to make a portion transparent,
For an example create a paint object as mentioned below and pass it to the canvas:
Paint mPaint = new Paint();
mPaint.setXferMode(new PorterDuffXferMode(PorterDuff.Mode.CLEAR));
You can pass it to the canvas as described below:
Canvas c = new Canvas(mBitmap);
c.drawCircle(cx, cy, radius, paint);
It is for the circle but hope you will get the hint to do it for the custom region as per your need.
If still it is not working then you might have to disable Hardware Acceleration for that particular View. For more information, refer this Google DOC.
Hope this will give you some hint.

Android - Creating a moveable non-rectangular bitmap

My goal right now is create a bitmap that is a non-rectangular shape, that I can also move. I have created a path that I can use as with canvas's clipPath method. Is it possible to move that clipPath around?
Also, am I doing this the best way, or is there a better way to accomplish this?
Here's my draw function:
public void draw(Canvas c){
// Paint object, for outline of clip Path.
Paint p = new Paint();
p.setStyle(Style.STROKE);
p.setColor(Color.RED);
// A currently defined path to clip the bitmap with
Path clipPath = new Path();
clipPath.moveTo(top_left.getX() + nodes.getNodeVals('L').getX(), top_left.getY() + nodes.getNodeVals('T').getY());
clipPath.addPath(outline);
c.save(); // Save the canvas (rotations, transformations, etc)
c.clipPath(clipPath); // Create a clip region
c.drawPath(clipPath, p); // Draw that clip region in red
c.drawBitmap(img, top_left.getX(), top_left.getY(), null); // Draw the bitmap in the clip
c.restore(); // Restore the canvas (rotations, transformations, etc)
}
The clipPath.moveTo line is where I'm having my problem, I believe. Basically, it should be creating a new path that is at the location defined with the x and y values of moveTo (I believe I have those set correctly elsewhere). The path is created beforehand, and stored into outline, and the addPath part should be adding the outline to clipPath.
Thanks in advance!
I'm not entirely sure if I understand exactly what it is you are trying to do, but if you simply want to offset the path from its original position, moveTo is not the way to go since the coordinates of a path you add will be retained.
Instead, you can add the offset coordinates in your addPath:
//clipPath.addPath(outline);
clipPath.addPath(outline, dx, dy);
where dx and dy are your offsets.

Android Bitmap shaped Rhomboid

I have to implement Bitmap whose shape is like a rhomboid(For ref. I have attached sample image for it.) and the source image is rectangular. I do want to make the image to be skewed, cut the image in this shape.
Can anyone give me any idea how it can be implemented?
Create a Path of the shape you need and use the canvas.clipPath() method prior to drawing each of the bitmaps.
I have done this with a circle:
Path mPath = new Path();
mPath.addCircle(centerx, centery, radius, Path.Direction.CCW);
canvas.clipPath(mPath, Region.Op.REPLACE);
canvas.drawBitmap(....);
There are various path methods that you can use to create the desired path (path.addArc, path.lineTo, etc).

Draw text on canvas and make it visible on screen

This code was supposed to convert text to image
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.RED);
paint.setTextSize(16);
paint.setAntiAlias(true);
paint.setTypeface(Typeface.MONOSPACE);
Bitmap bm = Bitmap.createBitmap(16, 16, Bitmap.Config.ALPHA_8);
float x = bm.getWidth();
float y = bm.getHeight();
Canvas c = new Canvas(bm);
c.drawText("Test", x, y, paint);
}
Is this code ok? If yes, how can I make this new bitmap visible on screen? I tried this code which produced an error
setContentView(c); //<- ERROR!
I am confused with the element Canvas as there is not such element in XML which I can use in the code.
setContentView(View) takes a View and Canvas is not a View.
I am not sure that you want to create a Canvas on your own. There are ways to get a Canvas passed to you from the Android Framework though. One way you can do this is by creating a custom View. To do this, you will need to create a new class that extends View.
When overriding a View class, you will have the ability to override the onDraw(Canvas) method. This is probably where you want to do what you are attempting to do in your onCreate() method in the code you posted.
This link gives a good overview of what is required to create your own custom view.
First: If you draw your text at the x and y position you specified, you draw it
at the lower right corner, starting with exactly that pixel. Nothing will be drawn on your canvas. Try bm.getWidth()/2, for height the same for test drawing. You can optimize that later.
Second: Canvas is not a View (does not extend the View class). You can only set Views via set ContentView(). What I recommend here is writing a XML layout containing only a single ImageView and set that via setContentView(R.layout.mylayout).
After that, you can use findViewById() to grab that ImageView and use ImageView.setImageBitmap(bm) to show your bitmap on it.
You dont have to do anything with the canvas, once you created it with your bitmap. Everything you draw inside the canvas from that point on is found in the Bitmap immediately.
Therefore you can't specify the Canvas in XML. It's just an "Editor" to edit pictures, so to speak and not an actual UI element.

Categories

Resources