Why is the rectangle not shown when I used drawRect() on canvas object,
and also declared it inside onCreate method.
Code
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activitymain);
Chronometer stopWatch = (Chronometer)findViewById(R.id.chrono);
mDrawingActivity = (DrawingActivity)findViewById(R.id.the_canvas);
Button b = (Button)findViewById(R.id.startButton);
b.setText("start");
b.setOnClickListener(this);
}
OnDraw() Method
protected void onDraw(Canvas Square)
{
super.onDraw(Square);
Paint squareColor = new Paint();
squareColor.setColor(Color.CYAN); // change the box color to cyan
Square.drawRect(100,100,100,100, squareColor);
return;
}
Clarification: Even the button and chronometer are not shown too and the program is forced closed.
You are drawing an point rectangle.
Change line
Square.drawRect(100,100,100,100, squareColor);
to
Square.drawRect(100, 100, 200, 200, squareColor)
Here is the definition from doc.
drawRect(float left, float top, float right, float bottom, Paint paint)
Draw the specified Rect using the specified paint. The rectangle will be filled or framed based on the Style in the paint.
Parameters
left The left side of the rectangle to be drawn
top The top side of the rectangle to be drawn
right The right side of the rectangle to be drawn
bottom The bottom side of the rectangle to be drawn
paint The paint used to draw the rect
Related
I am writing a custom renderer for Xamarin.Forms. I am overriding the class ImageRenderer to do some tweaks to the image.
However, I want to overlay some parts of the image with circles, so I override the method void OnDraw(Canvas canvas).
Then I modify the Canvas a bit by drawing some circles:
protected override void OnDraw(Canvas canvas)
{
var paint = new Paint
{
Color = Color.Red
};
paint.SetStyle(Paint.Style.Fill);
foreach (var mapObject in _control.PointSource)
{
canvas.DrawCircle(mapObject.Location.X, mapObject.Location.Y, 100 / _scaleFactor, paint);
}
base.OnDraw(canvas);
}
However, everything is drawn on the background. The actual image is always on top. How can one draw circles on top of the image?
https://developer.xamarin.com/api/member/Android.Views.View.OnDraw/
In the docs they say the following:
the canvas on which the background will be drawn
So the current behaviour was expected.
I managed to fix it by moving the circle logic to the function: void Draw(Canvas canvas).
I have a frame layout (full screen) that acts as a container for another frame layout which shows the camera preview. Now I want to show on top of the camera preview a circle of a given radius. The radius shall change as a function of some properties of the current preview image.
I know that I can add some (semi-transparent) layouts "on top" of the frame layout I use for the camera preview, but I am not sure how to best show this "overlay" circle. The circle shall be centered, have a solid line but not be filled, i.e. be transparent and also its background shall be transparent.
You can make your own View class inside your existing Activity class. Here is my class.
class CanvasView extends View {
private Paint paint;
public CanvasView(Context context){
super(context);
//Customize your own properties
paint = new Paint();
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(10f);
}
#Override
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
paint.setColor(Color.RED);
canvas.drawCircle(enter coords and radius here);
}
}
You can call the onDraw method from within that class by calling the invalidate() method..
Here is how you can add it to your layout..
Assuming you declared a CanvasView class, called drawable, and have your Framelayout, called main_layout, you add it to your existing framelayout by doing...
CanvasView drawable = new CanvasView(getApplicationContext());
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
main_layout.addView(drawable, params);
Hope this works!
I'm drawing a tooltip after clicking inside a custom bar chart (created with MPAndroidChart). The view hierarchy is as follows
<LinearLayout>
<TextView text=Move & Max Pain/>
<RelativeLayout with 2 textviews>
<chart
clipToChildren=false
clipToPadding=false
/>
</LinearLayout>
While the View is inside the Chart or its inmediate sibling, everthing looks good. But the moment it collides with its sibling, the tooltip is truncated
Using HierarchyViewer I can see that the content is present, but it's not drawn.
In order to get the clipping, I'm using this code inside draw
#Override
public void draw(Canvas canvas, float posx, float posy) {
// take offsets into consideration
posx += getXOffset();
posy += getYOffset();
canvas.save();
// translate to the correct position and draw
canvas.translate(posx, posy);
Rect clipBounds = canvas.getClipBounds();
clipBounds.inset(0, -getHeight());
canvas.clipRect(clipBounds, Region.Op.INTERSECT);
draw(canvas);
canvas.translate(-posx, -posy);
canvas.restore();
}
If I change Op to Region.Op.Replace, the tooltip is draw correctly but it replaces the Toolbar content, instead of scrolling under it.
You'll need the bounds of the area in which you want to be able to draw the tooltip, and I'm assuming that would be a scrollview. Then you can intersect the tooltip bounds with the scroll to work out what the clipping should be; and if it should be drawn at all.
To explain it in code it would be something like this (untested):
Rect scrollViewRect; // the bounds of your scrollview
Rect tooltipRect; // the bounds of your tooltip
bool intersects = tooltipRect.intersect(scrollViewRect)
if(intersects)
{
canvas.clipRect(tooltipRect, Region.Op.REPLACE);
draw(canvas);
}
I am using this ArcMenu https://github.com/daCapricorn/ArcMenu
Please check the code and the image of the ArcMenu on the github page
This custom view is arranged this way ->
ArcMenu[View] has Children -> ArcLayout[ViewGroup] has children -> ImageViews (to hold the icons for menu)
I am trying to draw colored arc behind the Arclayout. I am doing this in the onDraw
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
if(isExpanded()){
Log.d("ArcLayout","onDraw(Canvas)");
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(colorOne);
paint.setAlpha(125);
canvas.drawArc(rect, startAngle, sweepAngle, true, paint);
//Dimensions of rect are set in OnMeasure
}
super.onDraw(canvas);
}
Now the problem is that the OnDraw gets called for all the children and the logic for drawing is same for the parentView and children view. I want the Colored Arc to be drawn behind the parent view and not the children(Imageviews).
Rect dimensions are top=0, left=0, right=getMeasuredWidth(), bottom=getMeasuredHeight().
When the call to canvas.onDraw is made, top Left of the rect becomes relative to child i.e 0,0 of the child and the not the parent. The result is a colored arc behind the child and not the parent.
How should I ensure that the colored arc gets drawn behind the Parent view only?
I am writing a simple view that will show a dot, after some action, show the next dot. The dots are on a vertical strip. so I create a function that'll clear the strip. then draw a circle at the position where I want the dot. the code segment is as followed.
private void clear_strip(){
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.FILL_AND_STROKE);
m_canvas.drawRect( 0, 0,width/8, height, paint);
paint.setColor(Color.GREEN);
}
private void set_dot(){
clear_strip();
m_canvas.drawCircle(width/10, (int) (font_height*(scoreboard.current_batter_position()+0.5))/1, font_height/4, paint);
}
#Override
protected void onDraw(Canvas canvas) {
set_dot();
canvas.drawBitmap(m_bitmap, 0, 0, paint);
}
but one of the dot is just not updating. It'll keep the old dot, skip that dot, then move to the next dot. I tried to print out the position to logcat right before the drawCircle call, and the position is right, it's just not drawing (and not clearing as well)....please advise.
You get a canvas to draw into passed into your onDraw method. But the point drawing code uses the canvas m_canvas. Pass the canvas as a parameter to your dot drawing code to fix that.