解锁Damon——酷炫解锁
知识点
- 添加虚化层(第三方包)
1.1buid.gradle导入包
1.2使用
1.3基本方法 - 焦点
- 代码实现旋转(RotateAnimation)
详解
-
添加虚化层
使用网络第三方包
1.1buid.gradle导入包
1.2使用
<io.alterac.blurkit.BlurLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:blk_fps="0"
app:blk_blurRadius="5"
/>
1.3基本方法
fps:虚化第几帧
blurRadius:虚化程度
- 焦点
当有控件获得焦点focus ⾃动弹出键盘
1.点击软键盘的enter键 ⾃动收回键盘
2.代码控制 InputMethodManager
requestFocus
showSoftInput:显示键盘 必须先让这个view成为焦点requestFocus
hideSoftInputFromWindow 隐藏键盘
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN){
//隐藏键盘
//1.获取系统输⼊的管理器
InputMethodManager inputManager =
(InputMethodManager)
getSystemService(INPUT_METHOD_SERVICE);
//2.隐藏键盘
inputManager.hideSoftInputFromWindow(user.getWindowToken(),
0);
//3.取消焦点
View focusView = getCurrentFocus();
if (focusView != null) {
focusView.clearFocus(); //取消焦点
}
//getCurrentFocus().clearFocus();
//focusView.requestFocus();//请求焦点
}
return true;
}
-
代码实现旋转(RotateAnimation)
Damon——酷炫解锁
xml布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!--添加背景图片-->
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/bg"
android:scaleType="fitXY"
/>
<!--
添加虚化层
fps:虚化第几帧
blurRadius:虚化程度
-->
<io.alterac.blurkit.BlurLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:blk_fps="0"
app:blk_blurRadius="5"
/>
<!--猫头鹰-->
<RelativeLayout
android:layout_width="300dp"
android:layout_height="200dp"
android:layout_centerHorizontal="true"
android:layout_alignTop="@+id/bg"
android:layout_marginTop="-100dp">
<!--头像-->
<ImageView
android:id="@+id/iv_head"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/owl_head"
android:layout_centerHorizontal="true"
/>
<!--手掌-->
<ImageView
android:id="@+id/iv_left_hand"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@drawable/icon_hand"
android:layout_alignParentLeft="true"
android:layout_alignBottom="@+id/iv_head"
android:layout_marginBottom="-20dp"
android:layout_marginLeft="10dp"/>
<ImageView
android:id="@+id/iv_right_hand"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@drawable/icon_hand"
android:layout_alignParentRight="true"
android:layout_alignBottom="@+id/iv_head"
android:layout_marginBottom="-20dp"
android:layout_marginRight="10dp"/>
<!--翅膀-->
<ImageView
android:id="@+id/iv_left_arm"
android:layout_width="70dp"
android:layout_height="50dp"
android:src="@drawable/arm_left"
android:layout_below="@+id/iv_head"
android:layout_alignParentLeft="true"
android:layout_marginLeft="25dp"/>
<ImageView
android:id="@+id/iv_right_arm"
android:layout_width="70dp"
android:layout_height="50dp"
android:src="@drawable/arm_right"
android:layout_below="@+id/iv_head"
android:layout_alignParentRight="true"
android:layout_marginRight="25dp"/>
</RelativeLayout>
<!--输入框背景-->
<io.alterac.blurkit.BlurLayout
android:id="@+id/bg"
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="@drawable/inoput_bg_shap"
android:layout_centerInParent="true"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
app:blk_fps="0"
app:blk_blurRadius="25"
/>
<!--添加标题和输入框内容-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_centerInParent="true"
android:orientation="vertical"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp">
<!--标题-->
<TextView android:layout_height="50dp"
android:layout_width="match_parent"
android:text="酷炫登录"
android:textColor="#999999"
android:textSize="30dp"
android:textAlignment="center"/>
<!--添加输入框-->
<EditText
android:id="@+id/et_user"
android:drawableLeft="@drawable/iconfont_user"
style="@style/EditTextStyle"
android:inputType="text"
android:hint="请输入用户名"
/>
<EditText
android:id="@+id/et_password"
android:drawableLeft="@drawable/iconfont_password"
style="@style/EditTextStyle"
android:inputType="textPassword"
android:hint="请输入密码"/>
<Button
android:id="@+id/bt_login"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="登录"
android:textColor="#ffffff"
android:layout_marginTop="20dp"
android:enabled="false"
android:background="@drawable/login_selector"/>
</LinearLayout>
</RelativeLayout>
Java代码
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.view.animation.TranslateAnimation;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
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);
initViews();
}
public void initViews(){
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);
//监听EidtText的焦点变化 -> 控制是否需要捂住眼睛
password.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus == true){
//捂住眼睛
close();
}else{
//打开
open();
}
}
});
}
/**
* 当有控件获得焦点focus ⾃动弹出键盘
* 1. 点击软键盘的enter键 ⾃动收回键盘
* 2. 代码控制 InputMethodManager
* requestFocus
* showSoftInput:显示键盘 必须先让这个view成为焦点requestFocus
*
* hideSoftInputFromWindow 隐藏键盘
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN){
//隐藏键盘
//1.获取系统输⼊的管理器
InputMethodManager inputManager =
(InputMethodManager)
getSystemService(INPUT_METHOD_SERVICE);
//2.隐藏键盘
inputManager.hideSoftInputFromWindow(user.getWindowToken(),
0);
//3.取消焦点
View focusView = getCurrentFocus();
if (focusView != null) {
focusView.clearFocus(); //取消焦点
}
//getCurrentFocus().clearFocus();
//focusView.requestFocus();//请求焦点
}
return true;
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int
after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int
count) {
}
@Override
public void afterTextChanged(Editable s) {
//判断两个输⼊框是否有内容
if (user.getText().toString().length() > 0 &&
password.getText().toString().length() > 0){
//按钮可以点击
loginBtn.setEnabled(true);
}else{
//按钮不能点击
loginBtn.setEnabled(false);
}
}
public void close(){
//左边
RotateAnimation lAnim = new RotateAnimation(0,
175,
leftArm.getWidth(),
0f);
lAnim.setDuration(500);
lAnim.setFillAfter(true);
leftArm.startAnimation(lAnim);
RotateAnimation rAnim = new RotateAnimation(0,
-175,
0f,
0f);
rAnim.setDuration(500);
rAnim.setFillAfter(true);
rightArm.startAnimation(rAnim);
TranslateAnimation down = (TranslateAnimation) AnimationUtils.loadAnimation(this,R.anim.hang_down_anim);
leftHand.startAnimation(down);
rightHand.startAnimation(down);
}
public void open(){
//左边
RotateAnimation lAnim = new RotateAnimation(175,
0,
leftArm.getWidth(),
0f);
lAnim.setDuration(500);
lAnim.setFillAfter(true);
leftArm.startAnimation(lAnim);
RotateAnimation rAnim = new RotateAnimation(-175,
0,
0f,
0f);
rAnim.setDuration(500);
rAnim.setFillAfter(true);
rightArm.startAnimation(rAnim);
TranslateAnimation up = (TranslateAnimation) AnimationUtils.loadAnimation(this,R.anim.hang_up_anim);
leftHand.startAnimation(up);
rightHand.startAnimation(up);
}
}
配置文件