SwitchButton
SwitchButton???Android???????????Android2.1
??APP
??
- ??????
- ????
- ??Left?Top?Right?Bottom Drawable
- ????????????
?????Usage Guide?
??
?JCenter??
dependencies{
compile 'me.xiaopan:switchbutton:lastVersionName'
}
???? Android2.2 API 7
????
switch_frame.png
switch_state_normal.png
switch_state_disable.png
switch_state_mask.png
switch_slider_normal.png
switch_slider_disable.png
selector_switch_state.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_enabled="false" android:drawable="@drawable/switch_state_disable"/>
<item android:drawable="@drawable/switch_state_normal"/> </selector>
selector_switch_slider.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_enabled="false" android:drawable="@drawable/switch_slider_disable"/>
<item android:drawable="@drawable/switch_slider_normal"/> </selector>
??????SwitchButton????????????
<?xml version="1.0" encoding="utf-8"?> <me.xiaopan.switchbutton.SwitchButton
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/switch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="50dp"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:text="????"
app:withTextInterval="16dp"
app:frameDrawable="@drawable/switch_frame"
app:stateDrawable="@drawable/selector_switch_state"
app:stateMaskDrawable="@drawable/switch_state_mask"
app:sliderDrawable="@drawable/selector_switch_slider"/>
????
- withTextInterval?????????????
- frameDrawable???????????????????
- stateDrawable?????????????
- stateMaskDrawable????????
- sliderDrawable?????
?????
- setDrawables(Drawable frameBitmap, Drawable stateDrawable, Drawable stateMaskDrawable, Drawable sliderDrawable)?????
- setDrawableResIds(int frameDrawableResId, int stateDrawableResId, int stateMaskDrawableResId, int sliderDrawableResId)?????ID
- setWithTextInterval(int withTextInterval)???????????????16
- setDuration(int duration)??????????????????200
- setMinChangeDistanceScale(float minChangeDistanceScale)???????????????0.2????????100????0.2??????????????(100*0.2)????????????
??????
<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"> <LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="16dp"
android:paddingBottom="16dp">
<me.xiaopan.switchbutton.SwitchButton
style="@style/item"
android:id="@+id/switch_main_1"
android:text="????"
android:checked="true"
android:background="@drawable/selector_preference_complete"
app:withTextInterval="16dp"
app:frameDrawable="@drawable/switch_frame"
app:stateDrawable="@drawable/selector_switch_state"
app:stateMaskDrawable="@drawable/switch_state_mask"
app:sliderDrawable="@drawable/selector_switch_slider"/>
<me.xiaopan.switchbutton.SwitchButton
style="@style/item"
android:id="@+id/switch_main_2"
android:text="????"
android:background="@drawable/selector_preference_header"
android:layout_marginTop="16dp"
app:withTextInterval="16dp"
app:frameDrawable="@drawable/switch_frame"
app:stateDrawable="@drawable/selector_switch_state"
app:stateMaskDrawable="@drawable/switch_state_mask"
app:sliderDrawable="@drawable/selector_switch_slider"/>
<me.xiaopan.switchbutton.SwitchButton
style="@style/item"
android:id="@+id/switch_main_3"
android:text="????"
android:background="@drawable/selector_preference_center"
app:withTextInterval="16dp"
app:frameDrawable="@drawable/switch_frame"
app:stateDrawable="@drawable/selector_switch_state"
app:stateMaskDrawable="@drawable/switch_state_mask"
app:sliderDrawable="@drawable/selector_switch_slider"/>
<me.xiaopan.switchbutton.SwitchButton
style="@style/item"
android:id="@+id/switch_main_4"
android:text="????"
android:background="@drawable/selector_preference_center"
app:withTextInterval="16dp"
app:frameDrawable="@drawable/switch_frame"
app:stateDrawable="@drawable/selector_switch_state"
app:stateMaskDrawable="@drawable/switch_state_mask"
app:sliderDrawable="@drawable/selector_switch_slider"/>
<me.xiaopan.switchbutton.SwitchButton
style="@style/item"
android:id="@+id/switch_main_5"
android:text="????"
android:background="@drawable/selector_preference_center"
app:withTextInterval="16dp"
app:frameDrawable="@drawable/switch_frame"
app:stateDrawable="@drawable/selector_switch_state"
app:stateMaskDrawable="@drawable/switch_state_mask"
app:sliderDrawable="@drawable/selector_switch_slider"/>
<me.xiaopan.switchbutton.SwitchButton
style="@style/item"
android:id="@+id/switch_main_6"
android:text="????"
android:background="@drawable/selector_preference_footer"
app:withTextInterval="16dp"
app:frameDrawable="@drawable/switch_frame"
app:stateDrawable="@drawable/selector_switch_state"
app:stateMaskDrawable="@drawable/switch_state_mask"
app:sliderDrawable="@drawable/selector_switch_slider"/>
<me.xiaopan.switchbutton.SwitchButton
style="@style/item"
android:id="@+id/switch_main_7"
android:text="????"
android:background="@drawable/selector_preference_header"
android:layout_marginTop="16dp"
app:withTextInterval="16dp"
app:frameDrawable="@drawable/switch_frame"
app:stateDrawable="@drawable/selector_switch_state"
app:stateMaskDrawable="@drawable/switch_state_mask"
app:sliderDrawable="@drawable/selector_switch_slider"/>
<me.xiaopan.switchbutton.SwitchButton
style="@style/item"
android:id="@+id/switch_main_8"
android:text="????"
android:background="@drawable/selector_preference_footer"
app:withTextInterval="16dp"
app:frameDrawable="@drawable/switch_frame"
app:stateDrawable="@drawable/selector_switch_state"
app:stateMaskDrawable="@drawable/switch_state_mask"
app:sliderDrawable="@drawable/selector_switch_slider"/> </LinearLayout> </ScrollView>
License
/* * Copyright (C) 2013 Peng fei Pan <[email protected]> * * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * You may obtain a copy of the License at * *
http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */