解鎖Damon——酷炫解鎖
知識點(diǎn)
- 添加虛化層(第三方包)
1.1buid.gradle導(dǎo)入包
1.2使用
1.3基本方法 - 焦點(diǎn)
- 代碼實(shí)現(xiàn)旋轉(zhuǎn)(RotateAnimation)
詳解
-
添加虛化層
使用網(wǎng)絡(luò)第三方包
1.1buid.gradle導(dǎo)入包
.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:虛化程度
- 焦點(diǎn)
當(dāng)有控件獲得焦點(diǎn)focus ?動彈出鍵盤
1.點(diǎn)擊軟鍵盤的enter鍵 ?動收回鍵盤
2.代碼控制 InputMethodManager
requestFocus
showSoftInput:顯示鍵盤 必須先讓這個(gè)view成為焦點(diǎn)requestFocus
hideSoftInputFromWindow 隱藏鍵盤
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN){
//隱藏鍵盤
//1.獲取系統(tǒng)輸?的管理器
InputMethodManager inputManager =
(InputMethodManager)
getSystemService(INPUT_METHOD_SERVICE);
//2.隱藏鍵盤
inputManager.hideSoftInputFromWindow(user.getWindowToken(),
0);
//3.取消焦點(diǎn)
View focusView = getCurrentFocus();
if (focusView != null) {
focusView.clearFocus(); //取消焦點(diǎn)
}
//getCurrentFocus().clearFocus();
//focusView.requestFocus();//請求焦點(diǎn)
}
return true;
}
-
代碼實(shí)現(xiàn)旋轉(zhuǎn)(RotateAnimation)
旋轉(zhuǎn)
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"
/>
<!--添加標(biāo)題和輸入框內(nèi)容-->
<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">
<!--標(biāo)題-->
<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);
//監(jiān)聽內(nèi)容改變 -> 控制按鈕的點(diǎn)擊狀態(tài)
user.addTextChangedListener(this);
password.addTextChangedListener(this);
//監(jiān)聽EidtText的焦點(diǎn)變化 -> 控制是否需要捂住眼睛
password.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus == true){
//捂住眼睛
close();
}else{
//打開
open();
}
}
});
}
/**
* 當(dāng)有控件獲得焦點(diǎn)focus ?動彈出鍵盤
* 1. 點(diǎn)擊軟鍵盤的enter鍵 ?動收回鍵盤
* 2. 代碼控制 InputMethodManager
* requestFocus
* showSoftInput:顯示鍵盤 必須先讓這個(gè)view成為焦點(diǎn)requestFocus
*
* hideSoftInputFromWindow 隱藏鍵盤
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN){
//隱藏鍵盤
//1.獲取系統(tǒng)輸?的管理器
InputMethodManager inputManager =
(InputMethodManager)
getSystemService(INPUT_METHOD_SERVICE);
//2.隱藏鍵盤
inputManager.hideSoftInputFromWindow(user.getWindowToken(),
0);
//3.取消焦點(diǎn)
View focusView = getCurrentFocus();
if (focusView != null) {
focusView.clearFocus(); //取消焦點(diǎn)
}
//getCurrentFocus().clearFocus();
//focusView.requestFocus();//請求焦點(diǎn)
}
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) {
//判斷兩個(gè)輸?框是否有內(nèi)容
if (user.getText().toString().length() > 0 &&
password.getText().toString().length() > 0){
//按鈕可以點(diǎn)擊
loginBtn.setEnabled(true);
}else{
//按鈕不能點(diǎn)擊
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);
}
}
配置文件
image.png
image.png
image.png
image.png
image.png
image.png