对图像进行压缩再进行圆形处理

①压缩处理

②圆形处理

 1 <RelativeLayout
 2     android:id="@+id/rl_me_icon"
 3     android:layout_width="wrap_content"
 4     android:layout_height="wrap_content"
 5     android:layout_marginTop="4dp"
 6     android:layout_centerHorizontal="true">
 7 
 8     <ImageView
 9         android:layout_width="64dp"
10         android:layout_height="64dp"
11         android:scaleType="fitXY"
12         android:src="@drawable/my_user_bg_icon" />
13 
14     <ImageView
15         android:id="@+id/iv_me_icon"
16         android:layout_width="62dp"
17         android:layout_height="62dp"
18         android:layout_centerInParent="true"
19         android:scaleType="fitXY"
20         android:src="@drawable/my_user_default" />
21 </RelativeLayout>
 1 @Bind(R.id.iv_me_icon)
 2 ImageView ivMeIcon;
 3 
 4 //1.读取本地保存的用户信息
 5 User user = ((BaseActivity) this.getActivity()).readUser();
 6 
 7 //使用Picasso联网获取图片
 8 Picasso.with(this.getActivity()).load(user.getImageurl()).transform(new Transformation() {
 9     @Override
10     public Bitmap transform(Bitmap source) {//下载以后的内存中的bitmap对象
11         //压缩处理
12         Bitmap bitmap = BitmapUtils.zoom(source, UIUtils.dp2px(62),UIUtils.dp2px(62));
13         //圆形处理
14         bitmap = BitmapUtils.circleBitmap(bitmap);
15         //回收bitmap资源
16         source.recycle();
17         return bitmap;
18     }
19 
20     @Override
21     public String key() {
22         return "";//需要保证返回值不能为null。否则报错
23     }
24 }).into(ivMeIcon);
 1 public class BitmapUtils {
 2 
 3     public static Bitmap circleBitmap(Bitmap source) {
 4         //获取Bitmap的宽度
 5         int width = source.getWidth();
 6         //以Bitmap的宽度值作为新的bitmap的宽高值。
 7         Bitmap bitmap = Bitmap.createBitmap(width, width, Bitmap.Config.ARGB_8888);
 8         //以此bitmap为基准,创建一个画布
 9         Canvas canvas = new Canvas(bitmap);
10         Paint paint = new Paint();
11         paint.setAntiAlias(true);
12         //在画布上画一个圆
13         canvas.drawCircle(width / 2, width / 2, width / 2, paint);
14 
15         //设置图片相交情况下的处理方式
16         //setXfermode:设置当绘制的图像出现相交情况时候的处理方式的,它包含的常用模式有:
17         //PorterDuff.Mode.SRC_IN 取两层图像交集部分,只显示上层图像
18         //PorterDuff.Mode.DST_IN 取两层图像交集部分,只显示下层图像
19         paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
20         //在画布上绘制bitmap
21         canvas.drawBitmap(source, 0, 0, paint);
22 
23         return bitmap;
24 
25     }
26 
27     //实现图片的压缩处理
28     //设置宽高必须使用浮点型,否则导致压缩的比例:0
29     public static Bitmap zoom(Bitmap source,float width ,float height){
30 
31         Matrix matrix = new Matrix();
32         //图片的压缩处理
33         matrix.postScale(width / source.getWidth(),height / source.getHeight());
34 
35         Bitmap bitmap = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, false);
36         return bitmap;
37     }
38 
39 }

猜你喜欢

转载自www.cnblogs.com/ganchuanpu/p/8997752.html
今日推荐