昨天发现一个可以裁剪图片的框架-uCrop,看着说明还是很好用的。自己搜了搜发现百度上各种各样的介绍都是直接粘贴别人的,也不管能不能用就直接发布了。今天自己尝试了一下怎么使用,做个笔记。可能别的大神也写过相关的使用说明,但学到了就是自己的。(宣传图片,盗图一枚)
使用步骤:
第一步:加入jar。他们很多直接贴了github的地址,然后就没了。许多人看了就不知道怎么继续向下进行了。如果你要使用Android Studio的话,可以在build.gradle里面直接添加
compile 'com.yalantis:ucrop:2.2.0'
然后点击sys now,稍等一会就会自动下载下来。
第二步:注册。需要在AndroidManifest.xml中注册说明。
<activity
android:name="com.yalantis.ucrop.UCropActivity"
android:screenOrientation="portrait"
android:theme="@style/Theme.AppCompat.Light.NoActionBar"/>
他们说可以改theme,我还没有尝试过,以后试一试。如果你没有进行注册或者写的位置不对,会报如下错误:
android.content.ActivityNotFoundException: Unable to find explicit activity class
{com.example.dareway.android_work/com.yalantis.ucrop.UCropActivity}; have you declared this activity in your AndroidManifest.xml?
第三步:权限。因为我做了两个,第一个尝试的时候用的网络图片,传的固定的path,所以需要加网络权限,比别人多一个。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<uses-permission android:name='android.permission.INTERNET'/>
第四步:就是主代码了。xml页面就做了两个按钮。固定图片是实验用的,打我啊是用了之前的头像打开相册那个,直接copy之前的代码。
public View.OnClickListener ocl_image2= new View.OnClickListener() {
@Override
public void onClick(View v) {
startCrop("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1495278019643&di=6c22d1811d69402755a40bc0a806c67c&imgtype=0&src=http%3A%2F%2Fi2.hdslb.com%2Fbfs%2Farchive%2F87a0e31b204308c49e239fc5f8c92be91c957b9b.jpg");
}
};
//imageview 点击
public View.OnClickListener ocl_image = new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(Himage.this);
builder.setTitle("选择图片");
final String[] items = {"拍照","从相册中选择"};
builder.setItems(items, new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which)
{
switch (which){
case 0:
// openCamera();
Toast.makeText(Himage.this, "功能待开发", Toast.LENGTH_SHORT).show();
break;
case 1:
Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(intent, 0);
break;
}
}
});
AlertDialog dialog = builder.create();
dialog.setCanceledOnTouchOutside(true);
dialog.show();
}
};
这个是响应打开相册和裁剪提示的,打开相册有些旧代码没有删除就直接粘贴了。
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Bitmap bitmap = null;
if (requestCode==0){
try {
//通过URI得到输入流
InputStream inputStream = getContentResolver().openInputStream(data.getData());
//通过输入流得到bitmap对象
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize = 4;
bitmap = BitmapFactory.decodeStream(inputStream,null,opts);
filePath= GetPath.getPath(this,data.getData());
filePath = "file://"+filePath;
startCrop(filePath);
} catch (Exception e) {
e.printStackTrace();
}
}
if (resultCode == -1) {
//裁切成功
if (requestCode == UCrop.REQUEST_CROP) {
Toast.makeText(this, "裁切图片成功", Toast.LENGTH_SHORT).show();
}
//裁切失败
if (resultCode == UCrop.RESULT_ERROR) {
try{
Toast.makeText(this, "裁切图片失败", Toast.LENGTH_SHORT).show();
}catch (Exception e) {
e.printStackTrace();
Log.w("A+++A",e.getMessage());
}
}
}
}
这是ucrop的接口代码,很简单、方便。
public void startCrop(String path){
try{
Uri uri = Uri.parse(path);
SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyyMMddHHmmss");
Date date = new Date();
String imageName = simpleDateFormat.format(date);
Uri destinationUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory()+"/MyAndroid/Himg/",
imageName+".jpg"));
UCrop uCrop = UCrop.of(uri,destinationUri);
UCrop.Options options = new UCrop.Options();
options.setAllowedGestures(UCropActivity.SCALE, UCropActivity.ROTATE, UCropActivity.ALL);
//设置隐藏底部容器,默认显示
// options.setHideBottomControls(true);
//设置toolbar颜色
options.setToolbarColor(ActivityCompat.getColor(this, R.color.colorPrimary));
//设置状态栏颜色
options.setStatusBarColor(ActivityCompat.getColor(this, R.color.colorPrimary));
//是否能调整裁剪框
// options.setFreeStyleCropEnabled(true);
uCrop.withOptions(options);
uCrop.start(this);
}catch (Exception e) {
e.printStackTrace();
Log.w("BBB++",e.getMessage());
}
}
options其实可以设置很多内容,容我慢慢研究在做(说白了就是懒)。还可以设置裁剪框为圆形的,可以动手尝试一下。
