Skip to content

Commit 8b4d0dd

Browse files
committed
添加自定义关闭按钮大小的方法
1 parent 4115e2e commit 8b4d0dd

File tree

4 files changed

+30
-54
lines changed

4 files changed

+30
-54
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151

5252
```
5353
dependencies {
54-
compile 'com.github.MZCretin:ClearEditTextProject:1.1.0'
54+
compile 'com.github.MZCretin:ClearEditTextProject:1.2.0'
5555
}
5656
```
5757

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,10 @@
5151
android:layout_height="40dp"
5252
android:layout_marginLeft="20dp"
5353
android:layout_marginRight="20dp"
54-
android:layout_marginTop="20dp"
54+
android:layout_marginTop="10dp"
5555
android:background="#ffffff"
56+
app:drawableWidth="20dp"
57+
android:textSize="14sp"
5658
app:scaleSize="0.5"/>
5759

5860
</LinearLayout>

clearedittext/src/main/java/com/cretin/www/clearedittext/view/ClearEditText.java

Lines changed: 25 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public class ClearEditText extends EditText {
3636
private boolean showClose;
3737
private float scale;
3838
private float padding;
39+
private float mDrawWidth;
3940

4041
public ClearEditText(Context context) {
4142
super(context);
@@ -61,6 +62,7 @@ private void init(Context context, AttributeSet attrs) {
6162
//获得属性值
6263
clearIcon = a.getResourceId(R.styleable.ClearEditText_clearIcon, 0);
6364
scale = a.getFloat(R.styleable.ClearEditText_scaleSize, 0);
65+
mDrawWidth = a.getDimension(R.styleable.ClearEditText_drawableWidth, 0.0f);
6466
} finally { //回收这个对象
6567
a.recycle();
6668
}
@@ -129,55 +131,6 @@ protected void onDraw(Canvas canvas) {
129131
}
130132
}
131133

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-
181134
private boolean hasScale;
182135

183136
private void deal() {
@@ -186,9 +139,18 @@ private void deal() {
186139
int height = mClearBitmap.getHeight();
187140
Log.e("HHHHH", "width height " + width + " " + height);
188141
// 设置想要的大小
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+
}
192154
// 计算缩放比例
193155
float scaleWidth = (( float ) mBitWidth) / width;
194156
float scaleHeight = (( float ) mBitHeight) / height;
@@ -200,5 +162,16 @@ private void deal() {
200162
hasScale = true;
201163
}
202164
}
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+
}
203176
}
204177

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
<declare-styleable name="ClearEditText">
44
<attr name="clearIcon" format="reference"/>
55
<attr name="scaleSize" format="float"/>
6+
<attr name="drawableWidth" format="dimension"/>
67
</declare-styleable>
78
</resources>

0 commit comments

Comments
 (0)