Android开源:NumberKeyboard - 一个高仿闲鱼键自定义数字键盘特效
<p>仿造android端闲鱼发布选择价格数字键盘,自定义数字键盘</p> <p>先附上效果再说:</p> <p><img src="https://simg.open-open.com/show/997f59130af28880accf45d9f8645fd5.gif"></p> <p>价格输入测试</p> <p><img src="https://simg.open-open.com/show/19368b49386e57b2dfb1af044fdb732e.png"></p> <p>image</p> <p><img src="https://simg.open-open.com/show/8310ceba8a4ff57599eb36d7a885e8a1.png"></p> <pre> <code class="language-java">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); } } }</code></pre> <p>增加随机数字键盘 增加速记数字键盘,用于输入安全级别高的密码输入</p> <pre> <code class="language-java">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); }</code></pre> <p>最近在调用使用的android系统的api,keyboardview和keyboard</p> <p><img src="https://simg.open-open.com/show/a3f8bb4961fba599babf1d3ab0def370.gif"></p> <p>项目主页:<a href="http://www.open-open.com/lib/view/home/1503218485040">http://www.open-open.com/lib/view/home/1503218485040</a></p>
本文由用户 fengyizhy 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
转载本站原创文章,请注明出处,并保留原始链接、图片水印。
本站是一个以用户分享为主的开源技术平台,欢迎各类分享!