首页 > 移动平台 > 详细

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

时间:2015-04-25 18:23:44      阅读:320      评论:0      收藏:0      [点我收藏+]

原理:

通过自定义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;
	}

}



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

原文:http://blog.csdn.net/windowsxp2014/article/details/45271517

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!