首页 > 代码库 > 仿iphone开关按钮

仿iphone开关按钮

已屏蔽掉触摸冲突,直接上代码

package com.pobing.extra.views;

import com.example.iphonebut.R;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class SwitchButton extends LinearLayout {

	private int lastX;
	TextView top;
	TextView bottom;
	int marginLeft;
	boolean isLeft = true;
	int pressX;
	ObjectAnimator leftAnimator;
	ObjectAnimator rightAnimator;
	Context context;

	OnSwitchListener listener;
	public SwitchButton(Context context) {
		super(context);
		this.context = context;
		initView(context);
	}

	public SwitchButton(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		initView(context);
	}

	public SwitchButton(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		this.context = context;
		initView(context);
	}

	private void initView(Context context) {

		View view = LayoutInflater.from(context).inflate(R.layout.switch_but,
				null);

		top = (TextView) view.findViewById(R.id.top);
		bottom = (TextView) view.findViewById(R.id.bottom);
		top.setOnTouchListener(new ButTouchListener());
		marginLeft = DensityUtil.dip2px(context, 4);
		addView(view);

	}

	private class ButTouchListener implements OnTouchListener {
		public boolean onTouch(View v, MotionEvent event) {

			switch (event.getAction()) {

			case MotionEvent.ACTION_DOWN:
				lastX = (int) event.getRawX();
				pressX = lastX;
				leftAnimator = ObjectAnimator.ofFloat(top, "translationX", 0)
						.setDuration(80);
				rightAnimator = ObjectAnimator.ofFloat(top, "translationX",
						bottom.getWidth() - top.getWidth() - marginLeft)
						.setDuration(80);
				break;

			case MotionEvent.ACTION_MOVE:
				v.getParent().requestDisallowInterceptTouchEvent(true);// 屏蔽掉回弹控件事件
				int dx = (int) event.getRawX() - lastX;
				// v.setTranslationX(v.getTranslationX()+dx);
				if (v.getTranslationX() + dx < 0)
					v.setTranslationX(0);
				else if (v.getTranslationX() + dx + v.getWidth() > bottom
						.getWidth())
					v.setTranslationX(bottom.getWidth() - v.getWidth()
							- marginLeft);
				else
					v.setTranslationX(v.getTranslationX() + dx);
				lastX = (int) event.getRawX();
				break;
			case MotionEvent.ACTION_UP:
				int upX = (int) event.getRawX();
				
				if (Math.abs(upX - pressX) == 0) {// 点击事件
					isLeft=!isLeft;
					if (v.getTranslationX() == 0) {// 状态改变 ,右移
						rightAnimator.start();
						bottom.setBackgroundResource(R.drawable.green_bg);
						//Toast.makeText(context, "右  "   , 0).show();
						 if(listener!=null)
							 listener.onSwitch(!isLeft);
					} else {
						leftAnimator.start();
						bottom.setBackgroundResource(R.drawable.white_bg);
						//Toast.makeText(context, "左", 0).show();
						 if(listener!=null)
							 listener.onSwitch(!isLeft);
					}

				} else if (v.getTranslationX() + v.getWidth() / 2 < bottom
						.getWidth() / 2) {
					leftAnimator.start();
					bottom.setBackgroundResource(R.drawable.white_bg);
					if (isLeft == false) {
						isLeft = true;
					//	Toast.makeText(context, "左", 0).show();
						 if(listener!=null)
							 listener.onSwitch(!isLeft);
					} 

				}else if (v.getTranslationX() + v.getWidth() / 2 >=bottom
						.getWidth() / 2) {
					rightAnimator.start();
					bottom.setBackgroundResource(R.drawable.green_bg);
					if (isLeft == true) {
						isLeft = false;
						//Toast.makeText(context, "右", 0).show();
						 if(listener!=null)
							 listener.onSwitch(!isLeft);
					}
				}
			 
				break;
			}

			return true;
		}
	}

	public void setOnSwitchListener(OnSwitchListener listener) {
		this.listener=listener;
	}
	public interface OnSwitchListener{
		public void onSwitch(boolean isRight);
	}
}

 

circle_but.xml

<?xml version="1.0" encoding="utf-8"?>
<shape

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

    
    
    android:useLevel= "false" >

    <solid android:color= "#ffffff" />
	<stroke android:width="2dp"
	    android:color="#efeff4"/>
   
     
    <corners android:radius="9999dp"/>

</shape>


white_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android= "http://schemas.android.com/apk/res/android"
    android:useLevel= "false" >

    <solid android:color= "#efeff4" />
	<stroke android:width="2dp"
	    android:color="#f2f2f2"/>
    <corners android:radius="9999dp"/>

</shape>


green_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android= "http://schemas.android.com/apk/res/android"
    android:useLevel= "false" >

    <solid android:color= "#4bd863" />
	<stroke android:width="2dp"
	    android:color="#f2f2f2"/>
    <corners android:radius="9999dp"/>

</shape>



switch_but.xml

 
    <RelativeLayout  
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent">
	<TextView
    android:id="@+id/bottom"
        android:background="@drawable/white_bg"
        android:layout_width="55dp"
        android:layout_height="28dp"
        android:layout_centerVertical="true"
         />
    <TextView
        android:id="@+id/top"
        android:background="@drawable/circle_but"
        android:layout_centerVertical="true"
        android:layout_width="25dp"
        android:layout_height="25dp"
        android:layout_marginLeft="2dp"
      
         />
    </RelativeLayout>
 





demo下载地址   点击打开链接

仿iphone开关按钮