OnTouchListener触摸事件主要用到绘图操作、游戏等方面。
OnTouchListener是指用户触摸到屏幕后产生的一些形式,而当用户从屏幕上划过时候,可以通过OnTouchListener事件得到用户当前的坐标,OnTouchListener接口定义如下
public Interface View.OnTouchListener(){ public abstract boolean onTouch(View v,MotionEvent event); }
事例1.
在main.xml中进行布局(记住这里的TextView宽高是占满)
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <!-- 显示当前用户触摸的坐标, --> <TextView android:id="@+id/info" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="fdsaf" /> </LinearLayout>
2.在Activity中写代码完成触摸得到用户的坐标
package com.qn.touch; import android.app.Activity; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.widget.TextView; public class OnTouchActivity extends Activity { /** Called when the activity is first created. */ private TextView info=null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); info=(TextView)super.findViewById(R.id.info); info.setOnTouchListener(new OnTouchListenerIml()); } private class OnTouchListenerIml implements OnTouchListener{ public boolean onTouch(View v, MotionEvent event) { System.out.println("zenm "); info.setText(" X= " +event.getX()+ " Y= "+event.getY());//设置文本 return false;//执行完后还可以执行其他操作 } } }
结果
事例二
绘图
如果要绘图必须从新定义一个属于自己的新组件继承android.View.View
如果要定义组件的话一定要考虑好构造方法这个问题,
另外靠基本格式是不行的人,如果要绘图的话,就要保持多个坐标下来。所以 坐标可以通过Point类进行包装(Point类专门用来保持我们的x坐标、y坐标)
1.在main.xml中不需要写入其他的控件而是我们自己通过继承android.view.View类,所定义的
如下是自定义实现view的类和main.xml内的布局
package com.qn.mypain; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Point; import android.util.AttributeSet; import android.util.MonthDisplayHelper; import android.view.MotionEvent; import android.view.View; public class MainActivity extends View { private List<Point> allpoint=new ArrayList<Point>();//保存所有的操作坐标,只要有一个坐标触发就通过allpoint集合保存 public MainActivity(Context context, AttributeSet attrs) {//构造方法作用接收Context,同时接收属性集合 super(context, attrs);//调用父类的构造方法 super.setOnTouchListener(new OnTouchListenerImpl());//相当于定义了一个触摸事件 } private class OnTouchListenerImpl implements OnTouchListener{ // 现在触摸事件只是保存了所有的坐标点,如果想要这些内容显示,则需要复写View类的一个方法 // OnDraw()表示重绘(绘图); public boolean onTouch(View v, MotionEvent event) { Point point=new Point((int)event.getX(),(int)event.getY());//将我们的坐标点保存到Point类中 if(event.getAction()==MotionEvent.ACTION_DOWN){//用户按下 表示从新保存点 MainActivity.this.allpoint=new ArrayList<Point>();//从新绘制一张图 MainActivity.this.allpoint.add(point); }else if(event.getAction()==MotionEvent.ACTION_UP){//用户松开 MainActivity.this.allpoint.add(point);//记录坐标点 MainActivity.this.postInvalidate();//移动一点画一点移一点画一点(重新绘图形) }else if(event.getAction()==MotionEvent.ACTION_MOVE){//用户移动 MainActivity.this.allpoint.add(point);//记录坐标点 MainActivity.this.postInvalidate();//移动一点画一点移一点画一点(重新绘图形) } return true;//要改为true表示下面不在执行了 } } // 由于这个类没有太多复杂的操作,所以直接将这个类配置到布局管理器中即可(这是一个新的组件类) @Override protected void onDraw(Canvas canvas) {//进行绘图 Paint paint=new Paint();//依靠此类开始画线 paint.setColor(Color.RED);//定义图的颜色 if(MainActivity.this.allpoint.size()>1){//现有坐标点保存的时候进行开始绘图 Iterator<Point> iter=MainActivity.this.allpoint.iterator();//固定操作 通过点绘图 Point first=null; Point last=null; while(iter.hasNext()){ if(first==null){ first=(Point)iter.next();//取出坐标 }else{ if(last!=null){//前一阶段完成 first=last;//重新开始下一阶段 } last=(Point)iter.next();//结束点坐标 canvas.drawLine(first.x, first.y, last.x, last.y, paint); } } } } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <!-- 新的组件类(主要用于画图)而后Activity不需要其他二外的操作 就是调用布局管理器--> <com.qn.mypain.MainActivity android:id="@+id/paintView" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout>
2.在Activity中只需要调用main.xml布局就行
package com.qn.mypain; import android.app.Activity; import android.os.Bundle; public class MyTouch extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.main); } }