Android自定义滑动解锁控件使用详解
作者:lp506954774 发布时间:2022-08-20 05:36:38
标签:Android,滑动解锁
最近的项目里用到了,在网上找不到合适的,于是自己写了个简单的,带回弹效果:
可以自定义的属性有:
<!-- 滑动解锁控件 xml配置属性 -->
<declare-styleable name="SlideToUnlockView">
<attr name="slideImageViewWidth" format="dimension"/><!-- 滑块宽度 -->
<attr name="slideImageViewResId" format="reference"/><!-- 滑块资源id -->
<attr name="slideImageViewResIdAfter" format="reference"/><!-- 滑动到右边时,滑块资源id -->
<attr name="viewBackgroundResId" format="reference"/><!-- 背景资源id -->
<attr name="textHint" format="string"/><!-- 文本内容 -->
<attr name="textSize" format="integer"/><!-- 文本字号 -->
<attr name="textColorResId" format="color"/><!-- 文本字色 -->
<attr name="slideThreshold" format="float"/><!-- 滑动阈值,默认是0.5,当右滑距离不满整个控件宽度的0.5,就会回弹至左边 -->
</declare-styleable>
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:chuck="http://schemas.android.com/apk/res-auto"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.qdong.slidetounlockdemo.MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/relativeLayout">
<!-- chuck:textSize="14sp"
chuck:textColorResId="@color/colorWhite"-->
<com.qdong.slide_to_unlock_view.CustomSlideToUnlockView
android:id="@+id/slide_to_unlock"
android:layout_width="match_parent"
android:layout_height="50dp"
chuck:viewBackgroundResId="@drawable/shape_round_normal_green"
chuck:slideImageViewWidth="@dimen/slide_width"
chuck:slideImageViewResId="@mipmap/icon_slide"
chuck:slideImageViewResIdAfter="@mipmap/ic_launcher"
chuck:slideThreshold="0.5"
chuck:textSize="6"
chuck:textHint="@string/hint"
chuck:textColorResId="@color/colorWhite"
>
</com.qdong.slide_to_unlock_view.CustomSlideToUnlockView>
</RelativeLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="reset"
android:id="@+id/button"
android:layout_below="@+id/relativeLayout"
android:layout_centerHorizontal="true"
android:layout_marginTop="150dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_text"
android:text="slide distance:"
android:layout_alignBottom="@+id/button"
android:layout_centerHorizontal="true"
android:layout_marginBottom="60dp"/>
</RelativeLayout>
MainActivity:
public class MainActivity extends AppCompatActivity {
private com.qdong.slide_to_unlock_view.CustomSlideToUnlockView mCustomSlideToUnlockView;
private TextView tv_text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mCustomSlideToUnlockView= (com.qdong.slide_to_unlock_view.CustomSlideToUnlockView) findViewById(R.id.slide_to_unlock);
tv_text= (TextView) findViewById(R.id.tv_text);
CustomSlideToUnlockView.CallBack callBack=new CustomSlideToUnlockView.CallBack() {
@Override
public void onSlide(int distance) {
tv_text.setText("slide distance:"+distance);
}
@Override
public void onUnlocked() {
tv_text.setText("onUnlocked");
}
};
mCustomSlideToUnlockView.setmCallBack(callBack);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mCustomSlideToUnlockView.resetView();
}
});
}
}
下载地址:
https://github.com/506954774/AndroidCustomSlideToUnlockView


猜你喜欢
- Java事件处理机制java中的事件机制的参与者有3种角色:1.event object:事件状态对象,用于listener的相应的方法之中
- 上一节我们了解了Lock接口的一些简单的说明,知道Lock锁的常用形式,那么这节我们正式开始进入JUC锁(java.util.concurr
- 1. 确保你项目能编译通过,安装java jdk 环境填写环境变量2. 添加SpringBootServletInitializer的子类重
- 最近.一个朋友跟我说想,我给她弄个闹钟APP软件...功能其实很简单...只需要弄个简单的闹钟.自己设计设计时间.然后时间到了的时候,闹铃放
- 对于获取了一大堆字符串但是又不想要里面的html标签怎么办?特别是像博客园这个富文本框中,可以带样式的,取出来的文章内容也是带样式的。但是在
- 前段时间因为看JUC的源码,里面有大量关于unsafe的操作,所以就来看看了.写点笔记总结下(本文基于jdk1.8):unsafe可以帮我们
- 上一篇介绍了Tesseract库的使用(OCR库Tesseract初探),文末提到了Tesseract是用c/c++开发的,也有C#的开源版
- 1、抽象类1.1 什么是抽象类?1.1.1 对抽象类的理解1.1.2 关于抽象类类与类之间具有共同特征,将这些共同特征提取出来,形成的就是抽
- springboot对压缩请求的处理最近对接银联需求,为了节省带宽,需要对报文进行压缩处理。但是使用springboot自带的压缩设置不起作
- 1: .net framework 由两个部分组成:CLR 和 FCL。2:在CLR中,所有错误都是通过异常来报告的。3:智能感知功能主要是
- 正文:相关术语翻译说明:Mark,标记;Sweep,清除;Compact,整理; 也有人翻译为压缩,译者认为GC时不存在压缩这回事。Copy
- C#实现多态主要有3种方法,虚方法,抽象类,接口1 虚方法在父类的方法前面加关键字virtual, 子类重写该方法时在方法名前面加上over
- 自定义View是绘制文本有三类方法// 第一类public void drawText (String text, float x, flo
- 话不多说,上代码: /** * 获取字符串的长度,如果有中文,则每个中文字符计为2位 * @param value
- 大家好,这是 [C#.NET 拾遗补漏] 系列的第 08 篇文章,今天讲 C# 强大的 LINQ 查询。LINQ 是我最喜欢的 C# 语言特
- 本文实例讲述了Android编程入门之HelloWorld项目目录结构。分享给大家供大家参考,具体如下:我们介绍了如何搭建Android开发
- 问题描述:某天打开项目的activity的java文件界面突然变成下面这样了,但是用Notepad++打开代码什么的都正常,不知道什么原因造
- 实例如下:func exitApplication() { let app = UIApplication.sh
- 本文以实例详述了C#实现Socket通信的解决方法,具体实现步骤如下:1、首先打开VS新建两个控制台应用程序:ConsoleApplicat
- 一.先说结论针对任何一个代码记录都进行Revert Commit操作:①不管此记录是commit未push,还是已经push过;②不管此记录