package cn.c; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.os.Bundle; import android.view.Display; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.widget.ImageView; public class MainActivity extends Activity{ private ImageView mImageView; private Bitmap mBitmap; private Paint mPaint; private Canvas mCanvas; float downX=0; float downY=0; float upX=0; float upY=0; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mImageView=(ImageView) findViewById(R.id.imageView); Display display=getWindowManager().getDefaultDisplay(); int w=display.getWidth(); int h=display.getHeight(); mBitmap=Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); mCanvas=new Canvas(mBitmap); mPaint=new Paint(); mPaint.setColor(Color.RED); mImageView.setImageBitmap(mBitmap); } @Override public boolean onTouchEvent(MotionEvent event) { int action = event.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: downX=event.getX(); downY=event.getY(); break; case MotionEvent.ACTION_MOVE: upX=event.getX(); upY=event.getY(); mCanvas.drawLine(downX, downY, upX, upY, mPaint); mImageView.invalidate(); downX=upX; downY=upY; break; case MotionEvent.ACTION_UP: upX=event.getX(); upY=event.getY(); mCanvas.drawLine(downX, downY, upX, upY, mPaint); mImageView.invalidate(); break; case MotionEvent.ACTION_CANCEL: break; default: break; } return true; } }