Android UI設(shè)計系列之自定義SwitchButton開關(guān)實現(xiàn)類似IOS中UISwitch的動畫效果(2)
做IOS開發(fā)的都知道,IOS提供了一個具有動態(tài)開關(guān)效果的UISwitch組件,這個組件很好用效果相對來說也很絢麗,當(dāng)我們?nèi)c擊開關(guān)的時候有動畫效果,但遺憾的是Android上并沒有給我們提供類似的組件(聽說在Android4.0的版本上提供了具有動態(tài)效果的開關(guān)組件,不過我還沒有去看文檔),如果我們想實現(xiàn)類似的效果那該怎么辦了呢?看來又得去自定義了。
公司的產(chǎn)品最近一直在做升級,主要做的就是把界面做的更絢麗更美觀給用戶更好的體驗(唉,顧客是上帝......),其中的設(shè)置功能中就有開關(guān)按鈕,原來的開關(guān)做的是兩幅圖片,通過點擊圖片來給開關(guān)設(shè)置不同的狀態(tài)圖片,但是這種效果很死板和程序的整體風(fēng)格不太協(xié)調(diào),于是就想著實現(xiàn)類似于IOS中的開關(guān)效果。
拿著筆在圖紙上畫了畫,我實現(xiàn)的原理也是采用了兩幅圖片,一個整體的背景圖:和一個小滑塊圖:,用小滑塊圖實現(xiàn)在背景圖上滑動,當(dāng)小滑塊滑到左邊時恰好遮擋了開字,就是顯示的關(guān)了,同樣原理當(dāng)小滑塊滑動到右側(cè)時恰好遮擋了關(guān)字也就是現(xiàn)實開了,滑動的實現(xiàn)主要用的就是TranslateAnimation類,如有對TranslateAnimation不太熟悉的,問問度娘,她那有關(guān)TranslateAnimation的解說多了去了,畢竟自己動手,豐衣食足嘛,(*^__^*) 嘻嘻……
好了,老規(guī)矩來看一下項目結(jié)構(gòu)吧:

工程中switch_button.xml文件就是對應(yīng)的SwitchButton的布局文件,內(nèi)容不需要解釋,你一看就懂
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/switch_parent" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/switch_bg"> <ImageView android:id="@+id/switch_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/switch_btn" /> </LinearLayout>
SwitchButton的布局文件中根節(jié)點是個LinearLayout,把它的背景設(shè)置成了一個含有開關(guān)文字的圖片,里邊包含一個ImageView,這個ImageView就是用來在LinearLayout中進(jìn)行滑動的。
其中自定義開關(guān)組件就是都在wedgit包下的SwitchButton,那么趕緊來看一下SwitchButton的代碼吧
public class SwitchButton extends LinearLayout {
/**
* 開關(guān)圖片
*/
private LinearLayout switchParent;
/**
* 滑塊圖片
*/
private ImageView switchButton;
/**
* 按鈕狀態(tài),默認(rèn)關(guān)閉
*/
private boolean isOn = false;
/**
* 滑塊需要滑動的距離
*/
private int scrollDistance;
/**
* 開關(guān)按鈕監(jiān)聽器
*/
private SwitchChangedListner listner;
public SwitchButton(Context context) {
super(context);
initWedgits(context);
}
public SwitchButton(Context context, AttributeSet attrs) {
super(context, attrs);
initWedgits(context);
}
/**
* 初始化組件
*
* @param context
* 上下文環(huán)境
*/
private void initWedgits(Context context) {
try {
View view = LayoutInflater.from(context).inflate(
R.layout.switch_button, this);
switchParent = (LinearLayout) view.findViewById(R.id.switch_parent);
switchButton = (ImageView) view.findViewById(R.id.switch_button);
addListeners();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 添加事件監(jiān)聽器
*/
private void addListeners() {
try {
switchParent.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
isOn = !isOn;
scrollSwitch();
if (null != listner) {
// 開關(guān)開發(fā)或者關(guān)閉的回調(diào)方法
listner.switchChanged(getId(), isOn);
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 滑動開關(guān)
*/
private void scrollSwitch() {
// 獲取滑塊需要滑動的距離,滑動距離等于父組建的寬度減去滑塊的寬度
scrollDistance = switchParent.getWidth() - switchButton.getWidth();
// 初始化滑動事件
Animation animation = null;
if (isOn) {
animation = new TranslateAnimation(0, scrollDistance, 0, 0);
} else {
animation = new TranslateAnimation(scrollDistance, 0, 0, 0);
}
// 設(shè)置滑動時間
animation.setDuration(200);
// 滑動之后保持狀態(tài)
animation.setFillAfter(true);
// 開始滑動
switchButton.startAnimation(animation);
}
/**
* 獲取開關(guān)狀態(tài)
*
* @return 【true:打開】【false:關(guān)閉】
*/
public boolean isOn() {
return isOn;
}
/**
* 設(shè)置開關(guān)狀態(tài)
*
* @param isOn
* 開關(guān)狀態(tài)【true:打開】【false:關(guān)閉】
*/
public void setOn(boolean isOn) {
if (this.isOn == isOn) {
return;
}
this.isOn = isOn;
post(new Runnable() {
@Override
public void run() {
scrollSwitch();
}
});
}
/**
* 設(shè)置開關(guān)狀態(tài)監(jiān)聽器
*
* @param listner
* 開關(guān)狀態(tài)監(jiān)聽器
*/
public void setOnSwitchListner(SwitchChangedListner listner) {
this.listner = listner;
}
/**
* 開關(guān)狀態(tài)監(jiān)聽器
*
* @author llew
*
*/
public interface SwitchChangedListner {
/**
* 開關(guān)狀態(tài)改變
*
* @param viewId
* 當(dāng)前開關(guān)ID
* @param isOn
* 開關(guān)是否打開【true:打開】【false:關(guān)閉】
*/
public void switchChanged(Integer viewId, boolean isOn);
}
}
SwitchButton的實現(xiàn)也很簡單,首先是初始化組件initWedgits(),然后添加事件監(jiān)聽器addListeners(),在監(jiān)聽器中做邏輯判斷,代碼都有注釋,就不再詳細(xì)說明了
那么到了最后,我們來看看MainActivity中對SwitchButton的用法吧
public class MainActivity extends Activity {
private SwitchButton switchButton;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initWedgits();
}
/**
* 初始化各組件
*/
private void initWedgits() {
try {
switchButton = (SwitchButton) findViewById(R.id.switchButton);
// switchButton.setOn(false);
switchButton.setOn(true);
addListeners();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 添加事件監(jiān)聽器
*/
private void addListeners() {
try {
switchButton.setOnSwitchListner(new SwitchChangedListner() {
@Override
public void switchChanged(Integer viewId, boolean isOn) {
if(isOn) {
Toast.makeText(getApplicationContext(), "開關(guān)打開了", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "開關(guān)關(guān)閉了", Toast.LENGTH_LONG).show();
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}
好了,代碼都貼完了,接下來看看運行效果吧,(*^__^*) 嘻嘻……

很遺憾的是,木有動畫效果,先這樣
好了,基本上自定義開關(guān)組件到這里就講完了,如有不足,請大家諒解。
源碼下載:Android UI實現(xiàn)類似IOS中UISwitch的動畫效果
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- android開發(fā)教程之switch控件使用示例
- Android UI控件Switch的使用方法
- Android開關(guān)控件Switch的使用案例
- Android 自定義Switch開關(guān)按鈕的樣式實例詳解
- android自定義開關(guān)控件-SlideSwitch的實例
- Android自定義控件實現(xiàn)滑動開關(guān)效果
- Android自定義控件之開關(guān)按鈕學(xué)習(xí)筆記分享
- Android開發(fā)進(jìn)階自定義控件之滑動開關(guān)實現(xiàn)方法【附demo源碼下載】
- Android開發(fā)實現(xiàn)Switch控件修改樣式功能示例【附源碼下載】
相關(guān)文章
Android 藍(lán)牙連接 ESC/POS 熱敏打印機(jī)打印實例(藍(lán)牙連接篇)
這篇文章主要介紹了Android 藍(lán)牙連接 ESC/POS 熱敏打印機(jī)打印實例(藍(lán)牙連接篇),具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-04-04
Matrix的set,pre,post調(diào)用順序詳解
下面小編就為大家?guī)硪黄狹atrix的set,pre,post調(diào)用順序詳解。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04
Flutter禁止手機(jī)橫屏的簡單實現(xiàn)方法
app默認(rèn)是可以橫屏的,如果需要禁止橫屏話可以參考這篇文章,本文主要給大家介紹了關(guān)于Flutter禁止手機(jī)橫屏的簡單實現(xiàn)方法,需要的朋友可以參考下2021-07-07
Android實現(xiàn)Toast提示框圖文并存的方法
這篇文章主要介紹了Android實現(xiàn)Toast提示框圖文并存的方法,實例分析了Toast提示框的參數(shù)設(shè)置及圖文調(diào)用的相關(guān)技巧,需要的朋友可以參考下2016-01-01
Android使用OpenGL和MediaCodec錄制功能
OpenGL是一個跨平臺的操作GPU的API,但OpenGL需要本地視窗系統(tǒng)進(jìn)行交互,這就需要一個中間控制層, EGL就是連接OpenGL ES和本地窗口系統(tǒng)的接口,引入EGL就是為了屏蔽不同平臺上的區(qū)別,這篇文章主要介紹了Android使用OpenGL和MediaCodec錄制功能,需要的朋友可以參考下2025-04-04
Android筆記之:App應(yīng)用之啟動界面SplashActivity的使用
當(dāng)前比較成熟一點的應(yīng)用基本上都會在進(jìn)入應(yīng)用之顯示一個啟動界面.這個啟動界面或簡單,或復(fù)雜,或簡陋,或華麗,用意不同,風(fēng)格也不同2013-04-04

