简单的手指绘图并保存所绘图片【源码】
【摘要】
public class SimpleFingerDraw extends Activity implements OnTouchListener, OnClickListener{ ImageView imageView; Button choosePicture, savePicture; Bitmap bitmap; Bitma...
-
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);
-
}
-
}
-
-
}
文章来源: panda1234lee.blog.csdn.net,作者:panda1234lee,版权归原作者所有,如需转载,请联系作者。
原文链接:panda1234lee.blog.csdn.net/article/details/8727275
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)