您的位置:首页 > 移动开发

Android 为app自定义一个输入密码键盘

2015-04-22 11:41 603 查看
看到越来越多的app都使用自家的安全键盘,输入英文、数字、符号的字符。

public class KeyBoard extends View {

private OnKeyBoardClickListener mListener;
private RectF[] keyRectFs;
private Paint p;

private float width;
private float height;

private int white;
private int black;
private int gray;

//按下的按键,C为10,0为11,X为12.-1为没按下
private int down;
<span style="white-space:pre">	</span>//设置字体大小,为了保持在各个分辨率不走样,需要动态计算。
private int textSize;

public KeyBoard(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}

public KeyBoard(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}

public KeyBoard(Context context) {
super(context);
init();
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
width = w;
height = h;
textSize = (int) (h/8);
for(int i=0;i<12;i++){
keyRectFs[i] = new RectF((i%3)*width/3, (i/3)*height/4, ((i%3)+1)*width/3, ((i+3)/3)*height/4);
}
}

private void init(){
p = new Paint();
p.setAntiAlias(true);
down = -1;
keyRectFs = new RectF[12];
white = Color.parseColor("#ffffff");
gray = Color.parseColor("#939393");
black = Color.parseColor("#636363");
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(white);
for(int i=1;i<=12;i++){
if(i == down + 1){
p.setStyle(Paint.Style.FILL);
p.setColor(gray);
canvas.drawRect(keyRectFs[i-1], p);
}
else{
p.setStyle(Paint.Style.STROKE);
p.setColor(gray);
canvas.drawRect(keyRectFs[i-1], p);
}
p.setColor(black);
if(i<=9){
drawText(canvas, i+"", i-1);
}
else if(i == 10){
drawText(canvas, "C", i-1);
}
else if(i == 11){
drawText(canvas, "0", i-1);
}
else if(i == 12){
drawText(canvas, "X", i-1);
}
}
}

private void drawText(Canvas canvas, String s,int size){
FontMetricsInt fontMetrics;
int baseline;
p.setTextSize(textSize);
p.setTextAlign(Paint.Align.CENTER);
fontMetrics = p.getFontMetricsInt();
baseline = (int) (keyRectFs[size].top + (keyRectFs[size].bottom - keyRectFs[size].top -
fontMetrics.bottom + fontMetrics.top) / 2 - fontMetrics.top);
canvas.drawText(s, keyRectFs[size].centerX(), baseline, p);
}
//判断所点击的按键位置
private int getTouchRect(float x, float y){
int i,j;
if(x > keyRectFs[1].right){
j = 2;
}
else if(x < keyRectFs[1].left){
j = 0;
}
else{
j = 1;
}

if(y > keyRectFs[7].bottom){
i = 3;
}
else if(y < keyRectFs[4].top){
i = 0;
}
else if(y > keyRectFs[4].bottom){
i = 2;
}
else{
i = 1;
}
return 3*i + j;
}

@Override
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
down = getTouchRect(event.getX(), event.getY());
<span style="white-space:pre">			</span>//这里重绘显示点击效果。
invalidate();
}
else if(event.getAction() == MotionEvent.ACTION_UP){
if(down == getTouchRect(event.getX(), event.getY())){
if(mListener != null){
String s;
if(down >= 0 && down <= 8){
s = (down+1) + "";
}
else if(down == 9){
s = "C";
}
else if(down == 10){
s = 0 + "";
}
else{
s = "X";
}
mListener.onKeyBoardClick(this, s);
}
}
down = -1;
invalidate();
}

return true;
}

public void setOnKeyBoardListener(OnKeyBoardClickListener keyBoardClickListener){
mListener = keyBoardClickListener;
}

public interface OnKeyBoardClickListener{
void onKeyBoardClick(View view,String key);
}
}




看上去是奇丑无比,需要更漂亮的样式只需要在onDraw里使用canvas画上你喜爱的东西。

另外涉及到性能,可以在重绘的时候只重绘一部分,而不需要整个view重绘。

另外,附上
http://blog.csdn.net/zhufuing/article/details/18964725
以作参考。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