转载请注明出处:王亟亟的大牛之路
上一篇博文写了一个生成二维码的,这一篇来一个随机数的。
包目录
运行效果:
就是画了一串东西然后放入imageview
话不多说直接上代码:
public class MainActivity extends Activity {
ProgressDialog mLoadingDialog;
Button ShowCAPTCHAD;
ImageView imageview;
Bitmap validateCodeImage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageview=(ImageView)findViewById(R.id.imageview);
ShowCAPTCHAD=(Button)findViewById(R.id.ShowCAPTCHAD);
ShowCAPTCHAD.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showLoadingDialog("Loading......");
//回收bitmap
if(null != validateCodeImage && !validateCodeImage.isRecycled()){
validateCodeImage.recycle();
validateCodeImage = null;
}
//生成图片
validateCodeImage =makeValidateCode(200, 70);
imageview.setImageBitmap(validateCodeImage);
Toast.makeText(MainActivity.this, "验证码生成成功"+"验证码是"+gainValidateCodeValue(), Toast.LENGTH_SHORT).show();
dismissLoadingDialog();
}
});
}
/*显示对话框*/
public void showLoadingDialog(String msg) {
if (mLoadingDialog != null && mLoadingDialog.isShowing()) {
return;
}
mLoadingDialog = new ProgressDialog(this);
mLoadingDialog.setMessage(msg);
// mLoadingDialog.setOnKeyListener(mOnKeyListener);
// mLoadingDialog.setCancelable(false);
mLoadingDialog.show();
}
/**
* 取消加载对话框
*/
public void dismissLoadingDialog() {
if (mLoadingDialog != null) {
mLoadingDialog.dismiss();
}
}
@Override