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.
Related
I have a bitmap of cup image and i want to add dashed border around cup.
Here is what i did try, but it doesn't work :
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setARGB(255, 0, 0, 0);
paint.setStyle(Paint.Style.STROKE);
paint.setPathEffect(new DashPathEffect(new float[]{5, 10, 15, 20}, 0));
canvas.drawBitmap(cupBitmap, x, y, paint);
How can this be accomplished?
Here is what I've tried so far:
paint = new Paint();
path=new Path();
path.moveTo(50, 50);
paint.setColor(Color.argb(255, 25, 25, 255));
paint.setTextSize(70);
paint.setFakeBoldText(true);
paint.setMaskFilter(new BlurMaskFilter(10, BlurMaskFilter.Blur.SOLID));
Typeface typeface = Typeface.create(Typeface.SANS_SERIF,Typeface.BOLD);
paint.setTypeface(typeface);
paint.setStyle(Style.FILL_AND_STROKE);
paint.setAntiAlias(true);
paint.setDither(true);
paint.setTextAlign(Align.CENTER);
On a canvas I draw figures covering each-other to get the result I want. Their transparent is 128 and on cover areas color multiple. I need whole figure in one color. How to fix it?
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setColor(Color.GREEN);
Path path = new Path();
path.moveTo(margin,0);
path.lineTo(middleWidth+pieceWidth,0);
path.lineTo(middleWidth-pieceWidth,height);
path.lineTo(margin, height);
path.lineTo(margin, 0);
Path path1 = new Path();
path1.moveTo(middleWidth+pieceWidth+interval,0);
path1.lineTo(middleWidth-pieceWidth+interval,height);
path1.lineTo(width-margin,height);
path1.lineTo(width-margin,0);
path1.lineTo(middleWidth+pieceWidth+interval,0);
RectF rect = new RectF();
rect.set(0,0,middleWidth-pieceWidth,height);
RectF rect1 = new RectF();
rect1.set(middleWidth+pieceWidth,0,width,height);
paint.setStyle(Paint.Style.FILL);
paint.setColor(leftColor);
paint.setAlpha(128);
canvas.drawPath(path, paint);
paint.setColor(rightColor);
paint.setAlpha(128);
canvas.drawPath(path1, paint);
paint.setAlpha(128);
paint.setStyle(Paint.Style.FILL);
canvas.drawRoundRect(rect, corners, corners, paint);
canvas.drawRoundRect(rect1, corners, corners, paint);
}
The result is:
Result picture
I have several lines in my application. If somebody touches a line I have to highlight the line touched. I think if I could draw a transparent rectangle with light color, other than the clicked line color, then it will be highlighted properly.. So can anybody tell me how can I draw a transparent rectangle on Android canvas?
My line color is black. Please see the pic.
This will draw green 50% transparent rectangle on canvas:
Paint myPaint = new Paint();
myPaint.setStyle(Paint.Style.FILL);
myPaint.setColor(Color.rgba(0, 256, 0, 128)); // green color with 50% transparency
// c is your canvas
c.drawRect(100, 100, 200, 200, myPaint);
try this way
private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setColor(Color.RED);
mPaint.setStrokeWidth(3);
mPaint.setPathEffect(null);
canvas.drawRect(x, y, x + width, y + height, mPaint);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setColor(Color.WHITE);
mPaint.setStrokeWidth(3);
mPaint.setPathEffect(new DashPathEffect(new float[] { 5, 5 }, 5));
canvas.drawRect(x, y, x + width, y + height, mPaint);
you can use this:
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.MAGENTA);
DashPathEffect dashPath =new DashPathEffect(new float[ ]{70,2}, 30);
paint.setPathEffect(dashPath);
paint.setStrokeWidth(80);
canvas.drawRect(30, 300 , 320, 300, paint);
i am able to draw arc using Paint and passing some gradient color to it, my problem is that i need draw the arc using a gradient image.Is it possible to draw arc using an image?
If so how to do it?
this is my current code:
Paint nPaint = new Paint();
nPaint.setAntiAlias(true);
nPaint.setDither(true);
nPaint.setStrokeJoin(Paint.Join.ROUND);
nPaint.setStrokeCap(Paint.Cap.ROUND);
int gradientStart = getResources().getInteger(R.integer.gradient_start);
int gradientend = getResources().getInteger(R.integer.gradient_end);
nPaint.setShader(new RadialGradient(getWidth() / 2, getHeight() / 2, getWidth() / 2,
gradientStart, gradientend, Shader.TileMode.CLAMP));
Try this code i hope this will help you
#Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.CYAN);
Paint p = new Paint();
// smooths
p.setAntiAlias(true);
p.setColor(Color.RED);
p.setStyle(Paint.Style.STROKE);
p.setStrokeWidth(5);
// opacity
//p.setAlpha(0x80); //
RectF rectF = new RectF(50, 20, 100, 80);
canvas.drawOval(rectF, p);
p.setColor(Color.BLACK);
canvas.drawArc (rectF, 90, 45, true, p);
}