天天看点

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>

继续阅读