1、依赖引入:
compile 'com.edmodo:cropper:1.0.1'
2、布局引入:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.ceshi.allworkdemo.MainActivity">
<com.edmodo.cropper.CropImageView
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:id="@+id/CropImageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/imageView"
android:layout_centerHorizontal="true"
android:background="#ffffff"/>
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/lay"
android:layout_centerHorizontal="true"/>
<LinearLayout
android:id="@+id/lay"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="50dp">
<TextView
android:id="@+id/tet_11"
android:layout_weight="1"
android:gravity="center"
android:textColor="#333333"
android:textSize="15sp"
android:text="1:1"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
<TextView
android:id="@+id/tet_43"
android:layout_weight="1"
android:gravity="center"
android:textColor="#333333"
android:textSize="15sp"
android:text="4:3"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
<TextView
android:id="@+id/tet_34"
android:layout_weight="1"
android:gravity="center"
android:textColor="#333333"
android:textSize="15sp"
android:text="3:4"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
<TextView
android:id="@+id/tet_ok"
android:layout_weight="1"
android:gravity="center"
android:textColor="#333333"
android:textSize="15sp"
android:text="裁剪"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
</LinearLayout>
</RelativeLayout>
3、主要代码:
mCropImageView.setImageBitmap(getBitmap(R.drawable.bg));//为了兼容小图片,必须在代码中加载图片
mCropImageView.setFixedAspectRatio(true);//设置允许按比例截图,如果不设置就是默认的任意大小截图
mCropImageView.setAspectRatio(1, 1);//设置比例为一比一
// mCropImageView.setGuidelines(CropImageView.ON);//设置显示网格的时机,默认为on_touch
/**
* @param resId
* @return 如果图片太小,那么就拉伸
*/
public Bitmap getBitmap(int resId) {
WindowManager wm = (WindowManager) getBaseContext().getSystemService(Context.WINDOW_SERVICE);
int width = wm.getDefaultDisplay().getWidth();
Bitmap bitmap= BitmapFactory.decodeResource(getResources(), resId);
float scaleWidth = 1,scaleHeight = 1;
if(bitmap.getWidth() < width) {
scaleWidth = width / bitmap.getWidth();
scaleHeight = scaleWidth;
}
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),bitmap.getHeight()
, matrix, true);
return bitmap;
}
Bitmap bitmap = mCropImageView.getCroppedImage();//得到裁剪好的图片 ImageView croppedImageView = (ImageView) findViewById(R.id.imageView); croppedImageView.setImageBitmap(bitmap);//设置到imageview中
4、效果展示: