我的视频系列 http://edu.csdn.net/course/detail/2741,一起来学习Android…
代码参考地址 https://github.com/liuzhiyuan0932/SlideUnLock代码效果图>
自定义滑动解锁的控件继承自View
public class SlideUnlockView extends View
自定义SlideUnLockView的属性
- 在values文件夹中定义属性
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="SlideUnlockButton">
<!-- 背景图片的属性, 引用的是R.drawable.xx. -->
<attr name="slideUnlockBackgroundResource" format="reference" />
<!-- 滑动块图片的属性, 引用的是R.drawable.xx. -->
<attr name="slideUnlockBlockResource" format="reference" />
</declare-styleable>
</resources>
- 在xml布局中使用相关的属性
<com.zhiyuan.slideunlockdemo.view.SlideUnlockView
android:id="@+id/slideUnlockView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
test:slideUnlockBackgroundResource="@drawable/jiesuo_bg"
test:slideUnlockBlockResource="@drawable/jiesuo_button" />
定义滑块的几种状态
/**
* 滑块当前的状态
*/
public int currentState;
/**
* 未解锁
*/
public static final int STATE_LOCK = 1;
/**
* 解锁
*/
public static final int STATE_UNLOCK = 2;
/**
* 正在拖拽
*/
public static final int STATE_MOVING = 3;
获取图片资源,并进行初始绘制
- 在构造方法中获取属性值
public SlideUnlockView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// 默认滑动解锁为未解锁状态
currentState = STATE_