1.导入素材
2.配值xml文件
1.Activity_main.xml
2.edit_view_shape.xml
3.input_bg_shape.xml
4.login_bin_selector.xml
5.translate.xml
6.translate1.xml
7.addview.style.xml
8.styles.xml
3.MainActivity
public class MainActivity extends AppCompatActivity implements TextWatcher {
private EditText user;
private EditText password;
private Button loginBtn;
private ImageView leftarm;
private ImageView rightarm;
private ImageView leftHand;
private ImageView rightHand;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
public void initView(){
user=findViewById(R.id.et_user);
password=findViewById(R.id.et_password);
loginBtn=findViewById(R.id.bt_login);
leftarm=findViewById(R.id.iv_left_arm);
rightarm=findViewById(R.id.iv_right_arm);
leftHand=findViewById(R.id.iv_left_hand);
rightHand=findViewById(R.id.iv_right_hand);
//监听内容改变 控制按钮是否可以点击
user.addTextChangedListener(this);
password.addTextChangedListener(this);
//监听EditText的焦点变化->控制是否需要捂住眼睛
password.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean hasfocus) {
if (hasfocus==true){
//捂住眼睛
close();
}else{
//打开
open();
}
}
});
}
/**
*当有控件获得焦点focus 自动弹出键盘
* 1.点击软键盘的enter 自动收回键盘
* 2.代码控制 InputMethodMannager
*
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_DOWN) {
//隐藏键盘
//1.获取系统输入的管理器
InputMethodManager inputMethodManager=(InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
//2.隐藏键盘
inputMethodManager.hideSoftInputFromWindow(user.getWindowToken(),0);
//3.取消焦点
View focusView=getCurrentFocus();
if(focusView!=null){
focusView.clearFocus();
}
// focusView.requestFocus();//请求焦点
}
return true;
}
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
//判断两个输入框是否有内容
if(user.getText().toString().length()>0&&password.getText().toString().length()>0){
//按钮可以点击
loginBtn.setEnabled(true);
}else{
//按钮不能点击
loginBtn.setEnabled(false);
}
}
public void close(){
//旋转翅膀左边
RotateAnimation lrotateAnimation=new RotateAnimation(0,170,leftarm.getWidth(),0f);
lrotateAnimation.setDuration(400);
lrotateAnimation.setFillAfter(true);
//右边
RotateAnimation rrotateAnimation=new RotateAnimation(0,-170,0f,0f);
rrotateAnimation.setDuration(400);
rrotateAnimation.setFillAfter(true);
leftarm.startAnimation(lrotateAnimation);
rightarm.startAnimation(rrotateAnimation);
TranslateAnimation down = (TranslateAnimation) AnimationUtils.loadAnimation(this,R.anim.translate);
leftHand.startAnimation(down);
rightHand.startAnimation(down);
}
public void open(){
//旋转翅膀左边
RotateAnimation lrotateAnimation=new RotateAnimation(170,0,leftarm.getWidth(),0f);
lrotateAnimation.setDuration(400);
lrotateAnimation.setFillAfter(true);
//右边
RotateAnimation rrotateAnimation=new RotateAnimation(-170,0,0f,0f);
rrotateAnimation.setDuration(400);
rrotateAnimation.setFillAfter(true);
leftarm.startAnimation(lrotateAnimation);
rightarm.startAnimation(rrotateAnimation);
TranslateAnimation up = (TranslateAnimation) AnimationUtils.loadAnimation(this,R.anim.translate1);
leftHand.startAnimation(up);
rightHand.startAnimation(up);
}
}
4.心得体会
很有趣的一个demo,学多了不少关于补间动画的知识,很有意义