本文章主要实现 自定义view 进行自定义监听接口 实现点击,移动,抬起 是 触摸的坐标
1:首先自定义view
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
public class Myview extends View {
private OnDownActionListener down=null;
private OnMoveActionListener move=null;
private OnUpActionListener up=null;
public Myview(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public Myview(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public Myview(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int x,y;
if (event.getAction()==MotionEvent.ACTION_DOWN) {
x=(int) event.getX();
y=(int) event.getY();
if (down!=null) {
down.OnDown(x, y);
}
return true;
}else if (event.getAction()==MotionEvent.ACTION_MOVE) {
x=(int) event.getX();
y=(int) event.getY();
if (move!=null) {
move.OnMove(x, y);
}
}else if (event.getAction()==MotionEvent.ACTION_UP) {
x=(int) event.getX();
y=(int) event.getY();
if (up!=null) {
up.OnUp(x, y);
}
}
return super.onTouchEvent(event);
}
public OnDownActionListener getDown() {
return down;
}
public void setDown(OnDownActionListener down) {
this.down = down;
}
public OnMoveActionListener getMove() {
return move;
}
public void setMove(OnMoveActionListener move) {
this.move = move;
}
public OnUpActionListener getUp() {
return up;
}
public void setUp(OnUpActionListener up) {
this.up = up;
}
public interface OnDownActionListener{
public void OnDown(int x,int y);
}
public interface OnUpActionListener{
public void OnUp(int x,int y);
}
public interface OnMoveActionListener{
public void OnMove(int x,int y);
}
}
2:定义XML
<LinearLayout 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:orientation="vertical" >
<com.example.demo7interf.Myview
android:id="@+id/my_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ic_action_search" >
</com.example.demo7interf.Myview>
</LinearLayout>
3:在activity中使用view以及监听
package com.example.demo7interf;
import com.example.demo7interf.Myview.OnDownActionListener;
import com.example.demo7interf.Myview.OnMoveActionListener;
import com.example.demo7interf.Myview.OnUpActionListener;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Toast;
public class MainActivity extends Activity {
private Myview myview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myview=(Myview) findViewById(R.id.my_view);
myview.setDown(new OnDownActionListener() {
@Override
public void OnDown(int x, int y) {
Toast.makeText(MainActivity.this, "你点击的是down事件:坐标为:x"+x+"y:"+y, 1).show();
}
});
myview.setMove(new OnMoveActionListener() {
@Override
public void OnMove(int x, int y) {
Toast.makeText(MainActivity.this, "你点击的是MOVE事件:坐标为:x"+x+"y:"+y, 1).show();
}
});
myview.setUp(new OnUpActionListener() {
@Override
public void OnUp(int x, int y) {
Toast.makeText(MainActivity.this, "你点击的是up事件:坐标为:x"+x+"y:"+y, 1).show();
}
});
}
}
ok这样就实现了