Android超级好看的动态登陆界面(附有详细代码)
Posted Ken'
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android超级好看的动态登陆界面(附有详细代码)相关的知识,希望对你有一定的参考价值。
首先先演示一下动态的效果
QQ视频20220819164103
从视频效果中,看到这个动态的登陆界面还是比较好看的,下面开始介绍如何制作这个动态的登陆界面。
一、首先书写静态页面
activity_mian.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.motion.widget.MotionLayout
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"
app:layoutDescription="@xml/motion_scene_splash"
tools:context=".MainActivity"
tools:ignore="MissingConstraints">
<ImageView
android:id="@+id/imgView_logo"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_marginTop="80dp"
android:scaleType="centerInside"
android:src="@drawable/icon"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/imgView_logo_rays"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_marginTop="80dp"
android:rotation="0"
android:scaleType="centerInside"
android:src="@drawable/icon2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/imgView_cloudLeft"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_marginTop="16dp"
android:scaleType="centerInside"
android:src="@drawable/cloud"
app:layout_constraintTop_toTopOf="parent"
app:tint="@color/lightGrey" />
<ImageView
android:id="@+id/imgView_cloudRight"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_marginTop="100dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:scaleType="centerInside"
android:src="@drawable/cloud"
app:tint="@color/lightGrey"
/>
<LinearLayout
android:id="@+id/linlay_inputs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:layout_marginLeft="30dp"
android:layout_marginEnd="30dp"
android:layout_marginRight="30dp"
android:gravity="center"
android:layout_marginTop="10dp"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imgView_cloudRight">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-medium"
android:text="Welcome Back"
android:textColor="@color/textHeadline"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif"
android:text="Sign in to continue"
android:textColor="@color/textDescription"
android:textSize="14sp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:background="@drawable/edittext_bg"
android:drawableStart="@drawable/custom_email_icon"
android:drawablePadding="16dp"
android:hint="Email"
android:inputType="textEmailAddress"
android:padding="16dp"
android:textSize="14sp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="@drawable/edittext_bg"
android:drawableStart="@drawable/custom_lock_icon"
android:drawableEnd="@drawable/ic_baseline_visibility_24"
android:drawableRight="@drawable/ic_baseline_visibility_24"
android:drawablePadding="16dp"
android:hint="Password"
android:inputType="textPassword"
android:padding="16dp"
android:textSize="14sp" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:background="@drawable/button_bg"
android:text="Login"
android:textColor="@color/white" />
</LinearLayout>
<TextView
android:id="@+id/tv_forgotPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginEnd="30dp"
android:layout_marginRight="30dp"
android:text="Forgot Password?"
android:textColor="@color/textDescription"
android:textSize="13sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/linlay_inputs" />
<TextView
android:id="@+id/tv_register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Don't have an account?Register Here"
android:textColor="@color/textHeadline"
android:textSize="13sp"
android:gravity="center"
android:layout_marginBottom="30dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
/>
</androidx.constraintlayout.motion.widget.MotionLayout>
这里有一点值得说一下,我们使用MotionLayout这个布局来实现动画效果,这个布局平时不太常见,它其实是属于ConstraintLayout的子类,所以它是一种布局类型,但是它能够为布局属性添加动画效果,是开发者实现动画效果的另一个新的选择。
目前所实现的静态效果如下:
接下来会通过资源文件来实现动态效果
二、实现动态效果
1、在res目录下新建xml文件资源目录
2、在xml目录下新建xml资源文件(motion_scene_splash.xml)
<?xml version="1.0" encoding="utf-8"?>
<MotionScene xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:motion="http://schemas.android.com/apk/res-auto">
<Transition
motion:autoTransition="animateToEnd"
motion:constraintSetEnd="@layout/activity_login"
motion:constraintSetStart="@layout/activity_splash"
motion:duration="5000">
<KeyFrameSet>
<KeyPosition
motion:framePosition="65"
motion:keyPositionType="deltaRelative"
motion:motionTarget="@+id/imgView_logo"
motion:percentX="0"
motion:percentY="0" />
<KeyPosition
motion:framePosition="65"
motion:keyPositionType="deltaRelative"
motion:motionTarget="@+id/imgView_logo_rays"
motion:percentX="0"
motion:percentY="0" />
<KeyPosition
motion:framePosition="75"
motion:keyPositionType="deltaRelative"
motion:motionTarget="@+id/imgView_logo"
motion:percentX="1"
motion:percentY="1" />
<KeyPosition
motion:framePosition="75"
motion:keyPositionType="deltaRelative"
motion:motionTarget="@+id/imgView_logo_rays"
motion:percentX="1"
motion:percentY="1" />
<KeyAttribute
android:rotation="0"
motion:framePosition="0"
motion:motionTarget="@+id/imgView_logo_rays" />
<KeyAttribute
android:rotation="180"
motion:framePosition="85"
motion:motionTarget="@+id/imgView_logo_rays" />
<KeyPosition
motion:framePosition="75"
motion:keyPositionType="deltaRelative"
motion:motionTarget="@+id/imgView_cloudLeft"
motion:percentX="0"
motion:percentY="0" />
<KeyPosition
motion:framePosition="75"
motion:keyPositionType="deltaRelative"
motion:motionTarget="@+id/imgView_cloudRight"
motion:percentX="0"
motion:percentY="0" />
<KeyPosition
motion:framePosition="85"
motion:keyPositionType="deltaRelative"
motion:motionTarget="@+id/imgView_cloudLeft"
motion:percentX="1"
motion:percentY="1" />
<KeyPosition
motion:framePosition="85"
motion:keyPositionType="deltaRelative"
motion:motionTarget="@+id/imgView_cloudRight"
motion:percentX="1"
motion:percentY="1" />
<KeyAttribute
motion:motionTarget="@+id/linlay_inputs"
android:alpha="0"
motion:framePosition="75"/>
<KeyAttribute
motion:motionTarget="@+id/linlay_inputs"
android:alpha="1"
motion:framePosition="85"/>
<KeyAttribute
motion:motionTarget="@+id/tv_forgotPassword"
android:alpha="0"
motion:framePosition="75"/>
<KeyAttribute
motion:motionTarget="@+id/tv_forgotPassword"
android:alpha="1"
motion:framePosition="85"/>
<KeyPosition
motion:framePosition="75"
motion:percentY="0"
motion:percentX="0"
motion:motionTarget="@+id/tv_register"/>
<KeyPosition
motion:framePosition="85"
motion:percentY="1"
motion:percentX="1"
motion:motionTarget="@+id/tv_register"/>
</KeyFrameSet>
</Transition>
</MotionScene>
3、需要用到的全部资源文件
activity_login.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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"
tools:ignore="MissingConstraints">
<ImageView
android:id="@+id/imgView_logo"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_marginTop="80dp"
android:scaleType="centerInside"
android:src="@drawable/icon"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/imgView_logo_rays"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_marginTop="80dp"
android:rotation="0"
android:scaleType="centerInside"
android:src="@drawable/icon2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/imgView_cloudLeft"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_marginTop="16dp"
android:scaleType="centerInside"
android:src="@drawable/cloud"
app:layout_constraintTop_toTopOf="parent"
app:tint="@color/lightGrey" />
<ImageView
android:id="@+id/imgView_cloudRight"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_marginTop="100dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:scaleType="centerInside"
android:src="@drawable/cloud"
app:tint="@color/lightGrey"
/>
<LinearLayout
android:id="@+id/linlay_inputs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:layout_marginLeft="30dp"
android:layout_marginEnd="30dp"
android:layout_marginRight="30dp"
android:gravity="center"
android:layout_marginTop="10dp"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imgView_cloudRight">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-medium"
android:text="Welcome Back"
android:textColor="@color/textHeadline"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif"
android:text="Sign in to continue"
android:textColor="@color/textDescription"
android:textSize="14sp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:background="@drawable/edittext_bg"
android:drawableStart="@drawable/ic_baseline_email_24"
android:drawableLeft="@drawable/ic_baseline_email_24"
android:drawablePadding="16dp"
android:hint="Email"
android:inputType="textEmailAddress"
android:padding="16dp"
android:textSize="14sp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="@drawable/edittext_bg"
android:drawableStart="@drawable/ic_baseline_lock_24"
android:drawableLeft="@drawable/ic_baseline_lock_24"
android:drawableEnd="@drawable/ic_baseline_visibility_24"
android:drawableRight="@drawable/ic_baseline_visibility_24"
android:drawablePadding="16dp"
android:hint="Password"
android:inputType="textPassword"
android:padding="16dp"
android:textSize="14sp" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:background="@drawable/button_bg"
android:text="Login"
android:textColor="@color/white" />
</LinearLayout>
<TextView
android:id="@+id/tv_forgotPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginEnd="30dp"
android:layout_marginRight="30dp"
android:text="Forgot Password?"
android:textColor="@color/textDescription"
android:textSize="13sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/linlay_inputs" />
<TextView
android:id="@+id/tv_register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Don't have an account?Register Here"
android:textColor="@color/textHeadline"
android:textSize="13sp"
android:gravity="center"
android:layout_marginBottom="30dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
activity_splash.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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"
tools:ignore="MissingConstraints">
<ImageView
android:id="@+id/imgView_logo"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_marginTop="80dp"
android:scaleType="centerInside"
android:src="@drawable/icon"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/imgView_logo_rays"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_marginTop="80dp"
android:rotation="0"
android:scaleType="centerInside"
android:src="@drawable/icon2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/imgView_cloudLeft"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_marginTop="16dp"
android:scaleType="centerInside"
android:src="@drawable/cloud"
app:layout_constraintTop_toTopOf="parent"
app:tint="@color/lightGrey" />
<ImageView
android:id="@+id/imgView_cloudRight"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_marginTop="100dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:scaleType="centerInside"
android:src="@drawable/cloud"
app:tint="@color/lightGrey"
/>
<LinearLayout
android:id="@+id/linlay_inputs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:layout_marginLeft="30dp"
android:layout_marginEnd="30dp"
android:layout_marginRight="30dp"
android:gravity="center"
android:layout_marginTop="10dp"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imgView_cloudRight">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-medium"
android:text="Welcome Back"
android:textColor="@color/textHeadline"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif"
android:text="Sign in to continue"
android:textColor="@color/textDescription"
android:textSize="14sp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:background="@drawable/edittext_bg"
android:drawableStart="@drawable/ic_baseline_email_24"
android:drawableLeft="@drawable/ic_baseline_email_24"
android:drawablePadding="16dp"
android:hint="Email"
android:inputType="textEmailAddress"
android:padding="16dp"
android:textSize="14sp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="@drawable/edittext_bg"
android:drawableStart="@drawable/ic_baseline_lock_24"
android:drawableLeft="@drawable/ic_baseline_lock_24"
android:drawableEnd="@drawable/ic_baseline_visibility_24"
android:drawableRight="@drawable/ic_baseline_visibility_24"
android:drawablePadding="16dp"
android:hint="Password"
android:inputType="textPassword"
android:padding="16dp"
android:textSize="14sp" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:background="@drawable/button_bg"
android:text="Login"
android:textColor="@color/white" />
</LinearLayout>
<TextView
android:id="@+id/tv_forgotPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginEnd="30dp"
android:layout_marginRight="30dp"
android:text="Forgot Password?"
android:textColor="@color/textDescription"
android:textSize="13sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/linlay_inputs" />
<TextView
android:id="@+id/tv_register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Don't have an account?Register Here"
android:textColor="@color/textHeadline"
android:textSize="13sp"
android:gravity="center"
android:layout_marginBottom="30dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
button_bg.xml (按钮背景)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/colorPrimary" />
<corners android:radius="10dp" />
</shape>
给按钮设置了颜色属性和圆角属性,让按钮更加好看圆润。
edittext_bg.xml(输入框背景)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/lightGrey" />
<corners android:radius="10dp" />
</shape>
用到的图片资源如下:
想要项目源码的可以收藏点赞,然后私信我即可!!!,私信必回!!!
基于selenium实现12306模拟登陆
这里介绍一款强大验证码识别平台:超级鹰
- 超级鹰:http://www.chaojiying.com/about.html
- 注册:普通用户
- 登录:普通用户
- 题分查询:充值
- 创建一个软件(id)
- 下载示例代码
1. 打开url
一打开是扫码登陆的页面,这显然不是我们想要的,因此我们应该先打开账号登陆的界面。
2. 打开账号登陆界面
3.利用超级鹰对当前验证码进行识别
4.思路:模拟登录的思路是很简单的,难点在于验证码的识别与正确选项的定位,如何让浏览器自行选择正确的验证码是我们需要解决的最大问题。
from selenium import webdriver import time from PIL import Image from selenium.webdriver import ActionChains import requests from hashlib import md5 from selenium.webdriver import Chrome from selenium.webdriver import ChromeOptions class Chaojiying_Client(object): """超级鹰源代码""" def __init__(self, username, password, soft_id): self.username = username password = password.encode(‘utf8‘) self.password = md5(password).hexdigest() self.soft_id = soft_id self.base_params = { ‘user‘: self.username, ‘pass2‘: self.password, ‘softid‘: self.soft_id, } self.headers = { ‘Connection‘: ‘Keep-Alive‘, ‘User-Agent‘: ‘Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)‘, } def PostPic(self, im, codetype): """ im: 图片字节 codetype: 题目类型 参考 http://www.chaojiying.com/price.html """ params = { ‘codetype‘: codetype, } params.update(self.base_params) files = {‘userfile‘: (‘ccc.jpg‘, im)} r = requests.post(‘http://upload.chaojiying.net/Upload/Processing.php‘, data=params, files=files, headers=self.headers) return r.json() def ReportError(self, im_id): """ im_id:报错题目的图片ID """ params = { ‘id‘: im_id, } params.update(self.base_params) r = requests.post(‘http://upload.chaojiying.net/Upload/ReportError.php‘, data=params, headers=self.headers) return r.json() # 检测规避 option = ChromeOptions() option.add_experimental_option(‘excludeSwitches‘, [‘enable-automation‘]) driver = Chrome(options=option) bro = webdriver.Chrome(executable_path=‘./chromedriver.exe‘) # 最大化屏幕 bro.maximize_window() # 发送请求 bro.get(‘https://kyfw.12306.cn/otn/resources/login.html‘) # 找到账号的登陆的标签 btn = bro.find_elements_by_xpath(‘/html/body/div[2]/div[2]/ul/li[2]/a‘)[0] btn.click() time.sleep(2) # 将当前页面截屏生成图片 bro.save_screenshot(‘12306.png‘) # 定位到要截取的图片 pic = bro.find_element_by_xpath(‘/html/body/div[2]/div[2]/div[1]/div[2]/div[3]/div/div[4]/img‘) # 得到当前图片的左上角坐标 location = pic.location # 得到图片的长和宽 size = pic.size # 得到图片左上角和右下角的坐标 截图准确度与电脑缩放布局有关 当前为125% rangle = (location[‘x‘] * 1.25, location[‘y‘] * 1.25, (location[‘x‘] + size[‘width‘]) * 1.25, (location[‘y‘] + size[‘height‘]) * 1.25) # 保存生成的验证码图片 i = Image.open(‘./12306.png‘) code_img_name = ‘code.png‘ # 裁剪文件的文件名称 frame = i.crop(rangle) # 根据指定区域进行裁剪 frame.save(code_img_name) # 利用超级鹰识别验证码 chaojiying = Chaojiying_Client(‘用户名‘, ‘密码‘, ‘ 905993‘) im = open(‘./code.png‘, ‘rb‘).read() result = chaojiying.PostPic(im, 9004)[‘pic_str‘] all_list = [] if ‘|‘ in result: # 多个结果 print(result) ret_list = result.split(‘|‘) # 148,102|236,203 for ret in ret_list: # 将一组坐标放入列表 x_y = [] x = ret.split(‘,‘)[0] y = ret.split(‘,‘)[0] x_y.append(x) x_y.append(y) all_list.append(x_y) else: x_y = [] x = result.split(‘,‘)[0] y = result.split(‘,‘)[0] x_y.append(x) x_y.append(y) all_list.append(x_y) for ret in all_list: x = int(ret[0]) y = int(ret[1]) ActionChains(bro).move_to_element_with_offset(pic, x, y).click().perform() # 以图片对象为参考系 将鼠标移动到相对图片x,y处 点击图片 time.sleep(0.5) bro.find_element_by_id(‘J-userName‘).send_keys(‘xxxx‘) bro.find_element_by_id(‘J-password‘).send_keys(‘ssss‘) bro.find_element_by_id(‘J-login‘).click() time.sleep(3) bro.quit()
以上是关于Android超级好看的动态登陆界面(附有详细代码)的主要内容,如果未能解决你的问题,请参考以下文章