public class SimpleFingerDraw extends Activity implements OnTouchListener,
OnClickListener
{
ImageView imageView;
Button choosePicture, savePicture;
Bitmap bitmap;
Bitmap alteredBitmap;
Canvas canvas;
Paint paint;
Matrix matrix;
float downx = 0;
float downy = 0;
float upx = 0;
float upy = 0;
float top = 0, bottom = 0;
String TAG = "FingerDraw";
@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.ChosenImageView);
choosePicture = (Button) findViewById(R.id.ChoosePictureButton);
savePicture = (Button) findViewById(R.id.SavePictureButton);
choosePicture.setOnClickListener(this);
savePicture.setOnClickListener(this);
savePicture.setVisibility(View.GONE);
}
@Override
public boolean onTouch(View v, MotionEvent event)
{
// TODO Auto-generated method stub
int action = event.getAction();
switch (action)
{
case MotionEvent.ACTION_DOWN:
downx = event.getX();// 擷取的是以ImageView左上角為原點的坐标
downy = event.getY();
Log.e(TAG, "downx:" + downx + " downy:" + downy);
break;
case MotionEvent.ACTION_MOVE:
upx = event.getX();
upy = event.getY();
canvas.drawLine(downx, downy, upx, upy, paint);
imageView.invalidate();
downx = upx;
downy = upy;
break;
case MotionEvent.ACTION_UP:
upx = event.getX();
upy = event.getY();
canvas.drawLine(downx, downy, upx, upy, paint);// 注意:這裡是以圖像的左上角為原點進行繪制直線
imageView.invalidate();
break;
default:
break;
}
return true;
}
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
if (v == choosePicture)
{
Intent choosePictureIntent = new Intent(Intent.ACTION_PICK,
Media.EXTERNAL_CONTENT_URI);
startActivityForResult(choosePictureIntent, 0);
} else
{
if (alteredBitmap != null)
{
try
{
Uri ImageFileUri = getContentResolver().insert(
Media.EXTERNAL_CONTENT_URI, new ContentValues());
OutputStream imageFileOS = getContentResolver()
.openOutputStream(ImageFileUri);
alteredBitmap.compress(CompressFormat.JPEG, 100,
imageFileOS);
Toast.makeText(this, "Saved! ", Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK)
{
savePicture.setVisibility(View.VISIBLE);
Uri imageFileUri = data.getData();
Display currentDisplay = getWindowManager().getDefaultDisplay();
@SuppressWarnings("deprecation")
float dw = currentDisplay.getWidth();
@SuppressWarnings("deprecation")
float dh = currentDisplay.getHeight();
BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
bmpFactoryOptions.inJustDecodeBounds = true;
try
{
bitmap = BitmapFactory
.decodeStream(
getContentResolver().openInputStream(
imageFileUri), null, bmpFactoryOptions);
} catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
int heightRatio = (int) Math.ceil(bmpFactoryOptions.outHeight / dh);
int widthRatio = (int) Math.ceil(bmpFactoryOptions.outWidth / dw);
if (heightRatio >= 1 && widthRatio >= 1)
{
if (heightRatio > widthRatio)
{
bmpFactoryOptions.inSampleSize = heightRatio;
// Log.e(TAG,"heightRatio");
} else
{
bmpFactoryOptions.inSampleSize = widthRatio;
// Log.e(TAG,"widthRatio");
}
}
// Log.e(TAG,"heightRatio:"+heightRatio+" widthRatio:"+widthRatio);
bmpFactoryOptions.inJustDecodeBounds = false;
try
{
bitmap = BitmapFactory
.decodeStream(
getContentResolver().openInputStream(
imageFileUri), null, bmpFactoryOptions);
} catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
alteredBitmap = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), bitmap.getConfig());
Log.e(TAG, "bitmap.Height:" + bitmap.getHeight());
canvas = new Canvas(alteredBitmap);
paint = new Paint();
paint.setColor(Color.GREEN);
paint.setStrokeWidth(5);
matrix = new Matrix();
canvas.drawBitmap(bitmap, matrix, paint);
imageView.setImageBitmap(alteredBitmap);
// top=imageView.getTop(); //144.0
// bottom=imageView.getBottom();//144.0
// height=imageView.getHeight(); //0.0
// Log.e(TAG, "bottom:"+bottom);
imageView.setOnTouchListener(this);
}
}
}