SwitchView(滑动按钮)
最近写了个滑动按钮,分享给大家,写的不太好,多多包涵。有问题联系我 QQ157688302
最底下提供下载
废话不多说,先上效果图:
先介绍几个特性:
1. 显示的文字可以自己配置
2. 点击控件会自动更改状态(可以通过closeclickevent 函数关闭此功能)。
3. 拖动开关可以更改状态
4. 可通过OnSwitchListener获取事件更改.
5. 拖动开关至边界处,会产生抖动效果.
缺点:
1. 开关的背景图可以自己更换,本人美工不好,图片不是很好看.
2. 代码编写并不是很规范,不好之处还请多多体谅.
3. 没有禁止选择的函数(以后考虑添加)
使用方法:
1. 可以在布局中使用,如果在布局中使用,请务必不要写成wrap_content. 可以自己指定大小或者fill_parent( or match).
xml 实例:
[html] <com.xxx.SwitchView
android:id="@+id/switch_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_margin="10dp"
/>
<com.xxx.SwitchView
android:id="@+id/switch_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_margin="10dp"
/>
好了,其他废话不多说,上代码:
[java] package com.clw.signalenhancement;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
/**
* 滑动按钮
*
* @author Chaos(Thanks chenwen3k to help solve a serious problem)
* @date 2013-05-25
*/
public class SwitchView extends FrameLayout {
/**
* LeftSwitch
*/
private static final String TEXT_LEFT_SWITCH = "开";
/**
* RightSwitch
*/
private static final String TEXT_RIGHT_SWITCH = "关";
/**
* TYPE - LEFT
*/
public static final int TYPE_LEFT = 0;
/**
* TYPE - Right
*/
public static final int TYPE_RIGHT = 1;
/**
* The best width
*/
private static final int MAX_WIDTH = 300;
/**
* The best height
*/
private static final int MAX_HEIGHT = 80;
/**
* In order to trick your eyes
*/
private static final int FRAMES = 15;
/**
* Used to describe the degree of shaking
*/
private static final int ARGUMENT_SHAKE = 2;
/**
* The offset for the upper view
*/
private int mOffsetX = 0;
/**
* Total width of the floating layer (but I think that may not be required)
*/
private int mFloatingLayerWidth;
/**
* The total width of the view (but I think that may not be required)
*/
private int mTotalWidth;
/**
* Description of the currently checked type
*/
private int mNowType = TYPE_LEFT;
/**
* In order to listen for checked events
*/
private OnSwitchListener mSwitchListener;
/**
* Necessary object
*/
private TextView mLeftSwitch;
private TextView mRightSwitch;
private ImageView mFloatingLayer;
private LinearLayout mSwitchParent;
/**
* Touchevent interceptor
*/
private GestureDetector mDetector = null;
/**
* The click switch event flag
*/
private boolean isOpenClick = true;
public SwitchView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public SwitchView(Context context) {
super(context);
init();
}
private void init() {
// build parent
mSwitchParent = new LinearLayout(getContext());
// Ready to work
mSwitchParent.setOrientation(LinearLayout.HORIZONTAL);
setBackgroundResource(R.drawable.usage_list_dark);
// step.1: add floating layer in bottom(maybe called 'sediment layer')
补充:移动开发 , Android ,