A single touch is simple with respect to multi-touch.
To create a new project, first look at the layout file:
<relativelayout xmlns:android="Http://schemas.android.com/apk/res/android"xmlns:tools="Http://schemas.android.com/tools"Android:layout_width="Match_parent"android:layout_height="Match_parent"Android:paddingbottom="@dimen/activity_vertical_margin"android:paddingleft="@dimen/activity_horizontal_margin"android:paddingright="@dimen/activity_horizontal_margin"android:paddingtop="@dimen/activity_vertical_margin"tools:context="Com.example.touchevent.MainActivity"> <imageview android:id="@+id/iv"Android:layout_width="Match_parent"android:layout_height="Match_parent"Android:src="@drawable/jiafeimao"Android:scaletype="Matrix"/></relativelayout>
For a simple imageview, we will move this imageview in the activity:
Public class mainactivity extends Activity { PrivateImageView IV;@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main); IV = (ImageView) This. Findviewbyid (R.ID.IV); Iv.setontouchlistener (NewOntouchlistener () {Private floatXPrivate floatY//model for manipulating pictures PrivateMatrix Oldmatrix =NewMatrix ();PrivateMatrix Newmatrix =NewMatrix ();@Override Public Boolean OnTouch(View V, motionevent event) {Switch(Event.getaction ()) {//Determine the type of Operation CaseMotionevent.action_down://Press to remember X, y coordinatesx = Event.getx (); y = event.gety (); Oldmatrix.set (Iv.getimagematrix ()); Break; CaseMotionevent.action_move://When moving //Use another model to remember where you pressedNewmatrix.set (Oldmatrix);//Mobile modelNewmatrix.settranslate (Event.getx ()-X, Event.gety ()-y); Break; }//Put the picture in the moving modelIv.setimagematrix (Newmatrix);return true; } }); }}
It's that simple.
Full code Download
Android development for single point of touch