Android編程實(shí)現(xiàn)滑動按鈕功能詳解
本文實(shí)例講述了Android編程實(shí)現(xiàn)滑動按鈕功能。分享給大家供大家參考,具體如下:
首先效果圖:

然后是分別建立三個(gè)文件,第一個(gè)是main.class,第二個(gè)是SlipButton.class,第三個(gè)是 onchangeListener.class
main.class
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
public class Main extends Activity implements OnChangedListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SlipButton myBtn =(SlipButton) findViewById(R.id.slipBtn);//獲得指定控件
myBtn.SetOnChangedListener(this);//為控件設(shè)置監(jiān)聽器
}
@Override
public void OnChanged(boolean CheckState) {//當(dāng)按鈕狀態(tài)被改變時(shí)
// TODO Auto-generated method stub
if(CheckState)
Toast.makeText(this,"打開了..." , Toast.LENGTH_SHORT).show();
else
Toast.makeText(this,"關(guān)閉了..." , Toast.LENGTH_SHORT).show();
}
}
SlipButton.class
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
public class SlipButton extends View implements OnTouchListener{
private boolean NowChoose = false;//記錄當(dāng)前按鈕是否打開,true為打開,flase為關(guān)閉
private boolean OnSlip = false;//記錄用戶是否在滑動的變量
private float DownX,NowX;//按下時(shí)的x,當(dāng)前的x,
private Rect Btn_On,Btn_Off;//打開和關(guān)閉狀態(tài)下,游標(biāo)的Rect
private boolean isChgLsnOn = false;
private OnChangedListener ChgLsn;
private Bitmap bg_on,bg_off,slip_btn;
public SlipButton(Context context) {
super(context);
// TODO Auto-generated constructor stub
init();
}
public SlipButton(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
init();
}
private void init(){//初始化
//載入圖片資源
bg_on = BitmapFactory.decodeResource(getResources(), R.drawable.slip_bg_on);
bg_off = BitmapFactory.decodeResource(getResources(), R.drawable.slip_bg_off);
slip_btn = BitmapFactory.decodeResource(getResources(), R.drawable.slip_btn);
//獲得需要的Rect數(shù)據(jù)
Btn_On = new Rect(0,0,slip_btn.getWidth(),slip_btn.getHeight());
Btn_Off = new Rect(
bg_off.getWidth()-slip_btn.getWidth(),
0,
bg_off.getWidth(),
slip_btn.getHeight());
setOnTouchListener(this);//設(shè)置監(jiān)聽器,也可以直接復(fù)寫OnTouchEvent
}
@Override
protected void onDraw(Canvas canvas) {//繪圖函數(shù)
// TODO Auto-generated method stub
super.onDraw(canvas);
Matrix matrix = new Matrix();
Paint paint = new Paint();
float x;
{
if(NowX<(bg_on.getWidth()/2))//滑動到前半段與后半段的背景不同,在此做判斷
canvas.drawBitmap(bg_off,matrix, paint);//畫出關(guān)閉時(shí)的背景
else
canvas.drawBitmap(bg_on,matrix, paint);//畫出打開時(shí)的背景
if(OnSlip)//是否是在滑動狀態(tài),
{
if(NowX >= bg_on.getWidth())//是否劃出指定范圍,不能讓游標(biāo)跑到外頭,必須做這個(gè)判斷
x = bg_on.getWidth()-slip_btn.getWidth()/2;//減去游標(biāo)1/2的長度...
else
x = NowX - slip_btn.getWidth()/2;
}else{//非滑動狀態(tài)
if(NowChoose)//根據(jù)現(xiàn)在的開關(guān)狀態(tài)設(shè)置畫游標(biāo)的位置
x = Btn_Off.left;
else
x = Btn_On.left;
}
if(x<0)//對游標(biāo)位置進(jìn)行異常判斷...
x = 0;
else if(x>bg_on.getWidth()-slip_btn.getWidth())
x = bg_on.getWidth()-slip_btn.getWidth();
canvas.drawBitmap(slip_btn,x, 0, paint);//畫出游標(biāo).
}
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch(event.getAction())//根據(jù)動作來執(zhí)行代碼
{
case MotionEvent.ACTION_MOVE://滑動
NowX = event.getX();
break;
case MotionEvent.ACTION_DOWN://按下
if(event.getX()>bg_on.getWidth()||event.getY()>bg_on.getHeight())
return false;
OnSlip = true;
DownX = event.getX();
NowX = DownX;
break;
case MotionEvent.ACTION_UP://松開
OnSlip = false;
boolean LastChoose = NowChoose;
if(event.getX()>=(bg_on.getWidth()/2))
NowChoose = true;
else
NowChoose = false;
if(isChgLsnOn&&(LastChoose!=NowChoose))//如果設(shè)置了監(jiān)聽器,就調(diào)用其方法..
ChgLsn.OnChanged(NowChoose);
break;
default:
}
invalidate();//重畫控件
return true;
}
public void SetOnChangedListener(OnChangedListener l){//設(shè)置監(jiān)聽器,當(dāng)狀態(tài)修改的時(shí)候
isChgLsnOn = true;
ChgLsn = l;
}
}
onchangeListener.class
package CMD100.demo.slipButton;
public interface OnChangedListener {
abstract void OnChanged(boolean CheckState);
}
main.xml代碼
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<LinearLayout
android:orientation = "horizontal"
android:layout_width = "fill_parent"
android:layout_height = "wrap_content"
android:background = "#ff0000"
>
<TextView
android:text = "測試:"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
/>
<CMD100.demo.slipButton.SlipButton
android:id = "@+id/slipBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft = "10sp"
/>
</LinearLayout>
</LinearLayout>
注意:在xml里頭要放置的位置
<[包名].SlipButton android:id = "@+id/slipBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" />
然后可以像其他控件一樣使用了...
SlipButton myBtn =(SlipButton) findViewById(R.id.slipBtn); myBtn.SetOnChangedListener(...);
代碼到這里就全部完成了。
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android布局layout技巧總結(jié)》、《Android視圖View技巧總結(jié)》、《Android開發(fā)入門與進(jìn)階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》、《Android基本組件用法總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計(jì)有所幫助。
相關(guān)文章
autojs模仿QQ長按彈窗菜單實(shí)現(xiàn)示例
這篇文章主要為大家介紹了autojs模仿QQ長按彈窗菜單實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
React-Native中使用驗(yàn)證碼倒計(jì)時(shí)的按鈕實(shí)例代碼
這篇文章主要介紹了React-Native中使用驗(yàn)證碼倒計(jì)時(shí)的按鈕實(shí)例代碼,具有一定的參考價(jià)值,有興趣的可以了解一下2017-04-04
Android自定義View實(shí)現(xiàn)圓弧進(jìn)度的效果
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)圓弧進(jìn)度的效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-01-01
Android實(shí)現(xiàn)EditText的富文本編輯
這篇文章主要介紹了Android實(shí)現(xiàn)EditText的富文本編輯,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
Android實(shí)現(xiàn)自動變換大小的組件ViewPager2
這篇文章主要介紹了Android實(shí)現(xiàn)自動變換大小的組件ViewPager2,ViewPager2最顯著的特點(diǎn)是基于RecyclerView實(shí)現(xiàn),RecyclerView是目前Android端最成熟的AdapterView解決方案2023-03-03
簡單實(shí)現(xiàn)Android應(yīng)用的啟動頁
這篇文章主要介紹了簡單實(shí)現(xiàn)Android應(yīng)用的啟動頁,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09
Android手勢滑動實(shí)現(xiàn)ImageView縮放圖片大小
這篇文章主要為大家詳細(xì)介紹了Android手勢滑動實(shí)現(xiàn)ImageView縮放圖片大小的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-02-02
Kotlin類與屬性及構(gòu)造函數(shù)的使用詳解
這篇文章主要介紹了Kotlin語言中類與屬性及構(gòu)造函數(shù)的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-09-09

