1、Java Code
package com.fish.helloworld; import android.app.Activity; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.os.Bundle; import android.util.Log; import android.view.KeyEvent; import android.view.Menu; import android.view.MenuItem; import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(new TestMotionView(this)); } public class TestMotionView extends View{ private Paint mPaint = new Paint(); private int mAction; private float mX; private float mY; public TestMotionView(Context c){ super(c); mAction = MotionEvent.ACTION_POINTER_UP; mX = 0; mY = 0; } @Override protected void onDraw(Canvas canvas){ Paint paint = mPaint; canvas.drawColor(Color.WHITE); if(MotionEvent.ACTION_MOVE == mAction){ paint.setColor(Color.RED); }else if(MotionEvent.ACTION_UP == mAction){ paint.setColor(Color.GREEN); }else if(MotionEvent.ACTION_DOWN == mAction){ paint.setColor(Color.BLUE); } canvas.drawCircle(mX, mY, 10, paint); setTitle("A = " + mAction + " [" + mX + "," +mY +"]"); } @Override public boolean onTouchEvent(MotionEvent event){ mAction = event.getAction(); mX = event.getX(); mY = event.getY(); invalidate(); return true; } } }