其他
高仿闲鱼键自定义数字键盘UI特效
仿造android端闲鱼发布选择价格数字键盘,自定义数字键盘的学习与应用
博客地址:
http://www.jianshu.com/p/be68facfc4b8
效果图:
public class MyKeyBoardView extends KeyboardView {
private Context mContext;
private Keyboard mKeyBoard;
public MyKeyBoardView(Context context, AttributeSet attrs) {
super(context, attrs);
this.mContext = context;
}
public MyKeyBoardView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); this.mContext = context;
}
/**
* 重新画一些按键
*/
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
mKeyBoard = this.getKeyboard();
List<Key> keys = null;
if (mKeyBoard != null) {
keys = mKeyBoard.getKeys();
}
if (keys != null) {
for (Key key : keys) { // 数字键盘的处理
if (key.codes[0] == -4) {
drawKeyBackground(R.drawable.bg_keyboardview_yes, canvas, key);
drawText(canvas, key);
}
}
}
}
private void drawKeyBackground(int drawableId, Canvas canvas, Key key) {
Drawable npd = mContext.getResources().getDrawable(
drawableId);
int[] drawableState = key.getCurrentDrawableState();
if (key.codes[0] != 0) {
npd.setState(drawableState);
}
npd.setBounds(key.x, key.y, key.x + key.width, key.y + key.height);
npd.draw(canvas);
}
private void drawText(Canvas canvas, Key key) {
Rect bounds = new Rect();
Paint paint = new Paint();
paint.setTextAlign(Paint.Align.CENTER);
paint.setAntiAlias(true);
paint.setColor(Color.WHITE);
if (key.label != null) {
String label = key.label.toString();
Field field;
if (label.length() > 1 && key.codes.length < 2) { int labelTextSize = 0; try {
field = KeyboardView.class.getDeclaredField("mLabelTextSize");
field.setAccessible(true);
labelTextSize = (int) field.get(this);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
paint.setTextSize(labelTextSize);
paint.setTypeface(Typeface.DEFAULT_BOLD);
} else {
int keyTextSize = 0;
try {
field = KeyboardView.class.getDeclaredField("mLabelTextSize");
field.setAccessible(true);
keyTextSize = (int) field.get(this);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
paint.setTextSize(keyTextSize);
paint.setTypeface(Typeface.DEFAULT);
}
paint.getTextBounds(key.label.toString(), 0, key.label.toString()
.length(), bounds);
canvas.drawText(key.label.toString(), key.x + (key.width / 2),
(key.y + key.height / 2) + bounds.height() / 2, paint);
} else if (key.icon != null) { key.icon.setBounds(key.x + (key.width - key.icon.getIntrinsicWidth()) / 2, key.y + (key.height - key.icon.getIntrinsicHeight()) / 2, key.x + (key.width - key.icon.getIntrinsicWidth()) / 2 + key.icon.getIntrinsicWidth(), key.y + (key.height - key.icon.getIntrinsicHeight()) / 2 + key.icon.getIntrinsicHeight()); key.icon.draw(canvas);
}
}
}
增加随机数字键盘 增加速记数字键盘,用于输入安全级别高的密码输入
private void randomKeyboardNumber() {
List<Keyboard.Key> keyList = mKeyboardNumber.getKeys(); // 查找出0-9的数字键
List<Keyboard.Key> newkeyList = new ArrayList<Keyboard.Key>(); for (int i = 0; i < keyList.size(); i++) { if (keyList.get(i).label != null
&& isNumber(keyList.get(i).label.toString())) {
newkeyList.add(keyList.get(i));
}
} // 数组长度
int count = newkeyList.size(); // 结果集
List<KeyModel> resultList = new ArrayList<KeyModel>(); // 用一个LinkedList作为中介
LinkedList<KeyModel> temp = new LinkedList<KeyModel>(); // 初始化temp
for (int i = 0; i < count; i++) {
temp.add(new KeyModel(48 + i, i + ""));
} // 取数
Random rand = new Random();
for (int i = 0; i < count; i++) {
int num = rand.nextInt(count - i);
resultList.add(new KeyModel(temp.get(num).getCode(),
temp.get(num).getLable()));
temp.remove(num);
}
for (int i = 0; i < newkeyList.size(); i++) {
newkeyList.get(i).label = resultList.get(i).getLable();
newkeyList.get(i).codes[0] = resultList.get(i)
.getCode();
}
mKeyboardView.setKeyboard(mKeyboardNumber);
}
最近在调用使用的android系统的api,keyboardview和keyboard
最后的整体效果:
项目地址:
https://github.com/xuejinwei/NumberKeyboard
终端研发部提倡: 没有做不到的,只有想不到的。
在这里获得的不仅仅是技术!
这里学到不仅仅是技术