SeniorEditText.java 16.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
package com.studymachine.www.widget;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Build;
import android.text.InputType;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Property;
import android.view.MotionEvent;
import android.view.animation.Animation;
import android.view.animation.CycleInterpolator;
import android.view.animation.TranslateAnimation;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatEditText;

import com.blankj.utilcode.util.ConvertUtils;
import com.studymachine.www.R;


/**
 * 自定义可带清除、密码显示不显示的输入框
 */
public class SeniorEditText extends AppCompatEditText {


    /**
     * 边框样式
     */
    private static final String STYLE_RECT = "rectangle";//矩形
    private static final String STYLE_ROUND_RECT = "roundRect";//圆角矩形
    private static final String STYLE_HALF_RECT = "halfRect";//半矩形
    private static final String STYLE_ANIMATOR = "animator";//动画特效

    private static final int DEFAULT_ROUND_RADIUS = 20;//圆角矩形圆角度
    private static final int ANIMATOR_TIME = 200;//动画时间
    private static final int DEFAULT_FOCUSED_STROKE_WIDTH = 8;//获取到焦点的边框宽
    private static final int DEFAULT_UNFOCUSED_STROKE_WIDTH = 4;//未获取焦点的边框宽
    private static final int DEFAULT_STYLE_COLOR = Color.RED;//边框默认颜色

    //按钮间隔
    private int visible_res_padding = 0;
    //按钮宽度
    private int visible_res_width = 0;
    //右内边距
    private int mTextPaddingRight;

    //需要显示的drawable 资源id
    private int clear_res_id = 0;
    private int visible_res_id = 0;
    private int invisible_res_id = 0;

    //需要显示的bitmap---通过drawable 资源id来
    private Bitmap clear_bitmap;
    private Bitmap visible_bitmap;
    private Bitmap invisible_bitmap;

    private String edit_border_style = "";
    private int edit_border_color = -1;

    //出现和消失动画
    private ValueAnimator show_animator;
    private ValueAnimator dismiss_animator;
    //状态值
    private boolean clear_image_isShow = false;
    private boolean show_password_image_isShow = false;
    private boolean dismiss_password_image_isShow = false;

    private boolean isAnimatorRunning = false;
    private int mAnimatorProgress = 0;
    private ObjectAnimator mAnimator;

    //自定义属性动画
    private static final Property<SeniorEditText, Integer> BORDER_PROGRESS
            = new Property<SeniorEditText, Integer>(Integer.class, "borderProgress") {
        @Override
        public Integer get(SeniorEditText seniorEditText) {
            return seniorEditText.getBorderProgress();
        }

        @Override
        public void set(SeniorEditText seniorEditText, Integer value) {
            seniorEditText.setBorderProgress(value);
        }
    };

    private Paint mPaint;


