炫酷登录

简介

今天为大家介绍的是登录界面的搭建,使用动画使得登录界面更有趣味,话不多说,先上效果图:


实现

1. 将需要的图片文件导入drawable中(此项目素材在本文末尾提供)
2.在下图路径下添加 implementation 'io.alterac.blurkit:blurkit:1.1.0',导入虚化效果第三方库
3.创建布局需要的资源文件

1.在drawable下创建文件
editview_shape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <corners android:radius="10dp"></corners>
    <stroke android:width="1dp"
        android:color="#4F4F4F"></stroke>

</shape>

2.drawable路径下创建input_bg_shape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <corners android:radius="20dp"></corners>
    <solid android:color="#88999999"></solid>

</shape>

3.drawable路径下创建login_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!--无效状态 灰色背景 圆角矩形-->
    <item android:state_enabled="false">
        <shape android:shape="rectangle">
            <corners android:radius="10dp"></corners>
            <solid android:color="#666666"></solid>
        </shape>
    </item>
    <!--正常状态 蓝色背景 圆角矩形-->
    <item>
        <shape android:shape="rectangle">
            <corners android:radius="10dp"></corners>
            <solid android:color="#00BFFF"></solid>
        </shape>
    </item>
</selector>

4.在value路径下创建editview_style.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!--app标题:字体 字号 颜色-->
    <!--共同拥有的东西-->
    <style name="EditTextStyle">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">50dp</item>
        <item name="android:background">@drawable/editview_shape</item>
        <item name="android:layout_marginTop">20dp</item>
        <item name="android:paddingLeft">10dp</item>
        <item name="android:drawablePadding">10dp</item>
        <item name="android:textSize">20dp</item>
        <item name="android:maxLines">1</item>
    </style>

</resources>
4.接着在activity_main.xml中进行布局
<?xml version="1.0" encoding="utf-8"?>
<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"/>

    
    <!--添加虚化层-->
    <io.alterac.blurkit.BlurLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:blk_fps="0"
        app:blk_blurRadius="20"
        ></io.alterac.blurkit.BlurLayout>

    <!--猫头鹰-->
    <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/head"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/owl_head"
            android:layout_centerHorizontal="true"
            />

        <!--手掌-->
        <ImageView
        android:id="@+id/hand_left"
        android:layout_width="50dp"
        android:layout_height="60dp"
        android:src="@drawable/icon_hand"
        android:layout_alignParentLeft="true"
        android:layout_alignBottom="@+id/head"
        android:layout_marginBottom="-25dp"
            android:layout_marginLeft="10dp"/>
        <ImageView
        android:id="@+id/hand_right"
        android:layout_width="50dp"
        android:layout_height="60dp"
        android:src="@drawable/icon_hand"
        android:layout_alignParentRight="true"
        android:layout_alignBottom="@+id/head"
        android:layout_marginBottom="-25dp"
        android:layout_marginRight="10dp"/>

        <!--翅膀-->
        <ImageView
            android:id="@+id/arm_left"
            android:layout_width="65dp"
            android:layout_height="40dp"
            android:src="@drawable/arm_left"
            android:layout_below="@+id/head"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="20dp"/>

        <ImageView
            android:id="@+id/arm_right"
            android:layout_width="65dp"
            android:layout_height="40dp"
            android:src="@drawable/arm_right"
            android:layout_below="@+id/head"
            android:layout_alignParentRight="true"
            android:layout_marginRight="20dp"/>
    </RelativeLayout>


    <View
        android:id="@+id/bg"
        android:layout_width="match_parent"
        android:layout_height="280dp"
        android:background="@drawable/input_bg_shape"
        android:layout_centerInParent="true"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        />
    <io.alterac.blurkit.BlurLayout
        android:layout_width="match_parent"
        android:layout_height="280dp"
        app:blk_fps="0"
        app:blk_blurRadius="20"
        android:layout_centerInParent="true"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        ></io.alterac.blurkit.BlurLayout>

    <!--添加标题和输入框-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_centerInParent="true"
        android:orientation="vertical"
        android:padding="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        >

        <!--标题-->
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="我是一只猫头鹰"
            android:textSize="20dp"
            android:textAlignment="center"
            />

        <!--添加输入框-->
        <EditText
            android:id="@+id/et_user"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:background="@drawable/editview_shape"
            android:layout_marginTop="20dp"
            android:drawableLeft="@drawable/iconfont_user"
            android:paddingLeft="10dp"
            android:drawablePadding="10dp"
            android:textSize="18dp"
            android:hint="请输入账号"
            android:maxLines="1"
            android:inputType="text"/>

        <EditText
            android:id="@+id/et_password"
            style="@style/EditTextStyle"
            android:inputType="textPassword"
            android:drawableLeft="@drawable/iconfont_password"
            android:hint="请输入密码"/>

        <!--登录按钮-->
        <Button
            android:id="@+id/login_btn"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_marginTop="20dp"
            android:background="@drawable/login_selector"
            android:text="登录"
            android:enabled="false"
            android:textSize="25dp"
            android:textAlignment="center"
            android:textColor="#ffffff"/>
    </LinearLayout>

</RelativeLayout>
5.创建两个动画文件

