1.需求:
对图片进行裁剪,但是不采用Glide提供的CenterCrop和FitCenter裁剪方式,
而是利用Glide去获取图片的Bitmap,然后对Bitmap进行相对于的裁剪操作。
2.导入glide
导入最新稳定3.7.0版本的Glide:
compile 'com.github.bumptech.glide:glide:3.7.0'
3.对图片进行裁剪
view处理类:
/** * Created by can on 2017/08/11. * view处理类 */ public class ViewUtils { /** * 添加imageview * @param context 上下文 * @param string 图片地址 * @return ImageView */ public static ImageView addImageView(Context context,String string){ final ImageView iv = new ImageView(context); iv.setScaleType(ImageView.ScaleType.CENTER_CROP); //设置ImageView的填充方式 if(Util.isOnMainThread()) //判断是否在主线程处理 Glide.with(context).load(string).asBitmap().override(750,280).into(new SimpleTarget<Bitmap>() { @Override public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) { if(resource!=null){ Bitmap bitmap = BitmapCut.cutBitmap(resource); //调用裁剪图片工具类进行裁剪 if(bitmap!=null) iv.setImageBitmap(bitmap); //设置Bitmap到图片上 } } }); //方法中设置asBitmap可以设置回调类型 return iv; } }
裁剪图片类:
/** * Created by can on 2017/08/11. * 图片裁剪类 */ public class BitmapCut { /** * 裁剪图片 */ public static Bitmap cutBitmap(Bitmap bm){ Bitmap bitmap = null; if(bm!=null){ bitmap = Bitmap.createBitmap(bm,0,0,bm.getWidth(),bm.getHeight()/2); //对图片的高度的一半进行裁剪 } return bitmap; } }
4.结果演示:
裁剪前:
裁剪后: