Android. When extends TextView property singleLine as true breaks canvas drawing - android

My class extends TextView class.
I try draw path in onDraw method with code
#Override
protected void onDraw(Canvas canvas) {
TextPaint paint = this.getPaint();
paint.setColor(Color.RED);
paint.setStrokeWidth(1);
paint.setStyle(Paint.Style.FILL_AND_STROKE);
RectF rectF = new RectF(0, 0, this.getWidth(), this.getHeight());
Path path = new Path();
path.addArc(rectF, -180, 180);
canvas.drawPath(path, paint);
}
But then i property of TextView singleLine as true draw nothing, but if it's false - all fine works. And draw arc, why?

try
android:maxLines="1"
instead of single line call

Related

Android canvas drawing semi transparent figures, so they overlay each other

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

Android Drawing on Canvas

I have a canvas on which I'm drawing this figure, using array of points. How to fill it with a particular color?
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
RectF oval = new RectF(90, 100, 200, 300);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.RED);
paint.setStrokeWidth(5f);
// canvas.drawRect(oval, paint);
//canvas.drawArc(oval, 30f, 100f, true, paint);
float [] pts = {0,200,
120,140,120,140,
180,20,180,20,
260,100,260,100,
350,20,350,20,
410,140,410,140,
530,200,530,200,
410,260,410,260,
350,380,350,380,
260,300,260,300,
180,380,180,380,
120,260,120,260,
0,200
};
//canvas.drawPoints(pts, paint);
canvas.drawLines(pts, paint);
//RectF tail = new RectF(0f, 50f, 200f,100f);
//canvas.drawArc(tail, 0f, 50f, true, paint);
//canvas.drawRoundRect(oval, 20f, 20f, paint);
invalidate();
}
So basically what I want to do is to just fill the color along these points which are present in array. Any help on this??
You'll need to build a Path and use canvas.drawPath.

android programmatically add rectangle in

Hello i added rectangle in my canvas now i want to add textview or some other view in that rectangle.Suggest me some tutorial also. Thanks in advance
public class DrawView extends View {
public DrawView(Context context) {
super(context);
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Rect rect = new Rect();
rect.set(20 ,10 ,canvas.getWidth()/2, canvas.getHeight()/2);
Paint paint = new Paint();
paint.setColor(Color.GREEN);
canvas.drawRect(rect, paint);
}
}
You can draw text on your canvas.
Paint mpaint= new Paint();
mpaint.setColor(Color.RED);//set red color for rectangle
mpaint.setStyle(Paint.Style.FILL);//mpaint will fill the rectangle
Paint paint2 = new Paint();
paint2.setColor(Color.GREEN);//green color for text
paint2.setTextSize(30f);//set text size. you can change the stroke width also
#Override
protected void onDraw(Canvas canvas)
{
canvas.drawRect(30, 30, 600, 600, mpaint);
canvas.drawText("hello", 150, 150, paint2);//change x and y according to your needs
}
Resulting snapshot on samsung galaxy s3
Adding a View of any kind is out of question here since you are simply drawing on a canvas. But Canvas.drawText(...) might be exactly what you are looking for.

How do I erase (make transparent) one custom region in a Android's canvas?

I'm overriding Android's ImageView in order to make the corners of my image transparent. I can accomplish that clipping the canvas in my onDraw(Canvas canvas):
#Override
protected void onDraw(Canvas canvas) {
Path clipPath = new Path();
int w = this.getWidth();
int h = this.getHeight();
clipPath.addRoundRect(new RectF(0,0,w,h), 10.0f, 10.0f, Path.Direction.CW);
canvas.clipPath(clipPath);
super.onDraw(canvas);
}
Unfortunately, it's not possible to antialias this round rectangle, and the result are ugly corners like this:
I know I can clear parts of my canvas with antialiasing using Paint and PorterDuff.Mode.CLEAR, what I don't know is to specify the round corners as the region to be erased. I'm looking for something like this:
#Override
protected void onDraw(Canvas canvas) {
//superclass will draw the bitmap accordingly
super.onDraw(canvas);
final Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
//this will erase a round rectangle, I need the exact inverse
canvas.drawRoundRect(rect, rx, ry, paint);
}
Is there any way to "erase" not the round rectangle, but it's inverse, ie, the round corners? And what if I just want to erase one of the corners?
Draw using a BitmapShader with a transparent color for your Paint object.
If you just want to erase one of the corners, try drawing it as a Path instead of a RoundRect.
protected void onDraw(Canvas canvas) {
BitmapShader bitmapShader = new BitmapShader(<original drawable>, TileMode.CLAMP, TileMode.CLAMP);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(0xFF000000);
paint.setShader(bitmapShader);
canvas.drawRoundRect(rect, rx, ry, paint);
}

draw an oval shape around the text on canvas

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);
}
}

Categories

Resources