    public SeniorEditText(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public SeniorEditText(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        //抗锯齿和位图滤波
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
        //读取xml文件中的配置
        if (attrs != null) {
            TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.SeniorEditText);
            //获取清除图标资源
            clear_res_id = array.getResourceId(R.styleable.SeniorEditText_clearDrawable, R.drawable.common_close);
            //获取可见图标资源
            visible_res_id = array.getResourceId(R.styleable.SeniorEditText_visibleDrawable, R.drawable.common_psd_show);
            //获取不可见图标资源
            invisible_res_id = array.getResourceId(R.styleable.SeniorEditText_invisibleDrawable, R.drawable.common_psd_hidden);
            //可见图标按钮默认宽
            visible_res_width = array.getDimensionPixelSize(R.styleable.SeniorEditText_visibleDrawableWidth, (int) getResources().getDimension(R.dimen.dp_15));
            //可见图标按钮默认内边距
            visible_res_padding = array.getDimensionPixelSize(R.styleable.SeniorEditText_visibleDrawableSpacing, (int) getResources().getDimension(R.dimen.dp_5));
            //输入框边框样式
            edit_border_style = array.getString(R.styleable.SeniorEditText_editFrameStyle);
            if (TextUtils.isEmpty(edit_border_style)) {
                edit_border_style = STYLE_RECT;
            }
            //输入框边框颜色
            edit_border_color = array.getColor(R.styleable.SeniorEditText_editFrameColor, DEFAULT_STYLE_COLOR);
            array.recycle();
        }
        //初始化按钮显示的Bitmap
        clear_bitmap = createBitmap(context, clear_res_id, R.drawable.common_close);
        visible_bitmap = createBitmap(context, visible_res_id, R.drawable.common_psd_show);
        invisible_bitmap = createBitmap(context, invisible_res_id, R.drawable.common_psd_hidden);
        //如果自定义,则使用自定义的值,否则使用默认值
        if (visible_res_padding == 0) {
            visible_res_padding = dp2px(context, 5);
        }
        if (visible_res_width == 0) {
            visible_res_width = dp2px(context, 24);
        }
        //给文字设置一个padding,避免文字和按钮重叠了
        mTextPaddingRight = visible_res_padding * 4 + visible_res_width * 2;
        //按钮出现和消失的动画
        show_animator = ValueAnimator.ofFloat(1f, 0f).setDuration(ANIMATOR_TIME);
        dismiss_animator = ValueAnimator.ofFloat(0f, 1f).setDuration(ANIMATOR_TIME);
        //是否是密码样式
        show_password_image_isShow = getInputType() == (InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        //设置右内边距, 防止清除按钮和文字重叠
        setPadding(getPaddingLeft(), getPaddingTop(), mTextPaddingRight, getPaddingBottom());
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

//        mPaint.setStyle(Paint.Style.STROKE);

        //使用自定义颜色。如未定义,则使用默认颜色
//        if (edit_border_color != -1) {
//            mPaint.setColor(edit_border_color);
//        } else {
//            mPaint.setColor(DEFAULT_STYLE_COLOR);
//        }

        //控件获取焦点时,加粗边框
//        if (isFocused()) {
//            mPaint.setStrokeWidth(DEFAULT_FOCUSED_STROKE_WIDTH);
//        } else {
//            mPaint.setStrokeWidth(DEFAULT_UNFOCUSED_STROKE_WIDTH);
//        }


        //绘制边框
//        drawBorder(canvas);

        //绘制清空和明文显示按钮
        drawButtons(canvas);
    }

    private void drawBorder(Canvas canvas) {
        int width = getWidth();
        int height = getHeight();

        switch (edit_border_style) {
            //矩形样式
            case STYLE_RECT:
                setBackground(null);
                canvas.drawRect(0, 0, width, height, mPaint);
                break;

            //圆角矩形样式
            case STYLE_ROUND_RECT:
                setBackground(null);
                float roundRectLineWidth = 0;
                if (isFocused()) {
                    roundRectLineWidth = DEFAULT_FOCUSED_STROKE_WIDTH / 2;
                } else {
                    roundRectLineWidth = DEFAULT_UNFOCUSED_STROKE_WIDTH / 2;
                }
                mPaint.setStrokeWidth(roundRectLineWidth);
                if (Build.VERSION.SDK_INT >= 21) {
                    canvas.drawRoundRect(
                            roundRectLineWidth / 2, roundRectLineWidth / 2, width - roundRectLineWidth / 2, height - roundRectLineWidth / 2,
                            DEFAULT_ROUND_RADIUS, DEFAULT_ROUND_RADIUS,
                            mPaint);
                } else {
                    canvas.drawRoundRect(
                            new RectF(roundRectLineWidth / 2, roundRectLineWidth / 2, width - roundRectLineWidth / 2, height - roundRectLineWidth / 2),
                            DEFAULT_ROUND_RADIUS, DEFAULT_ROUND_RADIUS,
                            mPaint);
                }
                break;

            //半矩形样式
            case STYLE_HALF_RECT:
                setBackground(null);
                canvas.drawLine(0, height, width, height, mPaint);
                canvas.drawLine(0, height / 2, 0, height, mPaint);
                canvas.drawLine(width, height / 2, width, height, mPaint);
                break;

            //动画特效样式
            case STYLE_ANIMATOR:
                setBackground(null);
                if (isAnimatorRunning) {
                    canvas.drawLine(width / 2 - mAnimatorProgress, height, width / 2 + mAnimatorProgress, height, mPaint);
                    if (mAnimatorProgress == width / 2) {
                        isAnimatorRunning = false;
                    }
                } else {
                    canvas.drawLine(0, height, width, height, mPaint);
                }
                break;
            default:
                break;
        }
    }

    private void drawButtons(Canvas canvas) {
        if (clear_image_isShow) {
            //播放按钮出现的动画
            if (dismiss_animator.isRunning()) {
                float scale = (float) dismiss_animator.getAnimatedValue();
                drawClearButton(scale, canvas);
                if (show_password_image_isShow) {
                    drawVisibleButton(scale, canvas, dismiss_password_image_isShow);
                }
                invalidate();
                //绘制静态的按钮
            } else {
                drawClearButton(1, canvas);
                if (show_password_image_isShow) {
                    drawVisibleButton(1, canvas, dismiss_password_image_isShow);
                }
            }
        } else {
            //播放按钮消失的动画
            if (show_animator.isRunning()) {
                float scale = (float) show_animator.getAnimatedValue();
                drawClearButton(scale, canvas);
                if (show_password_image_isShow) {
                    drawVisibleButton(scale, canvas, dismiss_password_image_isShow);
                }
                invalidate();
            }
        }
    }

    private void drawClearButton(float scale, Canvas canvas) {

        int right = (int) (getWidth() + getScrollX() - visible_res_padding - visible_res_width * (1f - scale) / 2f);
        int left = (int) (getWidth() + getScrollX() - visible_res_padding - visible_res_width * (scale + (1f - scale) / 2f));
        int top = (int) ((getHeight() - visible_res_width * scale) / 2);
        int bottom = (int) (top + visible_res_width * scale);
        Rect rect = new Rect(left, top, right, bottom);
        canvas.drawBitmap(clear_bitmap, null, rect, mPaint);
    }

    private void drawVisibleButton(float scale, Canvas canvas, boolean isVisible) {

        int right = (int) (getWidth() + getScrollX() - visible_res_padding * 3 - visible_res_width - visible_res_width * (1f - scale) / 2f);
        int left = (int) (getWidth() + getScrollX() - visible_res_padding * 3 - visible_res_width - visible_res_width * (scale + (1f - scale) / 2f));
        int top = (int) ((getHeight() - visible_res_width * scale) / 2);
        int bottom = (int) (top + visible_res_width * scale);
        Rect rect = new Rect(left, top, right, bottom);
        if (isVisible) {
            canvas.drawBitmap(visible_bitmap, null, rect, mPaint);
        } else {
            canvas.drawBitmap(invisible_bitmap, null, rect, mPaint);
        }

    }

    // 清除按钮出现时的动画效果
    private void startVisibleAnimator() {
        endAllAnimator();
        dismiss_animator.start();
        invalidate();
    }

    // 清除按钮消失时的动画效果
    private void startGoneAnimator() {
        endAllAnimator();
        show_animator.start();
        invalidate();
    }

    // 结束所有动画
    private void endAllAnimator() {
        show_animator.end();
        dismiss_animator.end();
    }

    @Override
    protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
        super.onFocusChanged(focused, direction, previouslyFocusedRect);

        //播放按钮出现和消失动画
        if (focused && getText().length() > 0) {
            if (!clear_image_isShow) {
                clear_image_isShow = true;
                startVisibleAnimator();
            }
        } else {
            if (clear_image_isShow) {
                clear_image_isShow = false;
                startGoneAnimator();
            }
        }

        //实现动画特效样式
        if (focused && edit_border_style.equals(STYLE_ANIMATOR)) {
            isAnimatorRunning = true;
            mAnimator = ObjectAnimator.ofInt(this, BORDER_PROGRESS, 0, getWidth() / 2);
            mAnimator.setDuration(ANIMATOR_TIME);
            mAnimator.start();
        }
    }

    protected void setBorderProgress(int borderProgress) {
        mAnimatorProgress = borderProgress;
        postInvalidate();
    }

    protected int getBorderProgress() {
        return mAnimatorProgress;
    }

    @Override
    protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
        super.onTextChanged(text, start, lengthBefore, lengthAfter);

        if (text.length() > 0 && isFocused()) {
            if (!clear_image_isShow) {
                clear_image_isShow = true;
                startVisibleAnimator();
            }
        } else {
            if (clear_image_isShow) {
                clear_image_isShow = false;
                startGoneAnimator();
            }
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_UP) {

            boolean clearTouched =
                    (getWidth() - visible_res_padding - visible_res_width < event.getX())
                            && (event.getX() < getWidth() - visible_res_padding)
                            && isFocused();
            boolean visibleTouched =
                    (getWidth() - visible_res_padding * 3 - visible_res_width * 2 < event.getX())
                            && (event.getX() < getWidth() - visible_res_padding * 3 - visible_res_width)
                            && show_password_image_isShow && isFocused();

            if (clearTouched) {
                setError(null);
                setText("");
                return true;
            } else if (visibleTouched) {
                if (dismiss_password_image_isShow) {
                    dismiss_password_image_isShow = false;
                    setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT);
                    setSelection(getText().length());
                    invalidate();
                } else {
                    dismiss_password_image_isShow = true;
                    setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                    setSelection(getText().length());
                    invalidate();
                }
                return true;
            }
        }
        return super.onTouchEvent(event);
    }

    /**
     * 开始晃动的入口
     */
    public void startShakeAnimation() {
        if (getAnimation() == null) {
            setAnimation(shakeAnimation(4));
        }
        startAnimation(getAnimation());
    }

    /**
     * 晃动动画
     *
     * @param counts 0.5秒钟晃动多少下
     * @return
     */
    private Animation shakeAnimation(int counts) {
        Animation translateAnimation = new TranslateAnimation(0, 10, 0, 0);
        translateAnimation.setInterpolator(new CycleInterpolator(counts));
        translateAnimation.setDuration(500);
        return translateAnimation;
    }


    private Bitmap createBitmap(Context context, int resId, int defResId) {
        if (resId != 0) {
            return BitmapFactory.decodeResource(context.getResources(), resId);
        } else {
            return BitmapFactory.decodeResource(context.getResources(), defResId);
        }
    }

    public int dp2px(Context context, float dpValue) {

        return ConvertUtils.dp2px(dpValue);

//        float scale = context.getResources().getDisplayMetrics().density;
//        return (int) (dpValue * scale + 0.5f);
//        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,dpValue,context.getResources().getDisplayMetrics());
    }
}