Skip to content

Commit 979cee3

Browse files
committed
Add scale type of image view.
1 parent 9ee4813 commit 979cee3

File tree

3 files changed

+63
-24
lines changed

3 files changed

+63
-24
lines changed

library/src/main/java/pl/sigmapoint/customview/CustomButton.java

Lines changed: 53 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424
*/
2525
public class CustomButton extends LinearLayout implements View.OnClickListener {
2626

27-
public static int LEFT = 0;
28-
public static int TOP = 1;
29-
public static int RIGHT = 2;
30-
public static int BOTTOM = 3;
27+
public static final int LEFT = 0;
28+
public static final int TOP = 1;
29+
public static final int RIGHT = 2;
30+
public static final int BOTTOM = 3;
3131

3232
protected TextView textView; //text container
3333
protected LinearLayout container; //all content container
@@ -52,12 +52,12 @@ public class CustomButton extends LinearLayout implements View.OnClickListener {
5252
private int drawablePosition;
5353
private int imagePadding;
5454
private int[] imagePaddingArray;
55-
55+
private int imageScaleTypeAttr;
5656

5757
private int shapeType; // converted shape type from shapeTypeAttr
58+
private ImageView.ScaleType imageScaleType;
5859

5960
//TODO stosunek podziału image / text
60-
//TODO scaletype of image
6161

6262
public CustomButton(Context context, AttributeSet attrs) {
6363
super(context, attrs);
@@ -99,13 +99,13 @@ public CustomButton(Context context, AttributeSet attrs) {
9999
drawableNormal = attributes.getDrawable(R.styleable.CustomButton_cb_image_normal);
100100
drawablePressed = attributes.getDrawable(R.styleable.CustomButton_cb_image_pressed);
101101
drawableDisabled = attributes.getDrawable(R.styleable.CustomButton_cb_image_disabled);
102+
imageScaleTypeAttr = attributes.getInteger(R.styleable.CustomButton_cb_image_scale_type, 0);
102103
imagePadding = (int) attributes.getDimension(R.styleable.CustomButton_cb_image_padding, 0);
103104
imagePaddingArray[LEFT] = (int) attributes.getDimension(R.styleable.CustomButton_cb_image_padding_left, imagePadding);
104105
imagePaddingArray[TOP] = (int) attributes.getDimension(R.styleable.CustomButton_cb_image_padding_top, imagePadding);
105106
imagePaddingArray[RIGHT] = (int) attributes.getDimension(R.styleable.CustomButton_cb_image_padding_right, imagePadding);
106107
imagePaddingArray[BOTTOM] = (int) attributes.getDimension(R.styleable.CustomButton_cb_image_padding_bottom, imagePadding);
107108

108-
109109
if (backgroundColorState != null) { // if backgroundColor state is not null the set color from color state list to specific colors
110110
backgroundColorStateListToIntegers(backgroundColorState);
111111
}
@@ -114,6 +114,30 @@ public CustomButton(Context context, AttributeSet attrs) {
114114
frameColorStateListToIntegers(frameColorState);
115115
}
116116

117+
switch (imageScaleTypeAttr) {
118+
case 0:
119+
imageScaleType = ImageView.ScaleType.CENTER;
120+
break;
121+
case 1:
122+
imageScaleType = ImageView.ScaleType.CENTER_INSIDE;
123+
break;
124+
case 2:
125+
imageScaleType = ImageView.ScaleType.CENTER_CROP;
126+
break;
127+
case 3:
128+
imageScaleType = ImageView.ScaleType.FIT_CENTER;
129+
break;
130+
case 4:
131+
imageScaleType = ImageView.ScaleType.FIT_START;
132+
break;
133+
case 5:
134+
imageScaleType = ImageView.ScaleType.FIT_END;
135+
break;
136+
case 6:
137+
imageScaleType = ImageView.ScaleType.FIT_XY;
138+
break;
139+
}
140+
117141
setContent(context);
118142

119143
shapeType = (shapeTypeAttr == 0) ? GradientDrawable.RECTANGLE : GradientDrawable.OVAL; // convert xml attributes value to shape type constant
@@ -163,25 +187,22 @@ private void setContent(Context context) {
163187

164188
imageContainer = new ImageView(context);
165189
imageContainer.setPadding(imagePaddingArray[LEFT], imagePaddingArray[TOP], imagePaddingArray[RIGHT], imagePaddingArray[BOTTOM]);
190+
imageContainer.setScaleType(imageScaleType);
191+
imageContainer.setLayoutParams(layoutParams);
166192
if (drawable != null) {
167193
imageContainer.setImageDrawable(drawable);
168194
} else {
169195
imageContainer.setImageDrawable(listDrawable);
170196
}
171197

172-
imageContainer.setLayoutParams(layoutParams);
173-
imageContainer.setScaleType(ImageView.ScaleType.FIT_CENTER);
174-
175198
switch (drawablePosition) {
176-
case 0:
199+
case LEFT:
177200
container.setOrientation(HORIZONTAL);
178201
container.addView(imageContainer);
179202
break;
180-
case 1:
203+
case TOP:
181204
container.addView(imageContainer);
182205
break;
183-
case 3:
184-
break;
185206
}
186207
if (text != null) {
187208
textView.setLayoutParams(layoutParams);
@@ -190,10 +211,10 @@ private void setContent(Context context) {
190211
}
191212

192213
switch (drawablePosition) {
193-
case 2:
214+
case RIGHT:
194215
container.addView(imageContainer);
195216
break;
196-
case 3:
217+
case BOTTOM:
197218
container.setOrientation(HORIZONTAL);
198219
container.addView(imageContainer);
199220
break;
@@ -418,33 +439,40 @@ public void setTextPadding(int[] padding) {
418439
* @param drawableNormal state normal
419440
* @param drawablePressed state pressed
420441
* @param drawableDisabled state disabled
442+
* @param scaleType all without MATRIX
421443
* @param padding 4 elements array {CustomButton.LEFT, CustomButton.TOP, CustomButton.RIGHT, CustomButton.BOTTOM}
422444
*/
423-
public void setImage(int position, Drawable drawableNormal, Drawable drawablePressed, Drawable drawableDisabled, int[] padding) {
445+
public void setImage(int position, Drawable drawableNormal, Drawable drawablePressed, Drawable drawableDisabled, ImageView.ScaleType scaleType, int[] padding) {
424446
this.drawableDisabled = drawableDisabled;
425447
this.drawablePressed = drawablePressed;
426448
this.drawableNormal = drawableNormal;
427449
this.drawablePosition = position;
450+
this.imageScaleType = scaleType;
428451

429452
if (padding != null)
430453
imageContainer.setPadding(padding[LEFT], padding[TOP], padding[RIGHT], padding[BOTTOM]);
454+
if (scaleType != null)
455+
imageContainer.setScaleType(scaleType);
431456

432457
setContent(getContext());
433458
}
434459

435460
/**
436461
* Set image to button
437462
*
438-
* @param position CustomButton.LEFT, CustomButton.TOP, CustomButton.RIGHT, CustomButton.BOTTOM
439-
* @param drawable image resource
440-
* @param padding 4 elements array {CustomButton.LEFT, CustomButton.TOP, CustomButton.RIGHT, CustomButton.BOTTOM}
463+
* @param position CustomButton.LEFT, CustomButton.TOP, CustomButton.RIGHT, CustomButton.BOTTOM
464+
* @param drawable image resource
465+
* @param scaleType all without MATRIX
466+
* @param padding 4 elements array {CustomButton.LEFT, CustomButton.TOP, CustomButton.RIGHT, CustomButton.BOTTOM}
441467
*/
442-
public void setImage(int position, Drawable drawable, int[] padding) {
468+
public void setImage(int position, Drawable drawable, ImageView.ScaleType scaleType, int[] padding) {
443469
this.drawable = drawable;
444470
this.drawablePosition = position;
445471

446472
if (padding != null)
447473
imageContainer.setPadding(padding[LEFT], padding[TOP], padding[RIGHT], padding[BOTTOM]);
474+
if (scaleType != null)
475+
imageContainer.setScaleType(scaleType);
448476

449477
setContent(getContext());
450478
}
@@ -603,4 +631,8 @@ public int getImagePadding() {
603631
public int[] getImagePaddingArray() {
604632
return imagePaddingArray;
605633
}
634+
635+
public ImageView.ScaleType getImageScaleType() {
636+
return imageScaleType;
637+
}
606638
}

library/src/main/res/anim/elevation_button_custom.xml renamed to library/src/main/res/anim-v21/elevation_button_custom.xml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
android:valueType="floatType" />
1717
</set>
1818
</item>
19-
<!-- base state -->
20-
<item android:state_enabled="true">
19+
<item>
2120
<set>
2221
<objectAnimator
2322
android:duration="75"
@@ -32,5 +31,4 @@
3231
android:valueType="floatType" />
3332
</set>
3433
</item>
35-
...
3634
</selector>

library/src/main/res/values/attrs.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,15 @@
4747
<attr name="cb_image_padding_top" format="dimension" />
4848
<attr name="cb_image_padding_right" format="dimension" />
4949
<attr name="cb_image_padding_bottom" format="dimension" />
50+
<attr name="cb_image_scale_type" format="integer">
51+
<enum name="center" value="0" />
52+
<enum name="center_inside" value="1" />
53+
<enum name="center_crop" value="2" />
54+
<enum name="fit_center" value="3" />
55+
<enum name="fit_start" value="4" />
56+
<enum name="fit_end" value="5" />
57+
<enum name="fit_xy" value="6" />
58+
</attr>
5059

5160
</declare-styleable>
5261
</resources>

0 commit comments

Comments
 (0)