自定義開關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>

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM