笔记:此次简单的登录是对课本中的数据存储进行练习,比较简单;其中圆形图片是学习他人所得,具体忘记是谁看了太多就不@了OvO。
演示效果:
首先上效果图
二、ImageRound类和SpSaveAccount类
自己创建的类,imageRound继承AppCompatImageView,SpSaveAccount封装了保存账号,获取账号两个类,方便使用。
1、ImageRound类public class ImageRound extends AppCompatImageView {
笔画
private Paint imagePaint;
// 圆形半径
private int imageRadius;
// 圆形的缩放比例
private float imageScle;
public ImageRound(Context context) {
super(context);
}
public ImageRound(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ImageRound(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width=getMeasuredWidth();
int height=getMeasuredHeight();
int size=Math.min(width,height);
imageRadius=size/2;
setMeasuredDimension(size,size);
}
@Override
protected void onDraw(Canvas canvas) {
imagePaint=new Paint();
Drawable drawable=getDrawable();
if(drawable!=null){
Bitmap bitmap=drawableToBitmap(getDrawable());
// BitmapShader(传入bitmap对象,X轴的平铺模式,y轴的平铺模式)
// 平铺模式:Shader.TileMode.CLAMP:如果着色器超出原始边界范围,会复制边缘颜色
BitmapShader bitmapShader=new BitmapShader(bitmap, Shader.TileMode.CLAMP,Shader.TileMode.CLAMP);
// 计算缩放比例
imageScle=(imageRadius*2.0f)/Math.min(bitmap.getHeight(),bitmap.getWidth());
Matrix matrix=new Matrix();
matrix.setScale(imageScle,imageScle);
bitmapShader.setLocalMatrix(matrix);
imagePaint.setShader(bitmapShader);
// drawCircle方法(圆心的x坐标,圆心的y坐标,圆半径,画笔):绘制圆形
canvas.drawCircle(imageRadius,imageRadius,imageRadius,imagePaint);
}else {
super.onDraw(canvas);
}
}
// drawable转BitMap的方法
private Bitmap drawableToBitmap(Drawable drawable){
if (drawable instanceof BitmapDrawable) {
BitmapDrawable bitmapDrawable=(BitmapDrawable) drawable;
return bitmapDrawable.getBitmap();
}
int w = drawable.getIntrinsicWidth();
int h = drawable.getIntrinsicHeight();
Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, w, h);
drawable.draw(canvas);
return bitmap;
}
}
2、SpSaveAccount类
public class SpSaveAccount {
// 保存账号
public static void saveUserInfo(Context context,String account,String password){
SharedPreferences sharedPreferences=context.getSharedPreferences("user",Context.MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putString("userId",account);
editor.putString("password",password);
editor.commit();
}
// 读取账号
public static Map getUserInfo(Context context){
SharedPreferences sharedPreferences=context.getSharedPreferences("user",Context.MODE_PRIVATE);
String account=sharedPreferences.getString("userId",null);
String pwd=sharedPreferences.getString("password",null);
Map userMap=new HashMap();
userMap.put("account",account);
userMap.put("password",pwd);
return userMap;
}
}
三、MainActivity
public class MainActivity extends AppCompatActivity {
private Button btnLogin;
private EditText etName,etPwd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etName=findViewById(R.id.et_account);
etPwd=findViewById(R.id.et_password);
btnLogin=findViewById(R.id.btn_login);
// 登录账号
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String account=etName.getText().toString().trim();
String pwd=etPwd.getText().toString();
// 账户输入如果为空
if(TextUtils.isEmpty(account)||TextUtils.isEmpty(pwd)){
Toast.makeText(MainActivity.this,"账号密码输入不能为空",Toast.LENGTH_SHORT).show();
return;
}
SpSaveAccount.saveUserInfo(MainActivity.this,account,pwd);
Toast.makeText(MainActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
}
});
// 获取账号并将其显示出来
Map userMap=SpSaveAccount.getUserInfo(MainActivity.this);
if(userMap!=null){
etName.setText(userMap.get("account"));
etPwd.setText(userMap.get("password"));
}
}
}



