classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.6'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
复制到你项目中的build.gradle,整个build.gradle应该像这样
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.6'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
mCropView = (CropImageView)findViewById(R.id.cropImageView);
mCropView.setInitialFrameScale(0.8f);
String srcPicPath = "图片路径";
File file = new File(srcPicPath);
BitmapFactory.Options option = new BitmapFactory.Options();
if(file.length()>2*1024*1024){ //如果原图大于2M,压缩,避免程序崩溃
option.inSampleSize = (int)file.length()/(2*1024*1024);
Log.d(TAG, "initCropView: "+option.inSampleSize);
}
Bitmap bitmap = BitmapFactory.decodeFile(srcPicPath,option);
mCropView.setImageBitmap(bitmap);
if(bitmap.isRecycled()){
bitmap.recycle();
}
private void saveBitmapToPath(String path){ //path为图片存储路径
Bitmap bitmap = mCropView.getCroppedBitmap();
try {
File f = new File(path);
if (f.exists()) {
f.delete();
}
FileOutputStream out = new FileOutputStream(f);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (IOException e) {
Log.d(TAG, "saveBitmapToPath: "+e.toString());
}
}
colors.xml
#3F51B5
#303F9F
#FF4081
#AA1C1C1C
#1C1C1C
#5DAC81
#4F916D
#5DAC81
#FFFFFB
#FFFFFB
#1C1C1C
#333333
CropActivity.class package cn.edu.scu.creator.xxx;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import com.isseiaoki.simplecropview.CropImageView;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import static android.R.attr.button;
import static android.R.attr.data;
public class CropActivity extends AppCompatActivity {
private final static String TAG = MainActivity.TAG;
private CropImageView mCropView ;
private Button btOK;
private Button btCancel;
private Button btRotate;
private String srcPicPath;
private String dstPicPath;
/**
* 创建本Activity需要的Intent
* @param activity 调用本Activity的Activity
* @param srcPicPath 原图路径
* @param dstPicPath 保存路径
* @return
*/
public static Intent createIntent(AppCompatActivity activity,String srcPicPath, String dstPicPath){
Intent intent = new Intent(activity,CropActivity.class);
intent.putExtra("srcPicPath", srcPicPath);
intent.putExtra("dstPicPath", dstPicPath);
return intent;
}
/**
* 初始按钮和mCropView组件
* @param savedInstanceState
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_crop);
mCropView = (CropImageView)findViewById(R.id.cropImageView);
mCropView.setInitialFrameScale(0.8f); //设置裁剪框大小
mCropView.setCropMode(CropImageView.CropMode.RATIO_9_16); //设置裁剪框比例
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { //设置裁剪框颜色
mCropView.setFrameColor(getResources().getColor(R.color.frame,this.getTheme()));
mCropView.setHandleColor(getResources().getColor(R.color.handle,this.getTheme()));
mCropView.setGuideColor(getResources().getColor(R.color.guide,this.getTheme()));
} else {
mCropView.setFrameColor(getResources().getColor(R.color.frame));
mCropView.setHandleColor(getResources().getColor(R.color.handle));
mCropView.setGuideColor(getResources().getColor(R.color.guide));
}
btOK = (Button)findViewById(R.id.btOK);
btOK.setOnClickListener(onClickListener);
btCancel = (Button)findViewById(R.id.btCancel);
btCancel.setOnClickListener(onClickListener);
btRotate = (Button)findViewById(R.id.btRotate);
btRotate.setOnClickListener(onClickListener);
initCropView();
}
/**
* 从Intent获取原图路径和保存图片路径,并载入到mCropView控件中
*/
private void initCropView(){
try {
Intent intent = this.getIntent();
Bundle bundle = intent.getExtras();
if (bundle != null) {
srcPicPath = bundle.getString("srcPicPath"); // 原图路径
dstPicPath = bundle.getString("dstPicPath"); // 存储路径
BitmapFactory.Options option = new BitmapFactory.Options();
File file = new File(srcPicPath);
if(file.length()>2*1024*1024){ //如果原图大于2M,压缩,避免程序崩溃
option.inSampleSize = (int)file.length()/(2*1024*1024);
Log.d(TAG, "initCropView: "+option.inSampleSize);
}
Bitmap bitmap = BitmapFactory.decodeFile(srcPicPath,option);
mCropView.setImageBitmap(bitmap);
if(bitmap.isRecycled()){
bitmap.recycle();
}
}
}catch (Exception e){
Log.d(TAG, "onCreate: "+e.toString());
}
}
/**
* 按钮监听器
*/
private View.OnClickListener onClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btOK:
saveBitmapToPath(mCropView.getCroppedBitmap(),dstPicPath);
break;
case R.id.btCancel:
returnToMainActivity("",RESULT_CANCELED);
break;
case R.id.btRotate:
rotateImage();
break;
default:
break;
}
}
};
/**
* 保存图像
* @param bitmap 要保存的图像
* @param path 保存路径
*/
private void saveBitmapToPath(Bitmap bitmap,String path){
// mCropView.setImageBitmap(bitmap);
try {
File f = new File(path);
if (f.exists()) {
f.delete();
}
FileOutputStream out = new FileOutputStream(f);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (IOException e) {
Log.d(TAG, "saveBitmapToPath: "+e.toString());
}
returnToMainActivity(path,RESULT_OK);
}
/**
* 返回上一个Activity,并将保存路径放在Intent中返回
* @param dstPath 保存路径
* @param result 结果
*/
private void returnToMainActivity(String dstPath,int result){
Intent intent = new Intent();
intent.putExtra("dstPath", dstPath);
setResult(result, intent);
finish();
}
/**
* 旋转图片
*/
private void rotateImage(){
mCropView.rotateImage(CropImageView.RotateDegrees.ROTATE_90D);
}
}
public class MainActivity extends AppCompatActivity {
public final static String TAG = "TAG"; //调试用TAG
public final static int REQUEST_CROP = 2;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CROP && resultCode == RESULT_OK && data != null) {
//裁剪成功的操作
}
}
//裁剪图片,path为图片路径
private void cropPicture(String path){
try {
File srcPic = new File(path);
String toPath = Environment.getExternalStorageDirectory().getPath() + "/MyApp";
File destDir = new File(toPath);
if (!destDir.exists()) {
destDir.mkdirs();
}
String savePath = toPath+"/bg.jpeg"; //裁剪后的图片保存路径
startActivityForResult(CropActivity.createIntent(this,path,savePath), REQUEST_CROP);
}catch (Exception e){
Log.d(TAG, "cropPicture: "+e.toString());
}
}
}
读我.txt 将activity_crop.xml、colors.xml、CropActivity.java放到该放的位置,使用方法在MainActivity.java中。