概要
启动Android手机相机功能拍照,拍照后将按照指定尺寸对图片进行裁剪,在当前界面显示裁剪后的效果,这个小功能经常使用在修改头像的应用中。
系统相机
调用系统相机功能,拍摄照片,并将拍摄的照片保存在指定路径,完成后在当前界面显示,执行回调方法onActivityResult(int arg0,int arg1,Intent arg2),通过arg0参数判断是否拍照返回,arg2是返回的数据
- /**
- * 调用系统相机
- */
- private static File photoFile;
- private void goToTakePhoto() {
- String tempFileName = System.currentTimeMillis() + ".jpg";
- // 指定调用相机拍照后照片的储存路径
- photoFile = new File(Environment.getExternalStorageDirectory(),
- "temp_capture" + tempFileName);
- Intent cameraintent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
- cameraintent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
- startActivityForResult(cameraintent, RESULT_CAPTURE_IMAGE);
- }
系统截图
系统截图,需要设置截图的图片尺寸,图片保存的路径、名称,同样完成截图后,执行回调方法onActivityResult(int arg0,int arg1,Intent arg2),通过arg0参数判断是否截图返回,arg2是返回的数据
- /**
- * 系统截图
- *
- * @param uri
- */
- private static File photoFinal;
- public static void startPhotoZoom(Context context, Uri uri) {
- Log.e("TeachCourse", "------------裁剪");
- photoUploadTempName = System.currentTimeMillis() + ".jpg";
- File ZoomFile = new File(Environment.getExternalStorageDirectory()
- + "/temp");
- if (!ZoomFile.exists()) {
- ZoomFile.mkdirs();
- }
- photoFinal = new File(ZoomFile, photoUploadTempName);
- Intent intent = new Intent("com.android.camera.action.CROP");
- intent.setDataAndType(uri, "image/*");
- intent.putExtra("crop", "true");
- intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFinal)); // 裁剪后临时储存的jpg,return为true,直接加入bundle,并finish
- intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
- // aspectX,aspectY 是宽高的比例
- intent.putExtra("aspectX", 1);
- intent.putExtra("aspectY", 1);
- // outputX,outputY 是裁剪图片宽高
- intent.putExtra("outputX", 700);
- intent.putExtra("outputY", 700);
- intent.putExtra("scale", true);// 黑边
- intent.putExtra("scaleUpIfNeeded", true);// 黑边
- intent.putExtra("return-data", false);
- intent.addCategory(Intent.CATEGORY_DEFAULT);
- ((FragmentActivity) context).startActivityForResult(intent, PHOTO_CROP);
- }
裁剪后效果
获取保存在手机SDCard中的图片,通过ImageView显示
- /**
- * 获取裁剪后照片
- *
- * @param data
- * @throws IOException
- * @throws FileNotFoundException
- */
- private ImageView mCapture_img;
- private void getImageFromSystem(Intent data) throws FileNotFoundException,
- IOException {
- if (data != null) {
- File file = photoFinal;
- if (file != null) {
- mCapture_img.setImageDrawable(Drawable.createFromPath(file
- .getPath()));
- Toast.makeText(MainActivity.this, "裁剪成功", Toast.LENGTH_SHORT)
- .show();
- }
- }
- }
设置图像形状
自定义CircleImageView继承ImageView,重写onDraw(Canvas canvas)方法,绘制圆形头像,代码如下:
- @Override
- protected void onDraw(Canvas canvas) {
- if (getDrawable() == null) {
- return;
- }
- canvas.drawCircle(getWidth() / 2, getHeight() / 2, mDrawableRadius,
- mBitmapPaint);
- if (mBorderWidth != 0) {
- canvas.drawCircle(getWidth() / 2, getHeight() / 2, mBorderRadius,
- mBorderPaint);
- }
- }
你可能感兴趣的文章
来源:TeachCourse,
每周一次,深入学习Android教程,关注(QQ158#9359$239或公众号TeachCourse)
转载请注明出处: https://www.teachcourse.cn/1233.html ,谢谢支持!
转载请注明出处: https://www.teachcourse.cn/1233.html ,谢谢支持!
分类:Android基础
标签:Android, CircleImageView