Skip to content

Commit 42a2e8b

Browse files
author
hss
committed
自定义圆角imageview
1 parent c58c258 commit 42a2e8b

File tree

5 files changed

+92
-18
lines changed

5 files changed

+92
-18
lines changed

app/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ dependencies {
6868
compile 'com.joooonho:selectableroundedimageview:1.0.1'
6969
compile 'com.github.siyamed:android-shape-imageview:0.9.+@aar'
7070

71+
compile 'org.raphets:roundimageview:1.2.0'
72+
7173

7274

7375
}

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
66
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
77

8-
<uses-sdk tools:overrideLibrary="kale.adapter,pl.droidsonroids.gif"></uses-sdk>
8+
<uses-sdk tools:overrideLibrary="kale.adapter,pl.droidsonroids.gif,org.raphets.roundimageview"></uses-sdk>
99

1010
<application
1111
android:allowBackup="true"

app/src/main/java/com/hss01248/imageloaderdemo/multi/RcvHolder.java

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
import com.hss01248.imageloaderdemo.R;
3535
import com.makeramen.roundedimageview.RoundedImageView;
3636

37+
import org.raphets.roundimageview.RoundImageView;
38+
3739
import java.io.File;
3840
import java.util.List;
3941
import java.util.PriorityQueue;
@@ -46,13 +48,15 @@ public class RcvHolder extends SuperRvHolder<String,Activity> {
4648
//public ImageView imageView;
4749
private int imageSize;
4850
private int columnNumber;
49-
private RoundedImageView roundedImageView2;
51+
//private RoundedImageView roundedImageView2;
5052
private ImageLoaderRoundImageView roundImageView;
53+
private RoundImageView roundImageView3;
5154
public RcvHolder(View itemView) {
5255
super(itemView);
5356
this.rootView = itemView;
5457
roundImageView = itemView.findViewById(R.id.item_iv);
55-
roundedImageView2 = itemView.findViewById(R.id.iv_round2);
58+
//roundedImageView2 = itemView.findViewById(R.id.iv_round2);
59+
roundImageView3 = itemView.findViewById(R.id.iv_round3);
5660
//imageView = (ImageView) itemView.findViewById(R.id.item_iv);
5761

5862
}
@@ -87,26 +91,36 @@ public void assignDatasAndEvents(Activity context, final String data, int positi
8791

8892
//loadByGlide(context,data,position);
8993

90-
roundImageView.isCircle(true);
91-
roundImageView.setBorderWidth(2);
92-
roundImageView.setBorderColor(Color.BLUE);
94+
roundImageView.setCornerRadius(20,20,0,0);
95+
//roundImageView.setBorderWidth(2);
96+
//roundImageView.setBorderColor(Color.BLUE);
9397
ImageLoader.with(context)
9498
//.widthHeightByPx(360,360)
9599
.url(data)
96100
//.scale(ScaleMode.CENTER_CROP)
97101
//.blur(2)
98102
.defaultErrorRes(true)
99-
.loading(R.drawable.iv_loading_trans)
103+
//.loading(R.drawable.iv_loading_trans)
104+
.placeHolder(R.color.colorAccent)
100105
.into(roundImageView);
101106

102-
ImageLoader.with(context)
107+
/* ImageLoader.with(context)
103108
//.widthHeightByPx(360,360)
104109
.url(data+"?t=4")
105-
.scale(ScaleMode.CENTER_CROP)
110+
//.scale(ScaleMode.CENTER_CROP)
111+
//.blur(2)
112+
.defaultErrorRes(true)
113+
.loading(R.drawable.iv_loading_trans)
114+
.into(roundedImageView2);*/
115+
116+
ImageLoader.with(context)
117+
//.widthHeightByPx(360,360)
118+
.url(data+"?t=5")
119+
//.scale(ScaleMode.CENTER_CROP)
106120
//.blur(2)
107121
.defaultErrorRes(true)
108122
.loading(R.drawable.iv_loading_trans)
109-
.into(roundedImageView2);
123+
.into(roundImageView3);
110124

111125
/*.asBitmap(new SingleConfig.BitmapListener() {
112126
@Override

app/src/main/res/layout/item_iv.xml

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
34
android:id="@+id/item_root"
45
android:orientation="vertical"
56
android:layout_width="match_parent"
67
android:layout_height="match_parent">
7-
<com.hss01248.glideloader.ImageLoaderRoundImageView xmlns:android="http://schemas.android.com/apk/res/android"
8+
<com.hss01248.glideloader.ImageLoaderRoundImageView
89
android:id="@+id/item_iv"
9-
android:background="@drawable/shape_main_list_bg"
1010
android:layout_width="match_parent"
1111
android:layout_marginBottom="5dp"
1212
android:adjustViewBounds="true"
1313
android:layout_height="wrap_content">
1414

1515
</com.hss01248.glideloader.ImageLoaderRoundImageView>
1616

17-
<com.makeramen.roundedimageview.RoundedImageView
18-
xmlns:app="http://schemas.android.com/apk/res-auto"
17+
<!--<com.makeramen.roundedimageview.RoundedImageView
1918
android:id="@+id/iv_round2"
2019
android:background="@drawable/shape_main_list_bg"
2120
android:layout_width="match_parent"
@@ -25,5 +24,19 @@ android:scaleType="fitCenter"
2524
app:riv_corner_radius="10dip"
2625
app:riv_border_width="2dip"
2726
app:riv_border_color="#333333"
28-
app:riv_mutate_background="true" />
27+
app:riv_mutate_background="true" />-->
28+
29+
<org.raphets.roundimageview.RoundImageView
30+
android:layout_width="match_parent"
31+
android:adjustViewBounds="true"
32+
android:layout_height="wrap_content"
33+
android:id="@+id/iv_round3"
34+
app:border_width="3dp"
35+
android:scaleType="fitCenter"
36+
app:border_color="@android:color/holo_red_dark"
37+
app:leftBottom_corner_radius="0dp"
38+
app:leftTop_corner_radius="30dp"
39+
app:rightBottom_corner_radius="30dp"
40+
app:rightTop_corner_radius="0dp"
41+
app:type="round" />
2942
</LinearLayout>

glide/src/main/java/com/hss01248/glideloader/ImageLoaderRoundImageView.java

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public class ImageLoaderRoundImageView extends AppCompatImageView {
4141
private int borderColor = Color.WHITE;
4242

4343
private int cornerRadius;
44+
private float leftTop, rightTop, rightBottom, leftBottom;
4445

4546
private Xfermode xfermode;
4647

@@ -106,6 +107,9 @@ public ImageLoaderRoundImageView(Context context, AttributeSet attrs, int defSty
106107
xfermode = new PorterDuffXfermode(PorterDuff.Mode.DST_OUT);
107108
srcPath = new Path();
108109
}
110+
if(cornerRadius>0){
111+
setCornerRadius(cornerRadius);
112+
}
109113
calculateRadii();
110114
}
111115

@@ -209,17 +213,43 @@ private void calculateRadii() {
209213
if (isCircle) {
210214
return;
211215
}
212-
if (cornerRadius > 0) {
213-
for (int i = 0; i < borderRadii.length; i++) {
216+
if (getMaxCorner() > 0) {
217+
borderRadii[0] = leftTop;
218+
borderRadii[1] = leftTop;
219+
borderRadii[2] = rightTop;
220+
borderRadii[3] = rightTop;
221+
borderRadii[4] = rightBottom;
222+
borderRadii[5] = rightBottom;
223+
borderRadii[6] = leftBottom;
224+
borderRadii[7] = leftBottom;
225+
226+
227+
srcRadii[0] = leftTop - borderWidth / 2.0f;
228+
srcRadii[1] = leftTop - borderWidth / 2.0f;
229+
srcRadii[2] = rightTop- borderWidth / 2.0f;
230+
srcRadii[3] = rightTop- borderWidth / 2.0f;
231+
srcRadii[4] = rightBottom- borderWidth / 2.0f;
232+
srcRadii[5] = rightBottom- borderWidth / 2.0f;
233+
srcRadii[6] = leftBottom- borderWidth / 2.0f;
234+
srcRadii[7] = leftBottom- borderWidth / 2.0f;
235+
236+
237+
238+
239+
/*for (int i = 0; i < borderRadii.length; i++) {
214240
borderRadii[i] = cornerRadius;
215241
srcRadii[i] = cornerRadius - borderWidth / 2.0f;
216-
}
242+
}*/
217243
}
218244
}
219245

