标签:
TouchEvent.class
package com.bwie.touch; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; public class TouchEvent extends View { public float x=50;//刚进入时小球的位置 public float y=60; public TouchEvent(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); Paint paint=new Paint(); //设置画笔的颜色 paint.setColor(Color.CYAN); //画一个小球 canvas.drawCircle(x, y, 20, paint); } @Override public boolean onTouchEvent(MotionEvent event) { // TODO Auto-generated method stub //获得该小球的x,y this.x=event.getX(); this.y=event.getY(); //通知该组件重绘 this.invalidate(); //返回true表明处理方法已经处理该事件 return true; } }
xml
<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" tools:context=".MainActivity" > <com.bwie.touch.TouchEvent android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout>
标签:
原文地址:http://www.cnblogs.com/ldou/p/5369917.html