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.view.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;/** * 注意: * 1 event.getX()和event.getRawX()方法的區別 * event.getX()是相對於父控制項而言 * event.getRawX()是相對於螢幕左上方而言 * 2 在Activity中處理觸摸事件採用的是OnTouchEvent() * 在View中處理觸摸事件是實現OnTouchListener{}介面 * 覆寫其中的onTouch()方法 * 3 Bug的問題: * 計算options.inSampleSize這裡有Bug.有的圖片過大在 * 裝置圖片庫中是被旋轉處理了的(雖然看上去沒有).比如一 * 張大圖片在圖片庫中看上去是豎屏的,但得到後在ImageView * 中顯示卻是橫屏的. * 所以這裡方便測試直接: * options.inSampleSize=8; * 寫死了 * 4 調用bitmap.compress()方法的時候注意若設定格式為PNG,那麼則無效. * 因為PNG將始終保持所有資料.JPEG是"有損的"轉碼器 */public class MainActivity extends Activity implements OnTouchListener{ private ImageView mImageView; private Button mButton; private Button mSaveButton; private Bitmap mRawBitmap; private Bitmap mAlteredBitmap; private Paint mPaint; private Canvas mCanvas; private Uri mPhotoFileUri; private final int PICK=88; 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); 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 onActivityResult(int requestCode, int resultCode, Intent data) {super.onActivityResult(requestCode, resultCode, data);if (requestCode==PICK) {mPhotoFileUri=data.getData();Display display=getWindowManager().getDefaultDisplay();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;}}//Bug所在:options.inSampleSize=8;options.inJustDecodeBounds=false;mRawBitmap=BitmapFactory.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 ButtonOnClickListener 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;} }