转载请注明出处:王亟亟的大牛之路
运行效果
如何使用?
在布局XML里引用即可,如下:
<com.mingle.BouncyEditText
android:layout_width="fill_parent"
android:hint="账号"
android:text="255"
android:layout_height="wrap_content">
</com.mingle.BouncyEditText>
跟别的自定义控件一样操作。
只需要在项目包里复制内贴BouncyEditText这个类和对应的一些资源文件即可,也可以Library相应的LIB包。
我们来读下核心类:
public class BouncyEditText extends EditText {
private int hintColor=getResources().getColor(R.color.hint_color);
private Paint paint;
private Status status= Status.ANIMATION_NONE;
private String hintText;
private Interpolator animOutInterpolator;
private Interpolator animInInterpolator;
private float animInDuration=200.0f;
private float animOutDuration=260.0f;
private boolean isSetPadding=false;
private boolean isHasHint = false;
public BouncyEditText(Context context) {
super(context);
}
public BouncyEditText(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public BouncyEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
paint=new Paint();
//初始化一些动画效果
animOutInterpolator = new OvershootInterpolator(1.3f);
animInInterpolator = new DecelerateInterpolator();
if(getHint()!=null) {
isHasHint =true;
hintText = getHint().toString();
setHint("");
}
// if (!TextUtils.isEmpty(getText().toString())) {
// status = Status.ANIMATION_OUT;
//
// } else {
// status = Status.ANIMATION_IN;
// }
}
//版本兼容
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public BouncyEditText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init();
}
long startTime;
private String preString;
//继承textview重写的方法
@Override
protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
super.onTextChanged(text, start, lengthBefore, lengthAfter);
if(!isHasHint){
return;
}
if(TextUtils.isEmpty(preString) != TextUtils.isEmpty( getText().toString())) {
if (!TextUtils.isEmpty(getText().toString())) {
status = Status.ANIMATION_OUT;
} else {
status = Status.ANIMATION_IN;
}
preString = (String) getText().toString();
startTime = System.currentTimeMillis();
}
}
@Override
public void setText(CharSequence text, BufferType type) {
super.setText(text, type);
if(TextUtils.isEmpty(text)){
text="";
}
preString= (String) text;
}
public void setHintText(String hintText) {
this.hintText = hintText;
isHasHint =true;
this.hintText = hintText;
setHint("");
}
//具体的画一些效果啊,动作什么的,我们用就行了。
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if(!isHasHint){
return;
}
paint.set(getPaint());
paint.setAntiAlias(true);
paint.setColor(hintColor) ;
if(!isSetPadding) {
setPadding(getPaddingLeft(), getPaddingTop(), getPaddingRight() + (int) paint.measureText(hintText), getPaddingBottom());
isSetPadding=true;
}
float maxHintY = getBaseline();
switch (status){
case ANIMATION_IN:
if(System.currentTimeMillis()-startTime<animInDuration
){
float hintX=getCompoundPaddingLeft() + getScrollX()+(getWidth()-getCompoundPaddingRight()-getCompoundPaddingLeft())*(1- animInInterpolator.getInterpolation((System.currentTimeMillis()-startTime)/animInDuration));
canvas.drawText(hintText,hintX,maxHintY,paint);
postInvalidate();
}else{
float hintX = getCompoundPaddingLeft();
canvas.drawText(hintText, hintX, maxHintY, paint);
}
break;
case ANIMATION_OUT:
if(System.currentTimeMillis()-startTime<animOutDuration
){
float hintX=getCompoundPaddingLeft() + getScrollX()+(getWidth()-getCompoundPaddingRight()-getCompoundPaddingLeft())* animOutInterpolator.getInterpolation((System.currentTimeMillis()-startTime)/animOutDuration);
canvas.drawText(hintText,hintX,maxHintY,paint);
postInvalidate();
}else{
float hintX=
getScrollX()+(getWidth()-getCompoundPaddingRight());
canvas.drawText(hintText, hintX, maxHintY, paint);
}
break;
case ANIMATION_NONE:
if(getText().toString().length()==0){
float hintX = getCompoundPaddingLeft() + getScrollX();
canvas.drawText(hintText,hintX,maxHintY,paint);
}else{
float hintX=
getScrollX()+(getWidth()-getCompoundPaddingRight());
canvas.drawText(hintText,hintX,maxHintY,paint);
}
break;
}
}
public enum Status{
ANIMATION_IN,
ANIMATION_NONE,
ANIMATION_OUT
}
}
color.xml
<resources>
<color name="hint_color">#9e9e9e</color>
</resources>
当然如果是伸手党,就更简单,复制上面的一堆,然后用就行了。很简单,效果也不错。
下载地址http://yunpan.cn/cQ74xshPWkExJ 访问密码 5597
时间: 2024-10-07 22:02:48