220246
private void calculateRadiiAndRectf(boolean reset) {
221247
if (reset) {
222248
cornerRadius = 0;
249+
leftTop = 0;
250+
leftBottom = 0;
251+
rightBottom = 0;
252+
rightTop = 0;
223253
}
224254
calculateRadii();
225255
initBorderRectf();
@@ -235,13 +265,28 @@ public void isCircle(boolean isCircle) {
235265
invalidate();
236266
}
237267

268+
public float getMaxCorner(){
269+
return Math.max(rightBottom,Math.max(leftBottom,Math.max(leftTop,rightTop)));
270+
}
271+
238272
/**
239273
* 设置圆角
240274
*
241275
* @param cornerRadius 圆角/dp
242276
*/
243277
public void setCornerRadius(int cornerRadius) {
244278
this.cornerRadius = MyUtil.dip2px(cornerRadius);
279+
this.leftTop = cornerRadius;
280+
this.rightTop = cornerRadius;
281+
this.leftBottom = cornerRadius;
282+
this.rightBottom = cornerRadius;
283+
calculateRadiiAndRectf(false);
284+
}
285+
public void setCornerRadius(float leftTop, float rightTop, float leftBottom,float rightBottom) {
286+
this.leftTop = MyUtil.dip2px(leftTop);
287+
this.rightTop = MyUtil.dip2px(rightTop);
288+
this.leftBottom = MyUtil.dip2px(leftBottom);
289+
this.rightBottom = MyUtil.dip2px(rightBottom);
245290
calculateRadiiAndRectf(false);
246291
}
247292

0 commit comments

Comments
 (0)