android验证码(canvas)-创新互联
activity: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LinearLayout linearLayout = new LinearLayout(this); VerifyCodeView vc = new VerifyCodeView(this,6); TextView textView = new TextView(this); textView.setText("验证码:"); EditText mInCode = new EditText(this); vc.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { v.postInvalidate();//重新调用VerifyCodeView的构造方法,更新验证码 Toast.makeText(getApplicationContext(),"updat code",Toast.LENGTH_LONG).show(); } }); linearLayout.addView(textView); linearLayout.addView(mInCode); linearLayout.addView(vc); setContentView(linearLayout); } view: private Character[] codes = {'q','w','e','r','t','y','u','i' ,'o','p','a','s','d','f','g','h' ,'j','k','l','z','x','c','v','b' ,'n','m','1','2','3','4','5','6' ,'7','8','9','0'}; private Character[] vcode; public VerifyCodeView(Context context) { super(context); vcode = new Character[4];//定义验证码的长度 } public VerifyCodeView(Context context, int codeSize) { super(context); vcode = new Character[codeSize];//定义验证码的长度 } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); vcode = getCodes(codes); Paint pen = new Paint(); pen.setColor(Color.WHITE); int mwidth = getWidth(); canvas.drawRect(mwidth*2/3,10.0f,mwidth-20,100.0f,pen); Random r = new Random(100); int padding = 1; for (int i = 0; i < vcode.length; i++) { pen.setColor(Color.rgb(r.nextInt(),r.nextInt(),r.nextInt())); pen.setTextSize(60); //canvas.drawText(new char[]{'a','c','b','d','1','2','e'},0,7,mwidth*2/3+10,70.0f,pen); //String text = "h"+r.nextInt()+"e"+r.nextInt()+"l"; canvas.drawText(vcode[i].toString(),mwidth*2/3+padding,70.0f,pen); padding = padding + 35; } } public Character[] getCodes(Character[] codes){ Random rd = new Random(); for (int i = 0; i < vcode.length; i++) { vcode[i] = codes[rd.nextInt(36)]; } return vcode; }
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
成都网络公司-成都网站建设公司创新互联十载经验成就非凡,专业从事网站制作、成都网站建设,成都网页设计,成都网页制作,软文推广,一元广告等。十载来已成功提供全面的成都网站建设方案,打造行业特色的成都网站建设案例,建站热线:028-86922220,我们期待您的来电!分享题目:android验证码(canvas)-创新互联
文章起源:http://pwwzsj.com/article/idgcp.html