@@ -36,6 +36,7 @@ public class ClearEditText extends EditText {
36
36
private boolean showClose ;
37
37
private float scale ;
38
38
private float padding ;
39
+ private float mDrawWidth ;
39
40
40
41
public ClearEditText (Context context ) {
41
42
super (context );
@@ -61,6 +62,7 @@ private void init(Context context, AttributeSet attrs) {
61
62
//获得属性值
62
63
clearIcon = a .getResourceId (R .styleable .ClearEditText_clearIcon , 0 );
63
64
scale = a .getFloat (R .styleable .ClearEditText_scaleSize , 0 );
65
+ mDrawWidth = a .getDimension (R .styleable .ClearEditText_drawableWidth , 0.0f );
64
66
} finally { //回收这个对象
65
67
a .recycle ();
66
68
}
@@ -129,55 +131,6 @@ protected void onDraw(Canvas canvas) {
129
131
}
130
132
}
131
133
132
- @ Override
133
- protected void onMeasure (int widthMeasureSpec , int heightMeasureSpec ) {
134
- int desiredWidth = 100 ;
135
- int desiredHeight = 100 ;
136
-
137
- int widthMode = MeasureSpec .getMode (widthMeasureSpec );
138
- int widthSize = MeasureSpec .getSize (widthMeasureSpec );
139
- int heightMode = MeasureSpec .getMode (heightMeasureSpec );
140
- int heightSize = MeasureSpec .getSize (heightMeasureSpec );
141
-
142
- int width ;
143
- int height ;
144
-
145
- //Measure Width
146
- if ( widthMode == MeasureSpec .EXACTLY ) {
147
- //Must be this size
148
- width = widthSize ;
149
- } else if ( widthMode == MeasureSpec .AT_MOST ) {
150
- //Can't be bigger than...
151
- width = Math .min (desiredWidth , widthSize );
152
- } else {
153
- //Be whatever you want
154
- width = desiredWidth ;
155
- }
156
-
157
- //Measure Height
158
- if ( heightMode == MeasureSpec .EXACTLY ) {
159
- //Must be this size
160
- height = heightSize ;
161
- } else if ( heightMode == MeasureSpec .AT_MOST ) {
162
- //Can't be bigger than...
163
- height = Math .min (desiredHeight , heightSize );
164
- } else {
165
- //Be whatever you want
166
- height = desiredHeight ;
167
- }
168
- mWidth = width ;
169
- mHeight = height ;
170
-
171
- Log .e ("HHHHH" , "mWidth mHeight " + mWidth + " " + mHeight );
172
- //MUST CALL THIS
173
-
174
- //计算偏移量
175
- padding = (( float ) mHeight ) * (1 - scale ) / 2 ;
176
- //第一次进来的时候对图片进行处理
177
- deal ();
178
- setMeasuredDimension (width , height );
179
- }
180
-
181
134
private boolean hasScale ;
182
135
183
136
private void deal () {
@@ -186,9 +139,18 @@ private void deal() {
186
139
int height = mClearBitmap .getHeight ();
187
140
Log .e ("HHHHH" , "width height " + width + " " + height );
188
141
// 设置想要的大小
189
- mBitWidth = ( int ) (mHeight - 2 * padding );
190
- mBitHeight = mBitWidth ;
191
- Log .e ("HHHHH" , "mBitWidth mBitHeight " + mBitWidth + " " + mBitHeight );
142
+ if ( mDrawWidth == 0 ) {
143
+ padding = (( float ) mHeight ) * (1 - scale ) / 2 ;
144
+ mBitWidth = ( int ) (mHeight - 2 * padding );
145
+ mBitHeight = mBitWidth ;
146
+ } else {
147
+ if ( mDrawWidth > mHeight ) {
148
+ mDrawWidth = mHeight ;
149
+ }
150
+ padding = (( float ) (mHeight - mDrawWidth )) / 2 ;
151
+ mBitWidth = ( int ) mDrawWidth ;
152
+ mBitHeight = ( int ) mDrawWidth ;
153
+ }
192
154
// 计算缩放比例
193
155
float scaleWidth = (( float ) mBitWidth ) / width ;
194
156
float scaleHeight = (( float ) mBitHeight ) / height ;
@@ -200,5 +162,16 @@ private void deal() {
200
162
hasScale = true ;
201
163
}
202
164
}
165
+
166
+ @ Override
167
+ protected void onSizeChanged (int w , int h , int oldw , int oldh ) {
168
+ super .onSizeChanged (w , h , oldw , oldh );
169
+ mWidth = w ;
170
+ mHeight = h ;
171
+
172
+ //第一次进来的时候对图片进行处理
173
+ deal ();
174
+
175
+ }
203
176
}
204
177
0 commit comments