先贴上我的注册页面
import android.app.Activity; import android.app.ProgressDialog; import android.content.Intent; import android.content.pm.ActivityInfo; import android.graphics.Bitmap; import android.graphics.Matrix; import android.os.Bundle; import android.provider.MediaStore; import android.text.TextUtils; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.view.WindowManager; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.Toast; import static com.example.myproject.R.id.roundRect3; public class RegisterActivity extends Activity implements OnClickListener { private EditText eUsername; private EditText ePassword1; private EditText ePassword2; private Button submitBtn; private Button LoginButton; private ProgressDialog Progress; private ZQImageViewRoundOval iv_circle; private static int REQUEST_THUMBNAIL = 1;// 请求缩略图信号标识 private float scaleWidth=1; private float scaleHeight=1; /*private static int REQUEST_ORIGINAL = 2;// 请求原图信号标识 private String sdPath;//SD卡的路径 private String picPath;//图片存储路径 */ public String serverUrl = "http://xxx.xx.xxx.x:8080/xxxxx/NewAccount"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); setContentView(R.layout.activity_register); initViews(); } private void initViews() { eUsername = (EditText) findViewById(R.id.new_username); ePassword1 = (EditText) findViewById(R.id.new_password_1); ePassword2 = (EditText) findViewById(R.id.new_password_2); submitBtn = (Button) findViewById(R.id.new_btn_submit); LoginButton = (Button) findViewById(R.id.btnLogin); submitBtn.setOnClickListener(this); LoginButton.setOnClickListener(this); iv_circle=(ZQImageViewRoundOval)findViewById(roundRect3); iv_circle.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.new_btn_submit: TextToString(); break; case R.id.btnLogin: ToLogin(); break; case roundRect3: OpenCamera(); break; default: break; } } private void OpenCamera(){ Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(intent, 1); } /** * 返回应用时回调方法 */ @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK) { if (requestCode == REQUEST_THUMBNAIL) {//对应第一种方法 /** * 通过这种方法取出的拍摄会默认压缩,因为如果相机的像素比较高,拍摄出来的图会比较高清, * 如果图太大会造成内存溢出(OOM),因此此种方法会默认给图片尽心压缩 */ Bundle bundle = data.getExtras(); Bitmap bmp = (Bitmap) bundle.get("data"); int bmpWidth = bmp.getWidth(); int bmpHeight = bmp.getHeight(); if(bmpWidth<=180&&bmpWidth<=180){ ((ImageView) findViewById(R.id.roundRect3)).setImageBitmap(bmp);// 将图片显示在ImageView里 }else { /* 设置图片放大的比例 */ double scale = 4; /* 计算这次要放大的比例 */ scaleWidth = (float) (scaleWidth * scale); scaleHeight = (float) (scaleHeight * scale); /* 产生reSize后的Bitmap对象 */ Matrix matrix = new Matrix(); matrix.postScale(scaleWidth, scaleHeight); Bitmap resizeBmp = Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeight, matrix, true); ((ImageView) findViewById(R.id.roundRect3)).setImageBitmap(resizeBmp);// 将图片显示在ImageView里 } } } } private void TextToString() { String username = eUsername.getText().toString(); String password = ePassword1.getText().toString(); CheckTextBox(); Register(username, password); } private void Register(final String username, final String password) { Progress = new ProgressDialog(this); Progress.setCancelable(false); Progress.setCanceledOnTouchOutside(false); ADataBase db = new ADataBase(); int jsonResult = db.WebConnoct(username, password, serverUrl); hanleCreateAccountResult(jsonResult); } private void ToLogin() { Intent intent = new Intent(this, LoginActivity.class); startActivity(intent); finish(); } public void hanleCreateAccountResult(int jsonResult) { /* * result_code: * 0 注册成功 * 1 用户名已存在 * 2 数据库操作异常 * */ int result; result = jsonResult; if(result == 1) { Toast.makeText(this, "用户名已存在!", Toast.LENGTH_LONG).show(); return; } if(result == 2) { Toast.makeText(this, "注册失败!服务端出现异常!", Toast.LENGTH_LONG).show(); return; } if(result == 0) { Progress.dismiss(); Toast.makeText(this, "注册成功!前往登录页面!", Toast.LENGTH_LONG).show(); Intent intent = new Intent(this, LoginActivity.class); startActivity(intent); finish(); return; } } private boolean checkUsername() { String username = eUsername.getText().toString(); if(TextUtils.isEmpty(username)) { return false; } return true; } private int checkPassword() { /* * return value: * 0 password valid * 1 password not equal 2 inputs * 2 password empty * */ String pwd1 = ePassword1.getText().toString(); String pwd2 = ePassword2.getText().toString(); if(!pwd1.equals(pwd2)) { return 1; } else if(TextUtils.isEmpty(pwd1)) { return 2; } else { return 0; } } private void CheckTextBox() { boolean isUsernameValid = checkUsername(); if(!isUsernameValid) { Toast.makeText(this, "用户名不正确,请重新输入", Toast.LENGTH_LONG).show(); return; } int pwdResult = checkPassword(); if(pwdResult == 1) { Toast.makeText(this, "两次输入的密码不一致,请确认!", Toast.LENGTH_LONG).show(); return; } if (pwdResult == 2) { Toast.makeText(this, "密码不能为空!", Toast.LENGTH_LONG).show(); return; } } }
注册的xml页面代码
以下内容为说明:
上面的代码直接粘贴会出现问题,因为缺少一个自定义类——AImageViewRoundOval.java,代码我都已经放在了上一篇文章中。https://my.oschina.net/u/3724795/blog/1583883
说一下onActivityResult方法,这里使用的是bundle方法,对拍摄的图片进行压缩,但是我在运行时发现,如果直接使用下面的代码,图片会缩小,很难看。
/**
* 返回应用时回调方法
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == REQUEST_THUMBNAIL) {//对应第一种方法
/**
* 通过这种方法取出的拍摄会默认压缩,因为如果相机的像素比较高,拍摄出来的图会比较高清,
* 如果图太大会造成内存溢出(OOM),因此此种方法会默认给图片尽心压缩
*/
Bundle bundle = data.getExtras();
Bitmap bmp = (Bitmap) bundle.get("data");
((ImageView) findViewById(R.id.roundRect3)).setImageBitmap(bmp);// 将图片显示在ImageView里
}
}
}
所以我就重定义了一下ImageView的大小,放大图片到原来的大小(我还发现这个代码的漏洞就是相机如果使用的是前摄的话,即使我重新定义了一下大小,也没用,还是那么小),其实放大的比例可以根据你自己的需要进行调节。其实掌握了这个代码,你完全可以做一个放大,缩小图片的窗体。
/* 设置图片放大的比例 */
double scale = 4;
/* 计算这次要放大的比例 */
scaleWidth = (float) (scaleWidth * scale);
scaleHeight = (float) (scaleHeight * scale);