Adrian
Adrian

Reputation: 346

draw a line in a LinearLayout extended class

I have this class: My problem:

public class Example extends Activity implements OnClickListener{
    DrawView view1;
    Canvas canvas = new Canvas();

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);   
        view1 = new DrawView(this);
        setContentView(view1);
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
    int x = (int) event.getX();
    int y = (int) event.getY();

    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            view1.setFirstCoord(x, y);
            break;
        case MotionEvent.ACTION_MOVE:
            break;
        case MotionEvent.ACTION_UP:
            view1.setSecondCoord(x, y);
            view1.dispatchDraw(canvas);
            break;
        }
    return true;
    }

    public class DrawView extends LinearLayout {
        private Paint paint = new Paint();
        public int x1, x2;
        public int y1, y2;

        public DrawView(Context c){
            super(c);           
            LayoutInflater inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            addView(inflater.inflate(R.layout.union, null));

            x1 = 0;
            x2 = 0;
            y1 = 0;
            y2 = 0;

            paint.setColor(Color.BLACK);
            paint.setAntiAlias(true);
            paint.setDither(true);
            paint.setStyle(Paint.Style.STROKE);
            paint.setStrokeJoin(Paint.Join.ROUND);
            paint.setStrokeWidth(10);        
        }

        @Override
        public void dispatchDraw(Canvas canvas) {
            super.dispatchDraw(canvas);
            canvas.drawLine(0, 50, 900,2000 , paint);//WORKS
            canvas.drawLine((int) x1, (int) y1,(int) x2,(int) y2, paint);//NO WORKS
        }

        public void setFirstCoord(int e, int f){
            x1 = e;
            y1 = f;
        }

        public void setSecondCoord(int e, int f){
            x2 = e;
            y2 = f;
        }
    }
}

When the next line is executed I see the "example" line on the screem:

canvas.drawLine(0, 50, 900,2000 , paint);

But when the following line "is executed" is not painted any straight line. Why???

canvas.drawLine((int) x1, (int) y1,(int) x2,(int) y2, paint);

I also tried with:

canvas.drawLine(x1, y1, x2, y2, paint);

But, obviously, the results are the same.

I also tried with executed a onDraw() method but any line is painted because onDraw paint under de "UI" (under the content of the layout .xml file)

I hope found somebody who can help me. I think the solution can very easy but I'm going crazy trying things without finding the solution.

Thanks!!!

The solution:

public class Example extends Activity implements OnClickListener{
    DrawView view1;
    Canvas canvas = new Canvas();

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);   
        view1 = new DrawView(this);
        setContentView(view1);
        view1.setOnClickListener(this);
    }

    public class DrawView extends LinearLayout {
        private Paint paint = new Paint();
        public int x1, x2;
        public int y1, y2;

        public DrawView(Context c){
            super(c);           
            LayoutInflater inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            addView(inflater.inflate(R.layout.union, null));

            x1 = 0;
            x2 = 0;
            y1 = 0;
            y2 = 0;

            paint.setColor(Color.BLACK);
            paint.setAntiAlias(true);
            paint.setDither(true);
            paint.setStyle(Paint.Style.STROKE);
            paint.setStrokeJoin(Paint.Join.ROUND);
            paint.setStrokeWidth(10);        
        }

        public boolean onTouchEvent(MotionEvent event) {
        int x = (int) event.getX();
        int y = (int) event.getY();

        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            view1.setFirstCoord(x, y);
            break;
        case MotionEvent.ACTION_MOVE:
            break;
        case MotionEvent.ACTION_UP:
            view1.setSecondCoord(x, y);
            view1.onDraw(canvas);
            view1.postInvalidate();
            break;
        }
        return true;
        }

        public void setFirstCoord(int e, int f){
            x1 = e;
            y1 = f;
        }

        public void setSecondCoord(int e, int f){
            x2 = e;
            y2 = f;
        }

        public void onDraw(Canvas canvas){
        canvas.drawLine((int) x1, (int) y1,(int) x2,(int) y2, paint);
    }
    }
}

THANKS AGAIN for your help and your attention. It's a pleasure!!!

Upvotes: 2

Views: 822

Answers (4)

Tim
Tim

Reputation: 35933

I think your problem might be that the x/y coordinates you retrieve in the activity are in the screen space, while your draw coordinates on the canvas are in the canvas space.

So (0,0) in your activity is not the same location as (0,0) of your canvas.

You should probably receive the touchevent inside the view instead of in the activity, then the coordinate system will be the same for both the touchevent and the canvas draw.

Upvotes: 0

yoah
yoah

Reputation: 7230

  • move OnTouchEvent to the DrawView, not the activity
  • Make DrawView extend View, not LinearLayout
  • Instead of dispatchDraw, implement method onDraw and in onTouchEvent, call invalidate()

Upvotes: 1

Ljdawson
Ljdawson

Reputation: 12229

Instead of putting your drawing code in dispatch draw, try putting it in onDraw.

Then modify your onTouch method to look like this:

@Override
public boolean onTouchEvent(MotionEvent event) {
  int x = (int) event.getX();
  int y = (int) event.getY();
  switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
    view1.setFirstCoord(x, y);
    break;
    case MotionEvent.ACTION_MOVE:
    break;
    case MotionEvent.ACTION_UP:
    view1.setSecondCoord(x, y);
    view1.postInvalidate();
    break;
  }
   return true;
}

Upvotes: 1

mrb
mrb

Reputation: 3330

If you want to use the OnClickListener interface for this, you need to set your activity as an OnClickListener for the view:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);   
    view1 = new DrawView(this);
    setContentView(view1);

    // Add this:
    view1.setOnClickListener(this);
}

Otherwise, onTouchEvent will never be called.

Also, onTouchEvent should be named onClick.

Upvotes: 1

Related Questions