天天看點

android中使用ToggleButton(開關按鈕)自定義開關自定義開關ToggleButton

自定義開關ToggleButton

android中使用ToggleButton(開關按鈕)自定義開關自定義開關ToggleButton
android中使用ToggleButton(開關按鈕)自定義開關自定義開關ToggleButton

package com.example.test;

import android.os.Bundle;

import android.app.Activity;

import android.view.Menu;

//------------MainActivity中----------------------

public class MainActivity extends Activity {

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        //找到控件

        TooggleButtonCustom buttonCustom=(TooggleButtonCustom) findViewById(R.id.custom_button);

            //設定背景圖檔

              buttonCustom.setBackGroundPic(R.drawable.switch_background);

              //設定前邊的圖檔

              buttonCustom.setForePic(R.drawable.slide_button_background);

              //設定預設狀态

              buttonCustom.setState(false);

    }

}

//-------------自定義控件TooggleButtonCustom------繼承view--------------------

package com.example.test;

import android.content.Context;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.graphics.Canvas;

import android.util.AttributeSet;

import android.view.MotionEvent;

import android.view.View;

public class TooggleButtonCustom extends View{

    private Bitmap backBitmap;//背景圖檔

    private Bitmap foreBitmap;//上面的圖檔

    private boolean state=false;

    private int currentX;//獲得位置

    boolean isTouching;//是否是觸摸

    //有style資源檔案時使用

    public TooggleButtonCustom(Context context, AttributeSet attrs, int defStyle) {

        super(context, attrs, defStyle);

        // TODO Auto-generated constructor stub

    }

    // xml布局檔案中使用

    public TooggleButtonCustom(Context context, AttributeSet attrs) {

        super(context, attrs);

        // TODO Auto-generated constructor stub

    }

    //java代碼中建立時使用這個構造方法

    public TooggleButtonCustom(Context context) {

        super(context);

        // TODO Auto-generated constructor stub

    }

    //設定背景圖檔

    public void setBackGroundPic(int switchBackground) {

        backBitmap = BitmapFactory.decodeResource(getResources(),switchBackground);

    }

    //設定前邊圖檔

    public void setForePic(int slideButtonBackground) {

        foreBitmap = BitmapFactory.decodeResource(getResources(),slideButtonBackground);

    }

    //設定開關狀态

    public void setState(boolean b) {

        this.state=b;

    }

    @Override

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        setMeasuredDimension(backBitmap.getWidth(), backBitmap.getHeight());

    }

    @Override

    protected void onDraw(Canvas canvas) {

        // 1.平鋪背景圖檔

        canvas.drawBitmap(backBitmap, 0, 0, null);

        if (isTouching) {

            int left = currentX - foreBitmap.getWidth()/2;

            //兩個問題 ,位置不在中心,,移出範圍

            if (left < 0) {

                left = 0;

            }else if (left > backBitmap.getWidth() - foreBitmap.getWidth()) {

                left = backBitmap.getWidth() - foreBitmap.getWidth();

            }

            canvas.drawBitmap(foreBitmap, left, 0, null);

        }else {

            // 2.根據狀态去繪制 上面圖檔

            if (state) {

                // 右邊

                canvas.drawBitmap(foreBitmap,

                        backBitmap.getWidth() - foreBitmap.getWidth(), 0, null);

            } else {

                // 左邊

                canvas.drawBitmap(foreBitmap, 0, 0, null);

            }

        }

    }

    @Override

    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()) {

        case MotionEvent.ACTION_DOWN:

            isTouching = true;

            currentX = (int) event.getX();

            break;

        case MotionEvent.ACTION_MOVE:

            isTouching = true;

            currentX = (int) event.getX();

            break;

        case MotionEvent.ACTION_UP:

            isTouching = false;

            currentX = (int) event.getX();

            state = currentX > backBitmap.getWidth()/2;

            break;

        default:

            break;

        }

        invalidate();//重複繪制控件 自動調用OnDraw()

        return true;// 自己處理觸摸事件

    }

}

//------------------------主布局檔案---------------------------

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context=".MainActivity" >

    <com.example.test.TooggleButtonCustom

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_centerInParent="true"

        android:id="@+id/custom_button"></com.example.test.TooggleButtonCustom>

</RelativeLayout>

繼續閱讀