Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> andorid 自定義SwitchButton

andorid 自定義SwitchButton

編輯:關於Android編程

因項目緣故需重新定制SwitchButton,效果如下:

\ \

過程如下:

1.圓角矩形的繪制

2.字體繪制

3.小圓繪制

4.左右滑動動畫效果繪制

代碼如下:

 

package com.smart.view;

import java.util.Timer;
import java.util.TimerTask;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class MySwitchButton extends View{
	
	private int w, h;//組件寬高
	private boolean flag_switch;//開關標志
	private boolean flag_touch;//觸摸標志
	private String ON = "ON";
	private String OFF = "OFF";
	private float textSize = 40;//字體大小
	private int color1 = 0xff999999;//關背景顏色
	private int color2 = 0xff3e9edb;//開背景顏色
	private int color_bg = color1;
	private int circle_bg_nomal = Color.WHITE;//圓正常情況下的背景
	private int circle_bg_touch = Color.YELLOW;//觸摸時圓的背景
	
	//圓心
	private float y;
	private float x;
	private float radius;//圓半徑
	private int distance = 10;//左右邊距
	
	private OnCircleClickListner mOnCircleClickListner;

	public MySwitchButton(Context context) {
		super(context);
		System.out.println("---MySwitchButton1---");
	}


	public MySwitchButton(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		System.out.println("---MySwitchButton2---");
	}

	public MySwitchButton(Context context, AttributeSet attrs) {
		super(context, attrs);
		System.out.println("---MySwitchButton3---");
	}
	
	public void setOnCircleClickListner(OnCircleClickListner mOnCircleClickListner){
		this.mOnCircleClickListner = mOnCircleClickListner;
	}
	
	
	
	public boolean isFlag_switch() {
		return flag_switch;
	}

	public void setChecked(boolean flag_switch) {
		this.flag_switch = flag_switch;
		if(flag_switch){
			x = distance+radius;
			color_bg = color2;
		}else{
			x = w-(distance+radius);
			color_bg = color1;
		}
		circleMove();
//		invalidate();
		
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
//		float x = event.getX();
//		float y = event.getY();

		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			
			flag_touch = true;
			invalidate();
			break;
		case MotionEvent.ACTION_MOVE:
			break;
		case MotionEvent.ACTION_UP:
			if(mOnCircleClickListner!=null){
				mOnCircleClickListner.onClick(MySwitchButton.this,!flag_switch);
			}
			flag_touch = false;
			invalidate();
			break;
		}

		return true;
	}
	
	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		super.onSizeChanged(w, h, oldw, oldh);
		
		this.w = w;
		this.h = h;
		y = h/2;
		radius =  h/2-distance;
		x = distance+radius;
		
//		color_bg = color1;
		circleMove();
//		invalidate();
		System.out.println("-----onSizeChanged----");
//		System.out.println("x:"+x);
//		System.out.println("w:"+w);
//		System.out.println("h:"+h);
	}
	
	
	@Override
	protected void onDraw(Canvas canvas) {
		System.out.println("-----onDraw----");
		Paint paint = new Paint();
		paint.setAntiAlias(true);
		canvas2RoundRect(canvas,paint);//圓角矩形
		canvas2Text(canvas,paint);//字體
		canvas2Circle(canvas,paint);//小圓
		super.onDraw(canvas);
		
		
	}
	
	public void canvas2RoundRect(Canvas canvas,Paint paint){
		paint.setColor(color_bg);
		RectF oval3 = new RectF(0, 0, w, h); 
        canvas.drawRoundRect(oval3, 45, 45, paint);
		
	}
	
	public void canvas2Circle(Canvas canvas,Paint paint){
		if(flag_touch){
			paint.setColor(circle_bg_touch);
		}else{
			paint.setColor(circle_bg_nomal);
		}
//		circleMove();
//		System.out.println("drawX:"+x);
		canvas.drawCircle(x, y, radius, paint);
	}
	
	Timer timer; 
	TimerTask task;
	public void circleMove(){//小圓運動軌跡
		final float startX =distance+radius; 
		final float endX = w-(distance+radius);
		final long upDataDistance = 10;
		if(timer!=null){
			task.cancel();
			timer.cancel();
			timer=null;
			task=null;
		}
		timer = new Timer();
		task = new TimerTask() {
			
			@Override
			public void run() {
				if(flag_switch){
					x+=upDataDistance;//打開
				}else{
					x-=upDataDistance;//關閉
					
				}
				if(x>=endX){
					x=endX;
					this.cancel();
					timer.cancel();
					timer =null;
				}else if(x
   

監聽接口時要調用public void setChecked(boolean flag_switch)該方法才能使小球左右滑動

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved