最近在做的B2B的项目,图片大部分来源于用户自己上传;
由于android尺寸的不一,用户相机,相册的图片也是奇形怪状;
所以在上传之前对图片做一次裁剪是很有必要的!

下面是按比例裁剪图片的demo

资源文件activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.croppicturedemo.MainActivity" >    <LinearLayout  android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">         <Button  android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="选择图片" />        <ImageView android:id="@+id/imageview" android:layout_width="match_parent" android:layout_height="match_parent"/>    </LinearLayout></RelativeLayout>

MainActivity

public class MainActivity extends Activity implements OnClickListener {        private ImageView imageview;        private Button btn;        private static final int PHOTO_REQUEST_TAKEPHOTO = 1;// 拍照        private static final int PHOTO_REQUEST_GALLERY = 2;// 从相册中选择        private static final int PHOTO_REQUEST_CUT = 3;// 结果        // 创建一个以当前时间为名称的文件        File tempFile = new File(Environment.getExternalStorageDirectory(),getPhotoFileName());        /** Called when the activity is first created. */        @Override        public void onCreate(Bundle savedInstanceState) {            super.onCreate(savedInstanceState);            setContentView(R.layout.activity_main);            init();        }        //初始化控件        private void init() {            imageview = (ImageView) findViewById(R.id.imageview);            btn = (Button) findViewById(R.id.btn);            //为ImageButton和Button添加监听事件            imageview.setOnClickListener(this);            btn.setOnClickListener(this);        }        //点击事件        @Override        public void onClick(View v) {            // TODO Auto-generated method stub            switch (v.getId()) {            case R.id.imageview:                showDialog();                break;            case R.id.btn:                showDialog();                break;            }        }        //提示对话框方法        private void showDialog() {            new AlertDialog.Builder(this)                    .setTitle("头像设置")                    .setPositiveButton("拍照", new DialogInterface.OnClickListener() {                        @Override                        public void onClick(DialogInterface dialog, int which) {                            // TODO Auto-generated method stub                            dialog.dismiss();                            // 调用系统的拍照功能                            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);                            // 指定调用相机拍照后照片的储存路径                            intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(tempFile));                            startActivityForResult(intent, PHOTO_REQUEST_TAKEPHOTO);                        }                    })                    .setNegativeButton("相册", new DialogInterface.OnClickListener() {                        @Override                        public void onClick(DialogInterface dialog, int which) {                            // TODO Auto-generated method stub                            dialog.dismiss();                            Intent intent = new Intent(Intent.ACTION_PICK, null);                            intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,"image/*");                            startActivityForResult(intent, PHOTO_REQUEST_GALLERY);                        }                    }).show();        }        @Override        protected void onActivityResult(int requestCode, int resultCode, Intent data) {            // TODO Auto-generated method stub            switch (requestCode) {            case PHOTO_REQUEST_TAKEPHOTO:                startPhotoZoom(Uri.fromFile(tempFile), 150);                break;            case PHOTO_REQUEST_GALLERY:                if (data != null)                    startPhotoZoom(data.getData(), 150);                break;            case PHOTO_REQUEST_CUT:                if (data != null)                     setPicToView(data);                break;            }            super.onActivityResult(requestCode, resultCode, data);        }        private void startPhotoZoom(Uri uri, int size) {            Intent intent = new Intent("com.android.camera.action.CROP");            intent.setDataAndType(uri, "image/*");            // crop为true是设置在开启的intent中设置显示的view可以剪裁            intent.putExtra("crop", "true");            // aspectX aspectY 是宽高的比例            intent.putExtra("aspectX", 1);            intent.putExtra("aspectY", 1);            // outputX,outputY 是剪裁图片的宽高            intent.putExtra("outputX", size);            intent.putExtra("outputY", size);            intent.putExtra("scale", true);              intent.putExtra("return-data", true);            startActivityForResult(intent, PHOTO_REQUEST_CUT);        }        //将进行剪裁后的图片显示到UI界面上        private void setPicToView(Intent picdata) {            Bundle bundle = picdata.getExtras();            if (bundle != null) {                Bitmap photo = bundle.getParcelable("data");                Drawable drawable = new BitmapDrawable(photo);                imageview.setBackgroundDrawable(drawable);            }        }        // 使用系统当前日期加以调整作为照片的名称        private String getPhotoFileName() {            Date date = new Date(System.currentTimeMillis());            SimpleDateFormat dateFormat = new SimpleDateFormat("'IMG'_yyyyMMdd_HHmmss");            return dateFormat.format(date) + ".jpg";        }}

demo下载地址:点击下载

更多相关文章

  1. Android(安卓)matrix 控制图片的旋转、缩放、移动
  2. android拍照与读取相册
  3. Android(安卓)报错:Caused by: android.os.FileUriExposedExcepti
  4. android解决坚屏拍照和保存图片旋转90度的问题,并兼容4.0
  5. [Android]在App中使用相机
  6. android WebView 图片缩放功能小结
  7. Android(安卓)主流图片库Picasso Glide Fresco对比分析
  8. android背景选择器selector用法汇总
  9. ImageView的scaletype属性

随机推荐

  1. Android开发起步
  2. android默认系统日期、时间、时区更改
  3. Android(安卓)- Android(安卓)的消息机制
  4. Android(安卓)Layout文件的属性说明
  5. Windows Phone 7 不温不火学习之《ListBo
  6. Android(安卓)画图之Matrix(二)
  7. Android(安卓)源代码目录结构1 - bionic
  8. android “分享”功能的实现
  9. Android的文件系统
  10. Android(安卓)NDK1、Android(安卓)NDK带