Android自定义滑动解锁控件使用详解
程序员文章站
2022-06-04 21:38:39
最近的项目里用到了,在网上找不到合适的,于是自己写了个简单的,带回弹效果:
可以自定义的属性有:
...
最近的项目里用到了,在网上找不到合适的,于是自己写了个简单的,带回弹效果:
可以自定义的属性有:
<!-- 滑动解锁控件 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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。