Note that there are some explanatory texts on larger screens.

plurals
  1. POdraw a line in a LinearLayout extended class
    primarykey
    data
    text
    <p>I have this class: My problem:</p> <pre><code>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; } } } </code></pre> <p>When the next line is executed I see the "example" line on the screem:</p> <pre><code>canvas.drawLine(0, 50, 900,2000 , paint); </code></pre> <p>But when the following line "is executed" is not painted any straight line. Why???</p> <pre><code>canvas.drawLine((int) x1, (int) y1,(int) x2,(int) y2, paint); </code></pre> <p>I also tried with:</p> <pre><code>canvas.drawLine(x1, y1, x2, y2, paint); </code></pre> <p>But, obviously, the results are the same.</p> <p>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)</p> <p>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.</p> <p>Thanks!!!</p> <p>The solution:</p> <pre><code>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); } } } </code></pre> <p>THANKS AGAIN for your help and your attention. It's a pleasure!!!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload