Skip to content

Commit f1c37a6

Browse files
[core] ImageView: implement scale type
> Allow scale in fit, fill, crop and wrap in ImageView
1 parent 43e56f4 commit f1c37a6

3 files changed

Lines changed: 124 additions & 20 deletions

File tree

core/res/ex-layout/main.xml

Lines changed: 76 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8" ?>
2-
<FrameLayout
2+
<LinearLayout
33
width="match_parent"
44
height="wrap_content"
55
paddingBottom="8dp"
@@ -8,12 +8,85 @@
88
background="#AAA"
99
gravity="center">
1010

11+
<ScrollView
12+
width="200dp"
13+
height="match_parent">
14+
<EditText
15+
width="match_parent"
16+
height="wrap_content"
17+
hint="Example"
18+
margin="5dp"/>
19+
</ScrollView>
20+
1121
<LinearLayout
1222
width="match_parent"
13-
height="wrap_content"
23+
height="match_parent"
1424
layoutGravity="center|top"
1525
maxWidth="1024dp">
1626

27+
<LinearLayout
28+
width="match_parent"
29+
height="wrap_content"
30+
orientation="horizontal"
31+
padding="10dp">
32+
33+
34+
<ImageView
35+
width="50dp"
36+
height="100dp"
37+
background="#C00"
38+
src="@drawable/default_window_icon"
39+
scaleType="fill"
40+
margin="10dp"/>
41+
42+
<ImageView
43+
width="50dp"
44+
height="100dp"
45+
background="#C00"
46+
src="@drawable/default_window_icon"
47+
scaleType="crop"
48+
margin="10dp"/>
49+
50+
<ImageView
51+
width="50dp"
52+
height="100dp"
53+
background="#C00"
54+
src="@drawable/default_window_icon"
55+
margin="10dp"/>
56+
57+
</LinearLayout>
58+
<LinearLayout
59+
width="match_parent"
60+
height="wrap_content"
61+
orientation="horizontal"
62+
padding="10dp">
63+
64+
65+
<ImageView
66+
width="100dp"
67+
height="72dp"
68+
background="#C00"
69+
src="@drawable/default_window_icon"
70+
scaleType="fill"
71+
margin="10dp"/>
72+
73+
<ImageView
74+
width="100dp"
75+
height="72dp"
76+
background="#C00"
77+
src="@drawable/default_window_icon"
78+
scaleType="wrap"
79+
margin="10dp"/>
80+
81+
<ImageView
82+
width="100dp"
83+
height="72dp"
84+
background="#C00"
85+
src="@drawable/default_window_icon"
86+
margin="10dp"/>
87+
88+
</LinearLayout>
89+
1790
<Button
1891
width="match_parent"
1992
height="wrap_content"
@@ -37,13 +110,4 @@
37110

38111
</LinearLayout>
39112

40-
41-
<ScrollView
42-
width="400dp"
43-
height="match_parent">
44-
<EditText
45-
width="match_parent"
46-
height="wrap_content"
47-
margin="5dp"/>
48-
</ScrollView>
49-
</FrameLayout>
113+
</LinearLayout>

core/src/br/nullexcept/mux/view/AttrList.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,6 @@ public class AttrList {
5151
public static final String trackDrawable = "trackDrawable";
5252
public static final String progressDrawable = "progressDrawable";
5353
public static final String editable = "editable";
54-
public static String hintColor = "hintColor";
54+
public static final String hintColor = "hintColor";
55+
public static final String scaleType = "scaleType";
5556
}

core/src/br/nullexcept/mux/widget/ImageView.java

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,19 @@
99
import br.nullexcept.mux.view.AttrList;
1010
import br.nullexcept.mux.view.Gravity;
1111
import br.nullexcept.mux.view.View;
12+
import com.sun.org.apache.xpath.internal.operations.String;
13+
14+
import java.util.Arrays;
1215

1316
public class ImageView extends View {
17+
public static final int SCALE_FILL = 0;
18+
public static final int SCALE_FIT = 1;
19+
public static final int SCALE_CROP = 2;
20+
public static final int SCALE_WRAP = 3;
21+
1422
private Drawable image;
1523
private final Rect rect = new Rect();
24+
private int scaleType = SCALE_FIT;
1625

1726
public ImageView(Context context) {
1827
this(context, null);
@@ -22,6 +31,14 @@ public ImageView(Context context, AttributeList attrs) {
2231
super(context, attrs);
2332
attrs = initialAttributes();
2433
attrs.searchDrawable(AttrList.src, this::setImageDrawable);
34+
attrs.searchRaw(AttrList.scaleType, value -> {
35+
setScaleType(Arrays.asList("fill", "fit", "crop", "wrap").indexOf(value.toLowerCase().trim()));
36+
});
37+
}
38+
39+
public void setScaleType(int scaleType) {
40+
this.scaleType = scaleType;
41+
invalidate();
2542
}
2643

2744
public void setImageDrawable(Drawable image) {
@@ -38,13 +55,35 @@ public void onDraw(Canvas canvas) {
3855
float iw, ih;
3956

4057
if (image != null){
41-
float pw = w/image.getWidth();
42-
if (pw * image.getHeight() > h){
43-
ih = h;
44-
iw = (h/image.getHeight()) * image.getWidth();
45-
} else {
46-
iw = w;
47-
ih = pw * image.getHeight();
58+
switch (Math.max(0, Math.min(SCALE_WRAP, scaleType))) {
59+
case SCALE_FILL: {
60+
iw = w;
61+
ih = h;
62+
} break;
63+
case SCALE_FIT: {
64+
float pw = w/image.getWidth();
65+
if (pw * image.getHeight() > h){
66+
ih = h;
67+
iw = (h/image.getHeight()) * image.getWidth();
68+
} else {
69+
iw = w;
70+
ih = pw * image.getHeight();
71+
}
72+
} break;
73+
case SCALE_CROP: {
74+
float pw = w/image.getWidth();
75+
if (pw * image.getHeight() < h){
76+
ih = h;
77+
iw = (h/image.getHeight()) * image.getWidth();
78+
} else {
79+
iw = w;
80+
ih = pw * image.getHeight();
81+
}
82+
} break;
83+
default: {
84+
iw = Math.max(0, image.getWidth());
85+
ih = Math.max(0, image.getHeight());
86+
} break;
4887
}
4988
Gravity.applyGravity(getGravity(),Math.round(iw),Math.round(ih),Math.round(w), Math.round(h),rect);
5089
rect.move(getPaddingLeft(), getPaddingTop());

0 commit comments

Comments
 (0)