|
4 | 4 | package org.nativescript.widgets; |
5 | 5 |
|
6 | 6 | import android.content.Context; |
| 7 | +import android.graphics.*; |
7 | 8 | import android.graphics.drawable.Drawable; |
8 | 9 |
|
9 | 10 | /** |
10 | 11 | * @author hhristov |
11 | | - * |
| 12 | + * |
12 | 13 | */ |
13 | 14 | public class ImageView extends android.widget.ImageView { |
| 15 | + private float cornerRadius = 0; |
| 16 | + private float borderWidth = 0; |
| 17 | + |
| 18 | + private Path path = new Path(); |
| 19 | + private RectF rect = new RectF(); |
| 20 | + |
| 21 | + private double scaleW = 1; |
| 22 | + private double scaleH = 1; |
| 23 | + |
| 24 | + public ImageView(Context context) { |
| 25 | + super(context); |
| 26 | + this.setScaleType(ScaleType.FIT_CENTER); |
| 27 | + } |
| 28 | + |
| 29 | + public float getCornerRadius() { |
| 30 | + return this.cornerRadius; |
| 31 | + } |
| 32 | + |
| 33 | + public void setCornerRadius(float radius) { |
| 34 | + if (radius != this.cornerRadius) { |
| 35 | + this.cornerRadius = radius; |
| 36 | + this.invalidate(); |
| 37 | + } |
| 38 | + } |
14 | 39 |
|
15 | | - private double scaleW = 1; |
16 | | - private double scaleH = 1; |
17 | | - |
18 | | - public ImageView(Context context) { |
19 | | - super(context); |
20 | | - this.setScaleType(ScaleType.FIT_CENTER); |
21 | | - } |
22 | | - |
23 | | - @Override |
24 | | - protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
25 | | - |
26 | | - int width = MeasureSpec.getSize(widthMeasureSpec); |
27 | | - int widthMode = MeasureSpec.getMode(widthMeasureSpec); |
28 | | - |
29 | | - int height = MeasureSpec.getSize(heightMeasureSpec); |
30 | | - int heightMode = MeasureSpec.getMode(heightMeasureSpec); |
| 40 | + public float getBorderWidth() { |
| 41 | + return this.borderWidth; |
| 42 | + } |
| 43 | + |
| 44 | + public void setBorderWidth(float radius) { |
| 45 | + if (radius != this.borderWidth) { |
| 46 | + this.borderWidth = radius; |
| 47 | + this.invalidate(); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 53 | + |
| 54 | + int width = MeasureSpec.getSize(widthMeasureSpec); |
| 55 | + int widthMode = MeasureSpec.getMode(widthMeasureSpec); |
| 56 | + |
| 57 | + int height = MeasureSpec.getSize(heightMeasureSpec); |
| 58 | + int heightMode = MeasureSpec.getMode(heightMeasureSpec); |
31 | 59 |
|
32 | 60 | Drawable drawable = this.getDrawable(); |
33 | 61 | int measureWidth; |
34 | 62 | int measureHeight; |
35 | 63 | if (drawable != null) { |
36 | | - measureWidth = drawable.getIntrinsicWidth(); |
37 | | - measureHeight = drawable.getIntrinsicHeight(); |
| 64 | + measureWidth = drawable.getIntrinsicWidth(); |
| 65 | + measureHeight = drawable.getIntrinsicHeight(); |
| 66 | + } else { |
| 67 | + measureWidth = 0; |
| 68 | + measureHeight = 0; |
38 | 69 | } |
39 | | - else { |
40 | | - measureWidth = 0; |
41 | | - measureHeight = 0; |
| 70 | + |
| 71 | + boolean finiteWidth = widthMode != MeasureSpec.UNSPECIFIED; |
| 72 | + boolean finiteHeight = heightMode != MeasureSpec.UNSPECIFIED; |
| 73 | + |
| 74 | + if (measureWidth != 0 && measureHeight != 0 && (finiteWidth || finiteHeight)) { |
| 75 | + this.computeScaleFactor(width, height, finiteWidth, finiteHeight, measureWidth, measureHeight); |
| 76 | + int resultW = (int) Math.floor(measureWidth * this.scaleW); |
| 77 | + int resultH = (int) Math.floor(measureHeight * this.scaleH); |
| 78 | + |
| 79 | + measureWidth = finiteWidth ? Math.min(resultW, width) : resultW; |
| 80 | + measureHeight = finiteHeight ? Math.min(resultH, height) : resultH; |
42 | 81 | } |
43 | | - |
44 | | - boolean finiteWidth = widthMode != MeasureSpec.UNSPECIFIED; |
45 | | - boolean finiteHeight = heightMode != MeasureSpec.UNSPECIFIED; |
46 | | - |
47 | | - if (measureWidth != 0 && measureHeight != 0 && (finiteWidth || finiteHeight)) { |
48 | | - this.computeScaleFactor(width, height, finiteWidth, finiteHeight, measureWidth, measureHeight); |
49 | | - int resultW = (int) Math.floor(measureWidth * this.scaleW); |
50 | | - int resultH = (int) Math.floor(measureHeight * this.scaleH); |
51 | | - |
52 | | - measureWidth = finiteWidth ? Math.min(resultW, width) : resultW; |
53 | | - measureHeight = finiteHeight ? Math.min(resultH, height) : resultH; |
54 | | - } |
55 | | - |
56 | | - measureWidth += this.getPaddingLeft() + this.getPaddingRight(); |
57 | | - measureHeight += this.getPaddingTop() + this.getPaddingBottom(); |
58 | | - |
59 | | - measureWidth = Math.max(measureWidth, getSuggestedMinimumWidth()); |
60 | | - measureHeight = Math.max(measureHeight, getSuggestedMinimumHeight()); |
61 | | - |
62 | | - if (CommonLayoutParams.debuggable > 0) { |
63 | | - StringBuilder sb = CommonLayoutParams.getStringBuilder(); |
64 | | - sb.append("ImageView onMeasure: "); |
65 | | - sb.append(MeasureSpec.toString(widthMeasureSpec)); |
66 | | - sb.append(", "); |
67 | | - sb.append(MeasureSpec.toString(heightMeasureSpec)); |
68 | | - sb.append(", stretch: "); |
69 | | - sb.append(this.getScaleType()); |
70 | | - sb.append(", measureWidth: "); |
71 | | - sb.append(measureWidth); |
72 | | - sb.append(", measureHeight: "); |
73 | | - sb.append(measureHeight); |
74 | | - |
75 | | - CommonLayoutParams.log(CommonLayoutParams.tag, sb.toString()); |
76 | | - } |
77 | | - |
| 82 | + |
| 83 | + measureWidth += this.getPaddingLeft() + this.getPaddingRight(); |
| 84 | + measureHeight += this.getPaddingTop() + this.getPaddingBottom(); |
| 85 | + |
| 86 | + measureWidth = Math.max(measureWidth, getSuggestedMinimumWidth()); |
| 87 | + measureHeight = Math.max(measureHeight, getSuggestedMinimumHeight()); |
| 88 | + |
| 89 | + if (CommonLayoutParams.debuggable > 0) { |
| 90 | + StringBuilder sb = CommonLayoutParams.getStringBuilder(); |
| 91 | + sb.append("ImageView onMeasure: "); |
| 92 | + sb.append(MeasureSpec.toString(widthMeasureSpec)); |
| 93 | + sb.append(", "); |
| 94 | + sb.append(MeasureSpec.toString(heightMeasureSpec)); |
| 95 | + sb.append(", stretch: "); |
| 96 | + sb.append(this.getScaleType()); |
| 97 | + sb.append(", measureWidth: "); |
| 98 | + sb.append(measureWidth); |
| 99 | + sb.append(", measureHeight: "); |
| 100 | + sb.append(measureHeight); |
| 101 | + |
| 102 | + CommonLayoutParams.log(CommonLayoutParams.tag, sb.toString()); |
| 103 | + } |
| 104 | + |
78 | 105 | int widthSizeAndState = resolveSizeAndState(measureWidth, widthMeasureSpec, 0); |
79 | 106 | int heightSizeAndState = resolveSizeAndState(measureHeight, heightMeasureSpec, 0); |
80 | 107 |
|
81 | 108 | this.setMeasuredDimension(widthSizeAndState, heightSizeAndState); |
82 | | - } |
83 | | - |
84 | | - private void computeScaleFactor( |
85 | | - int measureWidth, |
86 | | - int measureHeight, |
87 | | - boolean widthIsFinite, |
88 | | - boolean heightIsFinite, |
89 | | - double nativeWidth, |
90 | | - double nativeHeight) { |
91 | | - |
92 | | - this.scaleW = 1; |
93 | | - this.scaleH = 1; |
94 | | - |
95 | | - ScaleType scale = this.getScaleType(); |
| 109 | + } |
| 110 | + |
| 111 | + private void computeScaleFactor(int measureWidth, int measureHeight, boolean widthIsFinite, boolean heightIsFinite, double nativeWidth, double nativeHeight) { |
| 112 | + |
| 113 | + this.scaleW = 1; |
| 114 | + this.scaleH = 1; |
| 115 | + |
| 116 | + ScaleType scale = this.getScaleType(); |
96 | 117 | if ((scale == ScaleType.CENTER_CROP || scale == ScaleType.FIT_CENTER || scale == ScaleType.FIT_XY) && |
97 | | - (widthIsFinite || heightIsFinite)) { |
| 118 | + (widthIsFinite || heightIsFinite)) { |
98 | 119 |
|
99 | | - this.scaleW = (nativeWidth > 0) ? measureWidth / nativeWidth : 0d; |
100 | | - this.scaleH = (nativeHeight > 0) ? measureHeight / nativeHeight : 0d; |
| 120 | + this.scaleW = (nativeWidth > 0) ? measureWidth / nativeWidth : 0d; |
| 121 | + this.scaleH = (nativeHeight > 0) ? measureHeight / nativeHeight : 0d; |
101 | 122 |
|
102 | 123 | if (!widthIsFinite) { |
103 | | - this.scaleW = scaleH; |
104 | | - } |
105 | | - else if (!heightIsFinite) { |
106 | | - this.scaleH = scaleW; |
107 | | - } |
108 | | - else { |
| 124 | + this.scaleW = scaleH; |
| 125 | + } else if (!heightIsFinite) { |
| 126 | + this.scaleH = scaleW; |
| 127 | + } else { |
109 | 128 | // No infinite dimensions. |
110 | 129 | switch (scale) { |
111 | | - case FIT_CENTER: |
112 | | - this.scaleH = this.scaleW < this.scaleH ? this.scaleW : this.scaleH; |
113 | | - this.scaleW = this.scaleH; |
114 | | - break; |
115 | | - case CENTER_CROP: |
116 | | - this.scaleH = this.scaleW > this.scaleH ? this.scaleW : this.scaleH; |
117 | | - this.scaleW = this.scaleH; |
118 | | - break; |
119 | | - default: |
120 | | - break; |
| 130 | + case FIT_CENTER: |
| 131 | + this.scaleH = this.scaleW < this.scaleH ? this.scaleW : this.scaleH; |
| 132 | + this.scaleW = this.scaleH; |
| 133 | + break; |
| 134 | + case CENTER_CROP: |
| 135 | + this.scaleH = this.scaleW > this.scaleH ? this.scaleW : this.scaleH; |
| 136 | + this.scaleW = this.scaleH; |
| 137 | + break; |
| 138 | + default: |
| 139 | + break; |
121 | 140 | } |
122 | 141 | } |
123 | 142 | } |
124 | 143 | } |
| 144 | + |
| 145 | + @Override |
| 146 | + protected void onDraw(Canvas canvas) { |
| 147 | + // floor the border width to avoid gaps between the border and the image |
| 148 | + float roundedBorderWidth = (float) Math.floor(this.borderWidth); |
| 149 | + float innerRadius = Math.max(0, this.cornerRadius - roundedBorderWidth); |
| 150 | + |
| 151 | + // The border width is included in the padding so there is no need for |
| 152 | + // clip if there is no inner border radius. |
| 153 | + if (innerRadius != 0) { |
| 154 | + this.rect.set( |
| 155 | + roundedBorderWidth, |
| 156 | + roundedBorderWidth, |
| 157 | + this.getWidth() - roundedBorderWidth, |
| 158 | + this.getHeight() - roundedBorderWidth); |
| 159 | + |
| 160 | + this.path.reset(); |
| 161 | + this.path.addRoundRect(rect, innerRadius, innerRadius, android.graphics.Path.Direction.CW); |
| 162 | + |
| 163 | + canvas.clipPath(this.path); |
| 164 | + } |
| 165 | + |
| 166 | + super.onDraw(canvas); |
| 167 | + } |
125 | 168 | } |
0 commit comments