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

<RelativeLayout
android:id="@+id/rl_me_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_centerHorizontal="true">
<ImageView
android:layout_width="64dp"
android:layout_height="64dp"
android:scaleType="fitXY"
android:src="@drawable/my_user_bg_icon" />
<ImageView
android:id="@+id/iv_me_icon"
android:layout_width="62dp"
android:layout_height="62dp"
android:layout_centerInParent="true"
android:scaleType="fitXY"
android:src="@drawable/my_user_default" />
</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 } 相关推荐
博了个客 2020-05-31
lixiaotao 2020-03-05
songfens 2020-01-10
MIKUScallion 2020-01-10
调调 2013-07-24
83580494 2013-06-29
TongsengsBlog 2013-08-27
Android进阶 2013-08-25
89143951 2011-08-25
八角塘塘主 2019-10-29
lerayZhang 2015-01-11
timewind 2019-07-01
Ashes 2019-06-30
learningITwell 2019-06-28
PGzxc 2019-06-28
xusong 2012-08-01
Android进阶 2019-06-27
helowken 2019-06-21
飞奔的熊猫 2019-06-21