I am trying to create an object that can be called whenever I want to draw something on the onDraw() function of my class. Here is my code which is not working:
//object class
public class DrawObject extends Canvas {
Paint paint = new Paint();
public void setColor(int color){
paint.setColor(color);
}
// I want to draw an arrow to instead of a line
public void drawArrow(float startPointX, float startPointY, float endPointX, float endPointY){
drawLine(startPointX, startPointY, endPointX, endPointY, paint);
// draw the rest of the arrow here
}
}
for the main class:
public class Screen extends ImageView{
Paint paint = new Paint();
public Screen(Context context){
super(context);
paint.setColor(Color.BLACK);
paint.setStyle(Style.STROKE);
}
public void onDraw(DrawObject drawObject){
//called DrawObject instead on Canvas
drawObject.drawArrow(10,10,100,100);
// I want to draw the arrow here but it is not working.
}
can someone please tell me what is the proper way to do it? Thanks.
Extending
Canvasis the wrong approach since you get a canvas supplied by the system which you have to use. You can’t force Android to use your subclass ofCanvas. Instead you can simply pass theCanvasto your class whenever it needs it.And use like