I wants to draw Circle on empty canvas, but not getting how to do.
This is code I'm using to create Empty canvas inside my custom ImageView class.
bmpBase = Bitmap.createBitmap(image_width, image_height, Bitmap.Config.ARGB_8888);
canvas = new Canvas(bmpBase);
Paint paint = new Paint();
paint.setColor(Color.BLUE);
canvas.drawCircle(100, 100, 30, paint);
try this, in your MainActivity first find your imageView and then:
drawingImageView = (ImageView)findViewById(R.id.DrawingImageView);
Paint paint = new Paint();
paint.setColor(Color.BLUE));
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(15);
bmpBase = Bitmap.createBitmap(image_width, image_height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawCircle(100, 100, 30, paint);
drawingImageView.setImageBitmap(bitmap);
Try this:
You need to set the Paint style to stroke if you just want an outline with no fill:
Paint p = new Paint();
p.setStyle(Paint.Style.STROKE);
and if you want filled circle then:
Paint p = new Paint();
p.setStyle(Paint.Style.FILL);
Or you can refer this:
http://android-coding.blogspot.in/2012/04/draw-circle-on-canvas-canvasdrawcirclet.html
Related
I have a bitmap and I am trying to draw a text over the bitmap at the top right corner. But at the first place I am not able to draw any text over it. I am converting a layout to bitmap and then trying to draw text over it. But its not working out. Here is my code:
private Bitmap viewToBitmap(LinearLayout layout) {
Bitmap bitmap = Bitmap.createBitmap(layout.getWidth(), layout.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
layout.draw(canvas);
canvas=new Canvas(bitmap);
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setAntiAlias(true);
paint.setTextSize(14.f);
paint.setTextAlign(Paint.Align.CENTER);
canvas.drawText("Hello Android!", 0, 0, paint);
return bitmap;
}
Please pay much attention to the text position & alignment.
private Bitmap viewToBitmap(View view)
{
Bitmap result = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(result);
view.draw(canvas);
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setAntiAlias(true);
paint.setTextSize(14);
paint.setTextAlign(Paint.Align.RIGHT);
canvas.drawText("Hello Android!", view.getWidth(), 14, paint); // draw watermark at top right corner
return result;
}
I need to draw stroke rectangle with rounded corners.
here is my code:
mLinePaint = new Paint();
mLinePaint.setColor(mDotColorTouched);
mLinePaint.setAntiAlias(true);
mLinePaint.setStrokeWidth(mLineWidth);
mLinePaint.setStyle(Paint.Style.STROKE);
mLinePaint.setStrokeCap(Paint.Cap.ROUND);
bitmap = Bitmap.createBitmap(300,
300, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bitmap);
RectF r = new RectF(0, 0, 300 , 300);
c.drawRoundRect(r, 30, 30, mLinePaint);
but as you can see the line width is bigger in the corners...
any idea why it happens and how to fix this?
here is screen
Try this set of code it's working.
Paint paint = new Paint();
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.BLUE);
paint.setStrokeWidth(10);
Bitmap b = Bitmap.createBitmap(300, 300, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
RectF rectF = new RectF();
rectF.set(5,5,250,250);
c.drawRoundRect(rectF, 10, 10, paint);
imgView.setImageBitmap(b);
I have created custom view to draw a custom speech bubble inside
and I want put some text on custom view. I use drawTextOnPath but it doesn't work right, I want the text to appear line by line.
Custom View - Speech Bubble
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setAntiAlias(true);
paint.setStrokeWidth(2);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setPathEffect(new CornerPathEffect(15) );
paint.setStyle(Paint.Style.FILL_AND_STROKE);
paint.setDither(true);
paint.setShader(new LinearGradient(0, 0, 0, getHeight(), Color.BLACK, Color.WHITE,
Shader.TileMode.MIRROR));
Path path = new Path();
paint.setShadowLayer(4, 2, 2, 0x80000000);
path.moveTo(myPath[0].x, myPath[0].y);
for (int i = 1; i < myPath.length; i++){
path.lineTo(myPath[i].x, myPath[i].y);
}
path.close();
canvas.clipPath(path);
canvas.drawPath(path, paint);
canvas.save();
paint = new Paint();
paint.setColor(Color.WHITE);
canvas.drawTextOnPath(MessageBody, path, 10, 10, paint);
Thanks.
something like this?
Paint paint = new Paint();
paint.setColor(Color.WHITE);
paint.setStyle(Style.FILL);
canvas.drawPaint(paint);
...
//tell the paint of the new color
paint.setColor(android.R.color.black);
paint.setTextSize(20);
canvas.drawText("Some Text", 10, 25, paint);
edit:
then why not something like this
Path path = new Path();
path.addCircle(width/2, height/2, radius, Path.Direction.CW);
Paint paint = new Paint();
paint.setColor(Color.WHITE);
paint.setTextSize(20);
canvas.drawTextOnPath("Some Text", path, 0, 0, paint);
edit nr2:
why not add a rect?
path.addRect(left, top, right, bottom, Direction.CW);
or
path.addRect(rect, Direction.CW);
I created a rectangle in the speech bubble like so
Rect rcText;
rcText = new Rect(rcBounds);//rcBounds is y speech bubble rect
canvas.save();
Limited the draw area
canvas.cliprect(rect);
And draw the text inside the speech bubble like this
canvas.drawtext(mytext,rect.left,rect.top,paint);
canvas.restore();
Thanks mc_fish.
I have tried to modify a Paint variable, but have been unsuccessful - how can I make a bitmap appear "semi-transparent"?
canvas.drawColor(Color.WHITE);
BitmapDrawable bd = (BitmapDrawable) getResources().getDrawable(R.drawable.loading);
Bitmap bm = bd.getBitmap();
Paint paint = new Paint();
paint.setAlpha(60); //you can set your transparent value here
canvas.drawBitmap(bm, 0, 0, paint);
Paint p = new Paint();
p.setAlpha(70);
Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.wallpaper);
canvas.drawBitmap(image, xPosition, yPosition, p);
I want to draw an oval shape around the text on Canvas, I am displaying the 3 texts on Canvas using drawwText() method.
Now when I click on a particular text, I need to draw an oval around that text and again when we click on another text, the oval shape should appear on the clicked text. For this give me some code suggestions.Thanks in advance
use drawOval method().. here is the signature of the method..
public void drawOval (RectF oval, Paint paint)
RectF is class for drawing rectangle...whose constructor is defined as following...
RectF(x,y,x+width,y+height);
you can make its object as follows
RectF rect = new RectF(x,y,x+width,y+height);...
now pass this object in drawOval method....
canvas.drawOval(rect,paint);
for resolution (480 x 800)
in onCreate()
setContentView(new SampleView(this));
create class
private static class SampleView extends View {
// CONSTRUCTOR
public SampleView(Context context) {
super(context);
setFocusable(true);
}
#SuppressLint("DrawAllocation")
#Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.WHITE);
//1
Paint paint = new Paint();
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.GRAY);
RectF oval1 = new RectF(0, 0, 250,250);
Paint p1 = new Paint();
p1.setColor(Color.BLACK);
canvas.drawText("Parent", 30, 50, p1);
canvas.drawOval(oval1, paint);
//2
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.BLUE);
RectF oval2 = new RectF(50, 50, 150, 150);
Paint p2 = new Paint();
p2.setColor(Color.GREEN);
canvas.drawText("Child", 75, 75, p2);
canvas.drawOval(oval2, paint);
}
}