res路径下创建anim目录(详情参考(https://www.jianshu.com/p/dcb6ae758410))
translate_down.xml

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromYDelta="0"
    android:toYDelta="100"
    android:fillAfter = "true"
    android:duration = "500">

</translate>

translate_up.xml

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromYDelta="100"
    android:toYDelta="0"
    android:fillAfter = "true"
    android:duration = "500">

</translate>
6.创建第二个界面MainActivity2,并配置xml

xml中配置

<?xml version="1.0" encoding="utf-8"?>
<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=".Main2Activity">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:src="@drawable/yans"
        android:scaleType="fitXY"/>

</RelativeLayout>
7.最后,在MainActivity中使用
public class MainActivity extends AppCompatActivity implements TextWatcher {

    private EditText user;
    private EditText password;
    private Button login;
    private ImageView rightArm;
    private ImageView leftArm;
    private ImageView leftHand;
    private ImageView rightHand;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initViews();
    }

    /**
     * 键盘
     * 当有控件获得焦点focus 自动弹出键盘
     * 1.点击软键盘的 return键 自动收回键盘
     * 2.代码控制  InputMethodManager
     *    showSoftInput  显示键盘 必须先让这个view成为焦点
     *
     * */

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN){
            //隐藏键盘
            // 1.获取系统输入输出的管理器
            InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
            // 2.隐藏键盘
            System.out.println(inputMethodManager.toString());
            inputMethodManager.hideSoftInputFromWindow(user.getWindowToken(), 0);
            // 3.取消焦点
            View focusView = getCurrentFocus();
            if (focusView != null){
                focusView.clearFocus();
            }
//            getCurrentFocus().requestFocus();  //请求焦点
        }
        return true;
    }


    private void initViews(){
        user = findViewById(R.id.et_user);
        password = findViewById(R.id.et_password);
        login = findViewById(R.id.login_btn);
        leftArm = findViewById(R.id.arm_left);
        rightArm = findViewById(R.id.arm_right);
        leftHand = findViewById(R.id.hand_left);
        rightHand = findViewById(R.id.hand_right);

        //监听内容改变
        user.addTextChangedListener(this);
        password.addTextChangedListener(this);

        //监听EditText的焦点变化
        password.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b == true){
                    // 捂住眼睛
                    close();
                }else {
                    //放开
                    open();
                }
            }
        });

        //登录按钮被点击
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //  登录需要完成操作的内容
                Intent intent = new Intent(MainActivity.this,Main2Activity.class);
                startActivity(intent);
            }
        });
    }

    @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)){
            // 按钮可以点击了
            login.setEnabled(true);
        }else {
            login.setEnabled(false);
        }
    }


    private void close(){
        //旋转动画
        RotateAnimation rAnim = new RotateAnimation(0, 170, Animation.RELATIVE_TO_SELF,1.0f,Animation.RELATIVE_TO_SELF,0f);
        rAnim.setDuration(500);
        rAnim.setFillAfter(true);
        leftArm.startAnimation(rAnim);

        RotateAnimation rAnim1 = new RotateAnimation(0, -170, Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,0f);
        rAnim1.setDuration(500);
        rAnim1.setFillAfter(true);
        rightArm.startAnimation(rAnim1);

        TranslateAnimation tAnim = (TranslateAnimation) AnimationUtils.loadAnimation(this,R.anim.translate_down );
        leftHand.startAnimation(tAnim);
        rightHand.startAnimation(tAnim);
    }

    private void open(){
        //旋转动画
        RotateAnimation rAnim = new RotateAnimation(170, 0, Animation.RELATIVE_TO_SELF,1.0f,Animation.RELATIVE_TO_SELF,0f);
        rAnim.setDuration(500);
        rAnim.setFillAfter(true);
        leftArm.startAnimation(rAnim);

        RotateAnimation rAnim1 = new RotateAnimation(-170, 0, Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,0f);
        rAnim1.setDuration(500);
        rAnim1.setFillAfter(true);
        rightArm.startAnimation(rAnim1);

        TranslateAnimation tAnim = (TranslateAnimation) AnimationUtils.loadAnimation(this,R.anim.translate_up );
        leftHand.startAnimation(tAnim);
        rightHand.startAnimation(tAnim);
    }
}

本文素材:

arm_left.png
arm_right.png
bg.jpeg
bg_blure.png
icon_hand.png
iconfont_password.png
iconfont_user.png
owl_head.png
yans.jpg
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,635评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,628评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,971评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,986评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,006评论 6 394
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,784评论 1 307
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,475评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,364评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,860评论 1 317
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,008评论 3 338
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,152评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,829评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,490评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,035评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,156评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,428评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,127评论 2 356

推荐阅读更多精彩内容

  • 来看一波图片吧 动画效果介绍 1.当用户输入用户名时,小猫头鹰的眼睛是没有被捂住的2.当用户输入密码时,小猫头鹰会...
    xianxun阅读 20,788评论 59 20
  • 【Android 动画】 动画分类补间动画(Tween动画)帧动画(Frame 动画)属性动画(Property ...
    Rtia阅读 6,164评论 1 38
  • 概述 Android把任何可绘制在屏幕上的图形图像都称为drawable 资源,你可以通过类似getDrawabl...
    小芸论阅读 2,722评论 2 5
  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,105评论 1 32
  • 姓名:赵丽娟 公司:丛迪服装有限公司 【日精进打卡第132天】 【知~学习】 《六项精进》大纲0遍,共213遍; ...
    阿诗玛_6209阅读 116评论 0 0