Android開發(fā)第六天

解鎖Damon——酷炫解鎖

知識點(diǎn)

  1. 添加虛化層(第三方包)
    1.1buid.gradle導(dǎo)入包
    1.2使用
    1.3基本方法
  2. 焦點(diǎn)
  3. 代碼實(shí)現(xiàn)旋轉(zhuǎn)(RotateAnimation)

詳解

  1. 添加虛化層
    使用網(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:虛化程度

  1. 焦點(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;
   }
  1. 代碼實(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
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末裹虫,一起剝皮案震驚了整個(gè)濱河市骚勘,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 212,599評論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異稠腊,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)鸣哀,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,629評論 3 385
  • 文/潘曉璐 我一進(jìn)店門架忌,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人我衬,你說我怎么就攤上這事叹放。” “怎么了挠羔?”我有些...
    開封第一講書人閱讀 158,084評論 0 348
  • 文/不壞的土叔 我叫張陵井仰,是天一觀的道長。 經(jīng)常有香客問我破加,道長俱恶,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,708評論 1 284
  • 正文 為了忘掉前任拌喉,我火速辦了婚禮速那,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘尿背。我一直安慰自己端仰,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,813評論 6 386
  • 文/花漫 我一把揭開白布田藐。 她就那樣靜靜地躺著荔烧,像睡著了一般。 火紅的嫁衣襯著肌膚如雪汽久。 梳的紋絲不亂的頭發(fā)上鹤竭,一...
    開封第一講書人閱讀 50,021評論 1 291
  • 那天,我揣著相機(jī)與錄音景醇,去河邊找鬼臀稚。 笑死,一個(gè)胖子當(dāng)著我的面吹牛三痰,可吹牛的內(nèi)容都是我干的吧寺。 我是一名探鬼主播,決...
    沈念sama閱讀 39,120評論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼散劫,長吁一口氣:“原來是場噩夢啊……” “哼稚机!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起获搏,我...
    開封第一講書人閱讀 37,866評論 0 268
  • 序言:老撾萬榮一對情侶失蹤赖条,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體纬乍,經(jīng)...
    沈念sama閱讀 44,308評論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡碱茁,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,633評論 2 327
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了仿贬。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片早芭。...
    茶點(diǎn)故事閱讀 38,768評論 1 341
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖诅蝶,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情募壕,我是刑警寧澤调炬,帶...
    沈念sama閱讀 34,461評論 4 333
  • 正文 年R本政府宣布,位于F島的核電站舱馅,受9級特大地震影響缰泡,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜代嗤,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 40,094評論 3 317
  • 文/蒙蒙 一棘钞、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧干毅,春花似錦宜猜、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,850評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至渠鸽,卻和暖如春叫乌,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背徽缚。 一陣腳步聲響...
    開封第一講書人閱讀 32,082評論 1 267
  • 我被黑心中介騙來泰國打工憨奸, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人凿试。 一個(gè)月前我還...
    沈念sama閱讀 46,571評論 2 362
  • 正文 我出身青樓排宰,卻偏偏與公主長得像,于是被迫代替她去往敵國和親红省。 傳聞我的和親對象是個(gè)殘疾皇子额各,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,666評論 2 350

推薦閱讀更多精彩內(nèi)容

  • android developers系列二(動畫) 這里先講一下動畫的類型 幀動畫 屬性動畫 補(bǔ)間動畫 下面有詳細(xì)...
    WangYouHu閱讀 546評論 0 1
  • 自己總結(jié)的Android開源項(xiàng)目及庫。 github排名https://github.com/trending,g...
    passiontim閱讀 2,532評論 1 26
  • 2017年9月20日下午2點(diǎn) 工作時(shí)間吧恃,正要打印文件時(shí)虾啦,打印機(jī)癱瘓,辦公軟件word和excel都打不開,總提示“...
    麥黃果閱讀 258評論 0 2
  • 這是一個(gè)盛行習(xí)劍的時(shí)代傲醉。家中若有男丁蝇闭,必定幼年就會別送到各大劍派修習(xí);而家中若有女眷硬毕,也必定都是一心相嫁一個(gè)仗劍天...
    欲曉閱讀 524評論 8 1