Android自定義滑動(dòng)解鎖控件使用詳解
最近的項(xiàng)目里用到了,在網(wǎng)上找不到合適的,于是自己寫(xiě)了個(gè)簡(jiǎn)單的,帶回彈效果:
可以自定義的屬性有:
<!-- 滑動(dòng)解鎖控件 xml配置屬性 --> <declare-styleable name="SlideToUnlockView"> <attr name="slideImageViewWidth" format="dimension"/><!-- 滑塊寬度 --> <attr name="slideImageViewResId" format="reference"/><!-- 滑塊資源id --> <attr name="slideImageViewResIdAfter" format="reference"/><!-- 滑動(dòng)到右邊時(shí),滑塊資源id --> <attr name="viewBackgroundResId" format="reference"/><!-- 背景資源id --> <attr name="textHint" format="string"/><!-- 文本內(nèi)容 --> <attr name="textSize" format="integer"/><!-- 文本字號(hào) --> <attr name="textColorResId" format="color"/><!-- 文本字色 --> <attr name="slideThreshold" format="float"/><!-- 滑動(dòng)閾值,默認(rèn)是0.5,當(dāng)右滑距離不滿整個(gè)控件寬度的0.5,就會(huì)回彈至左邊 --> </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
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android自定義控件實(shí)現(xiàn)可左右滑動(dòng)的導(dǎo)航條
- Android控件之SlidingDrawer(滑動(dòng)式抽屜)詳解與實(shí)例分享
- Android開(kāi)源堆疊滑動(dòng)控件仿探探效果
- Android自定義控件ScrollView實(shí)現(xiàn)上下滑動(dòng)功能
- Android實(shí)現(xiàn)可滑動(dòng)的自定義日歷控件
- Android控件SeekBar仿淘寶滑動(dòng)驗(yàn)證效果
- Android自定義View實(shí)現(xiàn)隨手勢(shì)滑動(dòng)控件
- Android仿微信列表滑動(dòng)刪除之可滑動(dòng)控件(一)
- Android自定義控件實(shí)現(xiàn)滑動(dòng)開(kāi)關(guān)效果
- Android自定義雙向滑動(dòng)控件
相關(guān)文章
簡(jiǎn)單實(shí)現(xiàn)安卓里百度地圖持續(xù)定位
本文主要介紹了在安卓的百度地圖開(kāi)發(fā)里面簡(jiǎn)單實(shí)現(xiàn)持續(xù)定位的方法,具有一定的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-01-01深入理解Android中的Window和WindowManager
這篇文章給大家介紹了Window和WindowManager知識(shí),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2017-02-02詳解Android(共享元素)轉(zhuǎn)場(chǎng)動(dòng)畫(huà)開(kāi)發(fā)實(shí)踐
本篇文章主要介紹了詳解Android(共享元素)轉(zhuǎn)場(chǎng)動(dòng)畫(huà)開(kāi)發(fā)實(shí)踐,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-08-08Android中AnimationDrawable使用的簡(jiǎn)單實(shí)例
這篇文章介紹了Android中AnimationDrawable使用的簡(jiǎn)單實(shí)例,有需要的朋友可以參考一下2013-10-10Android使用RecyclerView實(shí)現(xiàn)今日頭條頻道管理功能
這篇文章主要為大家詳細(xì)介紹了Android使用RecyclerView實(shí)現(xiàn)今日頭條頻道管理功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07Android編程實(shí)現(xiàn)通話錄音功能的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)通話錄音功能的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android廣播接收機(jī)制實(shí)現(xiàn)錄音功能的操作技巧,需要的朋友可以參考下2017-06-06