android中用Fresco实现圆角图片和圆形图片

效果图

img

代码

需要注意的地方用注释给出。

可以在github上下载源码。点我试试

1
2
3
4
5
6
7
8
9
Uri uri = Uri.parse("https://pic4.zhimg.com/03b2d57be62b30f158f48f388c8f3f33_b.png");
SimpleDraweeView commonImageView = (SimpleDraweeView) findViewById(R.id.commonImageView);
commonImageView.setImageURI(uri);

SimpleDraweeView circleImageView = (SimpleDraweeView) findViewById(R.id.circleImageView);
circleImageView.setImageURI(uri);

SimpleDraweeView roundedImageView = (SimpleDraweeView) findViewById(R.id.roundedImageView);
roundedImageView.setImageURI(uri);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="common image" />

<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/commonImageView"
android:layout_width="130dp"
android:layout_height="130dp"
fresco:actualImageScaleType="centerCrop"
fresco:placeholderImage="@mipmap/ic_launcher"
fresco:placeholderImageScaleType="centerCrop" />

</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="circle image" />

<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/circleImageView"
android:layout_width="130dp"
android:layout_height="130dp"
fresco:actualImageScaleType="centerCrop"
fresco:placeholderImage="@mipmap/ic_launcher"
fresco:placeholderImageScaleType="centerCrop"
fresco:roundAsCircle="true" />

</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="rounded image" />

<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/roundedImageView"
android:layout_width="130dp"
android:layout_height="130dp"
fresco:actualImageScaleType="centerCrop"
fresco:placeholderImage="@mipmap/ic_launcher"
fresco:placeholderImageScaleType="centerCrop"
fresco:roundedCornerRadius="25dp" />

</LinearLayout>
</LinearLayout>

参考文章

原文

Fork me on GitHub