I'm currently making my first graphics application to see what I can achieve.
At the moment I can touch the screen and drag my finger to somewhere else to create a line.
When I touch again to draw a new line the previous line is removed.
I'm novice to graphics programming so I bet its something simple but I have no idea!
Help please!
Thanks
Using java Syntax Highlighting
- public class Main extends Activity implements OnTouchListener
- {
- float x1 = 0;
- float y1 = 0;
- float x2 = 0;
- float y2 = 0;
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- LinearLayout layout = (LinearLayout)this.findViewById(R.id.layout_id);
- layout.setOnTouchListener((OnTouchListener)this);
- layout.addView(new Drawer(this));
- }
- @Override
- public boolean onTouch(View v, MotionEvent event)
- {
- switch (event.getAction())
- {
- case MotionEvent.ACTION_DOWN:
- x1 = event.getX();
- y1 = event.getY();
- return true;
- case MotionEvent.ACTION_MOVE:
- x2 = event.getX();
- y2 = event.getY();
- v.invalidate();
- return true;
- case MotionEvent.ACTION_UP:
- x2 = event.getX();
- y2 = event.getY();
- v.invalidate();
- return true;
- }
- return false;
- }
- public class Drawer extends View
- {
- public Drawer(Context context)
- {
- super(context);
- }
- protected void onDraw(Canvas canvas)
- {
- Paint p = new Paint();
- p.setColor(Color.parseColor("#7CFC00"));
- canvas.drawLine(x1, y1, x2 , y2, p);
- invalidate();
- }
- }
- }
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


