Draw a circle as an overlay within a frame layout - android

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!

Related

How to draw a circle in front of a textview with canvas in android

I made a code where a circle moves around, but whenever it moves in front of a textview, the textview gets in front of him and I want him to be in front of the textview.
I tried drawing the circle after making the textview but it doesn't fix it.
Example:
public MainView(Context context) {
super(context);
}
public void onDraw(Canvas canvas){
((ViewGroup) text.getParent()).removeView(text);//the text was already added to the activity
Paint paint=new Paint();
paint.setColor(Color.WHITE);
canvas.drawCircle(0.0,0.0,500.0, paint);
main.addContentView(text, parameters);//adding a textview named text in the activity
invalidate();
}
Don't create Views inside onDraw, it's called so many times and you will have a lot of TextViews but you only need one, normally created on activity onCreate.
The TextView seems in front due the children is clipped when drawing, try setting clipChildren to false in the ViewGroup container

How to add textview to dynamic surfaceview android

Actually my problem is I want to add text view to surface view at a particular position whether it may be corner or centre.In this I am getting surface view dynamically and I have to add text view to this surface view dynamically please help me
Another approach would be to write the text directly into the SurfaceView. To do this, you can create your custom implementation of SurfaceView by extending it. Then, rewrite the onDraw() method to draw your text on top of it.
Haven't tested to see if this works, but trye something like:
public class MySurfaceView extends SurfaceView {
Paint paint;
public MySurfaceView() {
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setTextSize(25);
setWillNotDraw(false);
}
public class onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawText("Your Text", 10, 10, paint);
}
}

How to draw line over an ImageView without having to reload it

I'm trying to draw a line over an ImageView but whenever I try it using Canvas, I have to reload the Bitmap, which is not my intention.
Is there a way to simply draw a line on an uploaded ImageView using Canvas without having to refresh the Image? Or another way to draw lines over Android ImageView?
Or, if you want to be able to draw any lines (rects, ovals, etc), subclass ImageView into your own ImageView and do the drawing yourself.
public class MyImageView extends ImageView {
Paint linePaint = new Paint();
#Override
protected void onDraw(Canvas canvas) {
super.onDraw();
// And draw your line.
// (Be sure to have set the values/fields in linePaint earlier so that you draw the correct line/type/size/etc).
canvas.drawLine(0, getHeight()/2, getWidth(), getHeight()/2, linePaint);
}
}
And in your layout xml, don't specify <ImageView .../>, but specify <com.mycompany.project.widget.MyImageView ... /> instead.
The way that I draw lines in Android is by creating a View with height or width of 1dp. Then set the other value to whatever you want and set the color.

How do I draw a line in a LinearLayout containing other views?

I've created a bar that is supposed to contain multiple views of different colors. A line has to be drawn that indicates the current position in the bar. Below is my simplified code:
public class Abar extends LinearLayout {
final Paint line_paint = new Paint();
private Context context;
public Abar(Context context) {
super(context);
this.context = context;
line_paint.setColor(Color.WHITE);
setWeightSum(2000);
setBackgroundResource(R.color.blue);
View view = new View(context);
view.setBackgroundResource(R.color.yellow);
this.addView(view, new LinearLayout.LayoutParams(0, LayoutParams.MATCH_PARENT, 1000));
}
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawLine(20, 80, 100, 80, line_paint);
}
}
Now, this does not seem to work. No matter if I swap the canvas.drawLine with super.onDraw, the line is not visible unless I remove the view.setBackgroundResource. How can I draw a line over the LinearLayout. I'd rather not use FrameLayout if possible.
Below are pictures of what I'm trying to achieve (adding a white line) on top of the bars (note: the white bar on the first picture is just exaggerated big for clearness on SO):
Override dispatchDraw() instead of onDraw(). You want to draw the line after the child views draw (which isn't in super.onDraw).

Drawing line under view in android

I am using the onDispatchDraw(Canvas canvas) method to draw lines in my view. When I call canvas.drawLine() it always draws the line on top of all my views. Is there any way to draw the line under a button but on top of another view in my layout using canvas.drawLine()?
I have tried the following but it still draws the line over the button.
Button b;
RelativeLayout r;
#Override
protected void dispatchDraw(Canvas canvas) {
super.dispatchDraw(canvas);
Paint p = new Paint();
p.setColor(Color.Black);
canvas.drawLine(0,0,100,100,p);
r.removeView(b);
r.addView(b);
}
You're trying to reinvent the wheel. Z-ordering is already implemented in the window management subsystem and you can use it.
Create a custom view you want to draw on.
Make it non-clickable using android:clickable="false" or setClickable(false).
Make its background transparent and implement dispatchDraw().
Put all the views you don't want to draw on above this view in the view hierarchy.
Call super.dispatchDraw() after drawing the line. The dispatchDraw is called by viewgroup for drawing its children, so in your case, calling super.dispatchDraw() will draw the button first then you are drawing the line over it. Do dispatchDraw this way :
Updated code
class myListViewWithLine extends ListView {
....
#Override
protected void dispatchDraw(Canvas canvas) {
Paint p = new Paint();
p.setColor(Color.Black);
canvas.drawLine(0,0,100,100,p);
super.dispatchDraw(canvas);
}
....
}
Another method will be to just add a view with 1 dip as height and background color whatever you like under the button. It should look like like a line.
View v = new View(this);
ViewGroup.LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT,1);
v.setLayoutParams(lp);
v.setBackgroundColor(Color.WHITE);
addView(v,INDEX NUMBER AFTER THE BUTTON);

Categories

Resources