I have read the following links:
http://marakana.com/s/android_2d_graphics_example,1036/index.html
http://developer.android.com/reference/android/graphics/Canvas.html
Activity.setContentView, View.setContentView?
How to create an activity without 'setContentView(R.layout.main)'
http://www.jayway.com/2009/03/26/layout-resources-in-android/
I have read the previous articles that talk about about my problem but I can't solve my problem.
My problem is that I want to draw straight lines to simulate the connection between images, but basically is to paint straight lines.
All the examples I've seen have the main class that inherits from the activity and have another inner class that inherits from the View. And in the main class set the user interface that contains the View class, so we have a empty user interface. Something like this:
public class ActivityMain extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyView view1 =new MyView(this);
setContentView(view1);
}
public class MyView extends View {
public MyView(Context c) {
super(c);
}
}
//And more code
}
I want to set my user interface (xml file) in the class that inherits from the activity (as usual):
public class ActivityMain extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.union);
}
}
And I want from the class that inherits from activity to draw straight lines, I don't want to have any class that inherits from View.
I tried the following but I don't see any line:
public boolean onTouchEvent(MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
Canvas canvas = new Canvas();
Paint paint = new Paint();
paint.setColor(Color.BLACK);
canvas.drawLine(0, 0, 300, 700, paint);
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
break;
}
return true;
}
In the previous method I want to draw a line from the corner 0,0 to the corner 300,700. It's a simple test. But I don't draw anything and I don't know why.