Android自定义滑动解锁控件使用详解

 更新时间:2020年04月05日 06:12:40   作者:lp506954774  
这篇文章主要为大家详细介绍了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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Android编程设计模式之工厂方法模式实例详解

    Android编程设计模式之工厂方法模式实例详解

    这篇文章主要介绍了Android编程设计模式之工厂方法模式,结合实例形式详细分析了Android工厂方法模式的概念、原理、使用方法及相关注意事项,需要的朋友可以参考下
    2017-12-12
  • Android自定义ListView实现下拉刷新

    Android自定义ListView实现下拉刷新

    这篇文章主要为大家详细介绍了Android自定义ListView实现下拉刷新的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-07-07
  • Android 开发程序锁应用简单实例

    Android 开发程序锁应用简单实例

    这篇文章主要介绍了Android 开发程序锁应用简单实例的相关资料,需要的朋友可以参考下
    2016-10-10
  • Android scrollToTop实现点击回到顶部(兼容PullTorefreshScrollview)

    Android scrollToTop实现点击回到顶部(兼容PullTorefreshScrollview)

    当页面滑动到底部,出现回到顶部的按钮相信对大家来说并不陌生,下面这篇文章主要介绍了关于Android scrollToTop实现点击回到顶部,并兼容PullTorefreshScrollview的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴。
    2017-03-03
  • Android6.0 Launcher2应用解析

    Android6.0 Launcher2应用解析

    这篇文章主要为大家详细介绍了Android6.0 Launcher2应用,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-09-09
  • Android-SPI学习笔记

    Android-SPI学习笔记

    这篇文章主要介绍了Android-SPI的相关资料,帮助大家更好的理解和学习使用Android,感兴趣的朋友可以了解下
    2021-02-02
  • Android zip文件下载和解压实例

    Android zip文件下载和解压实例

    这篇文章主要介绍了Android zip文件下载和解压实例,有需要的朋友可以参考一下
    2014-01-01
  • Android利用POI实现将图片插入到Excel

    Android利用POI实现将图片插入到Excel

    这篇文章主要为大家详细介绍了Android如何利用POI实现将图片插入到Excel,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2023-11-11
  • Android编程自定义title bar(标题栏)示例

    Android编程自定义title bar(标题栏)示例

    这篇文章主要介绍了Android编程自定义title bar(标题栏)的方法,结合实例形式分析了Android针对标题栏的设置与页面布局操作相关技巧,需要的朋友可以参考下
    2016-10-10
  • 详解adb工具的基本使用

    详解adb工具的基本使用

    adb全称Android Debug Bridge,是Android SDK中的一个工具, 使用adb可以直接操作管理Android模拟器或者真实的Andriod设备,就是起到调试桥的作用,这篇文章主要介绍了adb工具的基本使用,需要的朋友可以参考下
    2022-08-08

最新评论