Package cn. c; import java. io. outputStream; import android. app. activity; import android. content. intent; import android. graphics. bitmap; import android. graphics. bitmap. compressFormat; import android. graphics. bitmapFactory; import android. graphics. canvas; import android. graphics. color; import android. graphics. matrix; import android. graphics. paint; import android.net. uri; import android. OS. bundle; import android. vi Ew. display; import android. view. motionEvent; import android. view. view; import android. view. view. onClickListener; import android. view. view. onTouchListener; import android. widget. button; import android. widget. imageView; import android. widget. toast;/*** Note: * 1 event. getX () and event. difference between getRawX () Methods * event. getX () is relative to the parent control * event. getRawX () is relative to the upper left corner of the screen. * 2 Processing touch events in the Activity uses OnTouchEvent () * processing touch events in the View implements The uchListener {} interface * overwrites the onTouch () method * 3 Bug: * computing options. there is a Bug in inSampleSize. some images are too large to be rotated in the * Device Image Library (although it does not seem to exist ). for example, if a * large image looks portrait in the image library, it is displayed as a horizontal screen in ImageView. * This is convenient for testing: * options. inSampleSize = 8; * write dead * 4 call bitmap. when using the compress () method, note that if the format is PNG, it is invalid. * Because PNG will always keep all data. JPEG is a "lossy" decoder */public class MainActivity extends Activity implements OnTouchListener {private ImageView mImageView; private Bu Tton mButton; private Button mSaveButton; private Bitmap mRawBitmap; private Bitmap mAlteredBitmap; private Paint mPaint; private Canvas mCanvas; private Uri mPhotoFileUri; private final int PICK = 88; flodownat x = 0; float downY = 0; float upX = 0; float upY = 0; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); mImageView = (Image View) findViewById (R. id. imageView); mImageView. setOnTouchListener (this); mButton = (Button) findViewById (R. id. button); mButton. setOnClickListener (new ButtonOnClickListener (); mSaveButton = (Button) findViewById (R. id. saveButton); mSaveButton. setOnClickListener (new ButtonOnClickListener (); mPaint = new Paint (); mPaint. setColor (Color. RED); mPaint. setStrokeWidth (5) ;}@ Overrideprotected void onActivityRes Ult (int requestCode, int resultCode, Intent data) {super. onActivityResult (requestCode, resultCode, data); if (requestCode = PICK) {mPhotoFileUri = data. getData (); Display display = getWindowManager (). getdefadisplay display (); float w = display. getWidth (); float h = display. getHeight (); try {BitmapFactory. options options = new BitmapFactory. options (); options. inJustDecodeBounds = true; mRawBitmap = BitmapFactory. decodeStream (GetContentResolver (). openInputStream (mPhotoFileUri), null, options); int widthRation = (int) Math. ceil (options. outHeight/h); int heightRation = (int) Math. ceil (options. outWidth/w); if (heightRation> 1 & widthRation> 1) {if (heightRation> widthRation) {options. inSampleSize = heightRation;} else {options. inSampleSize = widthRation;} // Where the Bug is located: options. inSampleSize = 8; options. inJustDecodeBounds = false; mRawBitmap = Bitma PFactory. decodeStream (getContentResolver (). openInputStream (mPhotoFileUri), null, options); mAlteredBitmap = Bitmap. createBitmap (mRawBitmap. getWidth (), mRawBitmap. getHeight (), mRawBitmap. getConfig (); mCanvas = new Canvas (mAlteredBitmap); Matrix matrix = new Matrix (); mCanvas. drawBitmap (mRawBitmap, matrix, mPaint); mImageView. setImageBitmap (mAlteredBitmap);} catch (Exception e) {}} private class ButtonOnClic KListener implements OnClickListener {public void onClick (View v) {switch (v. getId () {case R. id. button: Intent intent = new Intent (Intent. ACTION_PICK, android. provider. mediaStore. images. media. EXTERNAL_CONTENT_URI); startActivityForResult (intent, PICK); break; case R. id. saveButton: try {if (mPhotoFileUri! = Null) {OutputStream fos = getContentResolver (). openOutputStream (mPhotoFileUri); mAlteredBitmap. compress (CompressFormat. JPEG, 90, fos); fos. close (); Toast. makeText (MainActivity. this, "OK", Toast. LENGTH_SHORT ). show () ;}} catch (Exception e) {// TODO: handle exception} break; default: break ;}} public boolean onTouch (View v, 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 ;}}