TouchScreenTest, touchscreensly.com
MainActivity:
Package com. example. touchscreentest; import android. OS. bundle; import android. r. layout; import android. app. activity; import android. view. menu; import android. view. motionEvent; import android. view. view; import android. view. view. onTouchListener; import android. widget. linearLayout; public class MainActivity extends Activity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // The LinearLayout linearLayout = (LinearLayout) findViewById (R. id. a); // create a custom View final MyView myView = new MyView (this); // set the maximum width and height of the custom build myView. setMinimumHeight (300); myView. setMinimumWidth (300); myView. setOnTouchListener (new OnTouchListener () {@ Override public boolean onTouch (View v, MotionEvent event) {// TODO Auto-generated method stub // you can specify the position myView. current_x = (int) event. getX (); myView. current_y = (int) event. getY (); // set the position and re-draw the myView. invalidate (); // return true indicates that the event has been processed return true ;}}); linearLayout. addView (myView) ;}@ Override public boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}}
MyView:
Package com. example. touchscreentest; import android. content. context; import android. graphics. canvas; import android. graphics. color; import android. graphics. paint; import android. view. view; public class MyView extends View {protected int current_x; protected int current_y; public MyView (Context context) {super (context ); // TODO Auto-generated constructor stub} // call @ Override protected void onDraw (Canvas canvas Canvas) {// TODO Auto-generated method stub super. onDraw (canvas); Paint p = new Paint (); p. setColor (Color. RED); canvas. drawCircle (current_x, current_y, 10, p );}}
Run: