[Android]实现带显示密码按钮的EditText(无内存泄露)

原理:

通过自定义View绘制显示密码按钮,当点击密码按钮的时候调用setInputType来更改属性。

解决方案:

就直接上代码了 

package com.finals.view;

import com.example.test.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;

public class PassEditText extends EditText {

	int mThumbWidth;
	int mThumbHeight;

	int offsetX;
	int offsetY;

	boolean isShowPass = false;

	Drawable mThumbDrawable;

	Bitmap mThumbDefault;

	public PassEditText(Context context, AttributeSet attrs) {
		super(context, attrs);
		InitDrawable(context, attrs);
	}

	public PassEditText(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		InitDrawable(context, attrs);
	}

	private void InitDrawable(Context context, AttributeSet attrs) {
		float density = context.getResources().getDisplayMetrics().density;

		TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.windows);
		mThumbDrawable = mTypedArray.getDrawable(R.styleable.windows_thumb);
		mThumbWidth = (int) mTypedArray.getDimension(R.styleable.windows_thumb_width, 20 * density);
		mThumbHeight = (int) mTypedArray.getDimension(R.styleable.windows_thumb_height, 20 * density);

		if (mThumbDrawable == null) {
			mThumbDefault = getThumbDefault();
		} else {
			mThumbDrawable.setBounds(0, 0, (int) mThumbWidth, (int) mThumbHeight);
		}
		mTypedArray.recycle();
		if (getInputType() != (EditorInfo.TYPE_TEXT_VARIATION_PASSWORD | EditorInfo.TYPE_CLASS_TEXT)) {
			isShowPass = true;
		}
	}

	@Override
	public int getCompoundPaddingRight() {
		return super.getCompoundPaddingRight() + mThumbWidth;
	}

	@Override
	public void draw(Canvas canvas) {
		super.draw(canvas);
		drawThumb(canvas);
	}

	void drawThumb(Canvas canvas) {
		offsetX = getWidth() - mThumbWidth - getPaddingRight();
		offsetY = (getHeight() - mThumbHeight) / 2;
		if (mThumbDrawable != null) {
			canvas.save();
			canvas.translate(getScrollX() + offsetX, getScrollY() + offsetY);
			mThumbDrawable.draw(canvas);
			canvas.restore();
		} else if (mThumbDefault != null) {
			canvas.drawBitmap(mThumbDefault, getScrollX() + offsetX, getScrollY() + offsetY, null);
		}
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		int action = event.getAction();
		switch (action) {
		case MotionEvent.ACTION_DOWN:
			checkThumb(event);
			break;
		default:
			break;
		}
		return super.onTouchEvent(event);
	}

	void checkThumb(MotionEvent event) {
		float x = event.getX();
		float y = event.getY();
		if (x > offsetX && x < offsetX + mThumbWidth && y > offsetY && y < offsetY + mThumbHeight) {
			if (!isShowPass) {
				isShowPass = true;
				this.setInputType(EditorInfo.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
			} else {
				isShowPass = false;
				this.setInputType(EditorInfo.TYPE_TEXT_VARIATION_PASSWORD | EditorInfo.TYPE_CLASS_TEXT);
			}
		}
	}

	@Override
	protected void onAttachedToWindow() {
		if (mThumbDrawable == null) {
			mThumbDefault = getThumbDefault();
		}
		super.onAttachedToWindow();
	}

	@Override
	protected void onDetachedFromWindow() {
		if (mThumbDefault != null) {
			mThumbDefault.recycle();
			mThumbDefault = null;
		}
		super.onDetachedFromWindow();
	}

	/**
	 * 创建默认图片
	 * 
	 * @return
	 */
	private Bitmap getThumbDefault() {
		float density = getContext().getResources().getDisplayMetrics().density;
		int stokenWidth = (int) (3 * density);

		Paint mPaint = new Paint();
		mPaint.setColor(Color.BLACK);
		mPaint.setStyle(Style.FILL);
		mPaint.setAntiAlias(true);
		Bitmap mBitmap = Bitmap.createBitmap(mThumbWidth, mThumbHeight, Config.ARGB_4444);
		Canvas mCanvas = new Canvas(mBitmap);
		mCanvas.drawCircle(mThumbWidth / 2, mThumbHeight / 2, mThumbWidth * 0.30F - stokenWidth, mPaint);

		mPaint.setStyle(Style.STROKE);

		mPaint.setStrokeWidth(stokenWidth);

		RectF oval = new RectF(stokenWidth, stokenWidth, mThumbWidth - stokenWidth, mThumbHeight - stokenWidth);
		mCanvas.drawArc(oval, -175, 170, false, mPaint);
		return mBitmap;
	}

	public boolean isShowPassword() {
		return isShowPass;
	}

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值