模仿支付寶輸入效果,實現很簡單,就是畫個矩形框和圓形,其他的通過組合view來實現所有功能,雖然簡單但是封裝起來,方便以后使用,也分享一下,希望對別人也有點幫助。

1、如何使用,可以設置自己的進入退出動畫,不設置則沒有動畫效果,自己覺得封裝之后還是非常用好的。
private MyInputPwdUtil myInputPwdUtil;@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myInputPwdUtil = new MyInputPwdUtil(this); myInputPwdUtil.getMyInputDialogBuilder().setAnimStyle(R.style.dialog_anim); //可以定制自己進入退出動畫,不設置沒有動畫 myInputPwdUtil.setListener(new InputPwdView.InputPwdListener() { @Override public void hide() { myInputPwdUtil.hide(); } @Override public void forgetPwd() { Toast.makeText(MainActivity.this, "忘記密碼", Toast.LENGTH_SHORT).show(); } @Override public void finishPwd(String pwd) { Toast.makeText(MainActivity.this, pwd, Toast.LENGTH_SHORT).show(); } });}public void show(View view){ myInputPwdUtil.show();}2、輸入框實現主要代碼,就是繪制矩形和中間的圓形而已。
int height = getHeight(); int width = getWidth(); //畫邊框 RectF rect = new RectF(0, 0, width, height); borderPaint.setColor(borderColor); canvas.drawRoundRect(rect, borderRadius, borderRadius, borderPaint); //畫內容區域 RectF rectContent = new RectF(rect.left + defaultContentMargin, rect.top + defaultContentMargin, rect.right - defaultContentMargin, rect.bottom - defaultContentMargin); borderPaint.setColor(getResources().getColor(R.color.myInputPwdBase_gray)); canvas.drawRoundRect(rectContent, borderRadius, borderRadius, borderPaint); //畫分割線:分割線數量比密碼數少1 borderPaint.setColor(borderColor); borderPaint.setStrokeWidth(defaultSplitLineWidth); for (int i = 1; i < passwordLength; i++) { float x = width * i / passwordLength; canvas.drawLine(x, 0, x, height, borderPaint); } //畫密碼內容 float px, py = height / 2; float halfWidth = width / passwordLength / 2; for (int i = 0; i < textLength; i++) { px = width * i / passwordLength + halfWidth; canvas.drawCircle(px, py, passwordWidth, passwordPaint); }3、作為library的module,在定義使用到的屬性的時候最好特別能區分開,設置特定的開頭,這樣能避免引入自己的工程之后導致沖突。
如有bug,謝謝指出。
源碼下載
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。
新聞熱點
疑難解答