密码SwitchView




/**
 * 
 */
package com.example.test;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.animation.LinearInterpolator;
import android.widget.Scroller;

/**
 * @author Young
 *
 */
public class PwdSwitchView extends View {
	
	private Scroller scroller;
	private float position;
	private Paint paint;
	private int animTime=150;
	
	private OnPwdStateChangeListener pwdStateChangeListener;
	
	private int bagColor=0xff4169E1;
	private int thumbColor=0xffffffff;
	
	private boolean isRight=false;
	public PwdSwitchView(Context context) {
		super(context);
		init();
	}

	public PwdSwitchView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}
	public PwdSwitchView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		init();
	}
	@SuppressLint("NewApi")
	public PwdSwitchView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
		super(context, attrs, defStyleAttr, defStyleRes);
		init();
	}
	
	private void init() {
		scroller=new Scroller(getContext(), new LinearInterpolator());
		paint=new Paint();
		paint.setAntiAlias(true);
		try {
			String[]colors=((String)getTag()).split(",|:|;|\\|");
			thumbColor=Color.parseColor(colors[0]);
			bagColor=Color.parseColor(colors[1]);
		} catch (Exception e) {
			throw new RuntimeException("you should set color ,by using android:tag=\"#ffffff|#fe7320\","
					+ "and #ffffff is thumb color , #fe7320 is background . you can also use : or , or ; to split color");
		}
	}

	@Override
	protected void onDraw(Canvas canvas) {
		int width=getWidth();
		int height=getHeight();
		
		float r=width/3.0f/2;
		canvas.translate(0, height/2.0f);
		
		
		paint.setColor(bagColor);
		
		canvas.drawCircle(r, 0, r, paint);
		
		canvas.drawCircle(width-r, 0, r, paint);
		
		canvas.drawRect(r, -r, width-r,	 r, paint);
		
		paint.setColor(thumbColor);
		
		if (position<=width/6.0f) {
			
			canvas.drawCircle(r, 0, r*0.8f, paint);
			
			canvas.translate(r+r, 0);
			
			float distance=width/3.0f*2/4.0f;
			
			canvas.drawCircle(distance, 0, distance*0.3f, paint);
			canvas.drawCircle(distance*2, 0, distance*0.3f, paint);
			canvas.drawCircle(distance*3, 0, distance*0.3f, paint);
		}else if (position>=width*5.0f/6) {
			canvas.drawCircle( width*5.0f/6.0f , 0, r*0.8f, paint);
		}else {
			canvas.drawCircle(position, 0, r*0.8f, paint);
			canvas.translate(r+r, 0);
			float distance=width/3.0f*2/4.0f;
			if (width-position>distance*3) {
				canvas.drawCircle(distance, 0, distance*0.3f, paint);
				canvas.drawCircle(distance*2, 0, distance*0.3f, paint);
				canvas.drawCircle(distance*3, 0, distance*0.3f, paint);
			}else if (width-position>distance*2) {
				canvas.drawCircle(distance*2, 0, distance*0.3f, paint);
				canvas.drawCircle(distance*3, 0, distance*0.3f, paint);
			}else{
				canvas.drawCircle(distance*3, 0, distance*0.3f, paint);
			}
		}
	}
	
	@Override
	public void computeScroll() {
		super.computeScroll();
		
		if (scroller.computeScrollOffset()) {
			
			position=scroller.getCurrX();
			invalidate();
		}
	}
	private float pressX;
	private float pressY;
	private Boolean isMove;// 3值,null表示暂未判断出是否可以左右滑动,true表示需要左右滑动,false表示不需要左右滑动
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		
		switch (event.getAction()) {
			case MotionEvent.ACTION_DOWN:
				pressX=event.getX();
				pressY=event.getY();
//				position=pressX;
			case MotionEvent.ACTION_MOVE:
				if (isMove==null) {
					float dx=Math.abs(event.getX()-pressX);
					float dy=Math.abs(event.getY()-pressY);
					if (dx>dy&& dx>ViewConfiguration.get(getContext()).getScaledTouchSlop()) {
						isMove=true;
					}else if (dx<dy&& dx>ViewConfiguration.get(getContext()).getScaledTouchSlop()) {
						isMove=false;
					}
					
				}else if (isMove) {
					getParent().requestDisallowInterceptTouchEvent(true);
					position=event.getX();
				}
				
				break;
	
			case MotionEvent.ACTION_UP:
			case MotionEvent.ACTION_CANCEL:
				isMove=null;
				if (position<getWidth()/2) {
					scroller.startScroll( (int)position, 0, -(int)position+getWidth()/6, 0, animTime);
					if (pwdStateChangeListener!=null&&isRight) {
						isRight=false;
						pwdStateChangeListener.onPwdStateChange(this, false);
					}
				}else{
					scroller.startScroll( (int)position, 0,getWidth()- (int)position-getWidth()/6, 0, animTime);
					if (pwdStateChangeListener!=null&& !isRight) {
						isRight=true;
						pwdStateChangeListener.onPwdStateChange(this, true);
					}
				}
				break;
		 
		}
		
		invalidate();
		
		return true;
	}

	public void setOnPwdStateChangeListener(OnPwdStateChangeListener pwdStateChangeListener){
		this.pwdStateChangeListener=pwdStateChangeListener;
	}
	
	public void  setAnimDuration(int duration) {
		animTime=duration;
	}
	public interface OnPwdStateChangeListener{
		public void onPwdStateChange(View v,boolean isRight);
	}
}



import com.example.test.PwdSwitchView.OnPwdStateChangeListener;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

public class MainActivity extends  Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
 
		setContentView(R.layout.activity_main);
		 
		ViewGroup viewGroup=(ViewGroup) findViewById(R.id.container);
		
		for (int i = viewGroup.getChildCount()-1	; i >-1; i--) {
			PwdSwitchView pwdSwitchView=(PwdSwitchView) viewGroup.getChildAt(i);
			
			pwdSwitchView.setOnPwdStateChangeListener(new OnPwdStateChangeListener() {
				
				@Override
				public void onPwdStateChange(View v, boolean isRight) {
					Toast.makeText(getApplicationContext(), v.getTag()+" "+v+" "+isRight , 1).show();
					
				}
			});
		}
	}

 
	
	
}

<ScrollView 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" >

    <LinearLayout android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <com.example.test.PwdSwitchView
            android:layout_width="200dp"
            android:layout_height="100dp"
            
            android:tag="#ffffff|#fe7320" 
            android:layout_margin="5dp"/>
        
         <com.example.test.PwdSwitchView
            android:layout_width="100dp"
            android:layout_height="35dp"
            
            android:tag="#fe7320,#ffffff"
            android:layout_margin="5dp" />
         
          <com.example.test.PwdSwitchView
            android:layout_width="300dp"
            android:layout_height="100dp"
            
            android:tag="#ffffff;#ff4169E1" 
            android:layout_margin="5dp"/>
         <com.example.test.PwdSwitchView
            android:layout_width="80dp"
            android:layout_height="30dp"
            
            android:tag="#ff4169E1,#ffffff" 
            android:layout_margin="5dp"/>
         <com.example.test.PwdSwitchView
            android:layout_width="50dp"
            android:layout_height="20dp"
            
            android:tag="#ff0000,#e1e1e1" 
            android:layout_margin="5dp"/>
        
        
        <com.example.test.PwdSwitchView
            android:layout_width="200dp"
            android:layout_height="100dp"
            
            android:tag="#ffffff|#fe7320" 
            android:layout_margin="5dp"/>
        
         <com.example.test.PwdSwitchView
            android:layout_width="100dp"
            android:layout_height="35dp"
            
            android:tag="#fe7320,#ffffff"
            android:layout_margin="5dp" />
         
          <com.example.test.PwdSwitchView
            android:layout_width="150dp"
            android:layout_height="60dp"
            
            android:tag="#ffffff;#ff4169E1" 
            android:layout_margin="5dp"/>
         <com.example.test.PwdSwitchView
            android:layout_width="80dp"
            android:layout_height="30dp"
            
            android:tag="#ff4169E1,#ffffff" 
            android:layout_margin="5dp"/>
         <com.example.test.PwdSwitchView
            android:layout_width="50dp"
            android:layout_height="20dp"
            
            android:tag="#ff0000,#e1e1e1" 
            android:layout_margin="5dp"/>
        
          <com.example.test.PwdSwitchView
            android:layout_width="200dp"
            android:layout_height="100dp"
            
            android:tag="#ffffff|#fe7320" 
            android:layout_margin="5dp"/>
        
         <com.example.test.PwdSwitchView
            android:layout_width="100dp"
            android:layout_height="35dp"
            
            android:tag="#fe7320,#ffffff"
            android:layout_margin="5dp" />
         
          <com.example.test.PwdSwitchView
            android:layout_width="300dp"
            android:layout_height="100dp"
            
            android:tag="#ffffff;#ff4169E1" 
            android:layout_margin="5dp"/>
         <com.example.test.PwdSwitchView
            android:layout_width="80dp"
            android:layout_height="30dp"
            
            android:tag="#ff4169E1,#ffffff" 
            android:layout_margin="5dp"/>
         <com.example.test.PwdSwitchView
            android:layout_width="50dp"
            android:layout_height="20dp"
            
            android:tag="#ff0000,#e1e1e1" 
            android:layout_margin="5dp"/>
        
        <com.example.test.PwdSwitchView
            android:layout_width="40dp"
            android:layout_height="20dp"
            
            android:tag="#ff0000,#e1e1e1" 
            android:layout_margin="5dp"/>
        
        <com.example.test.PwdSwitchView
            android:layout_width="30dp"
            android:layout_height="20dp"
            
            android:tag="#ff0000,#e1e1e1" 
            android:layout_margin="5dp"/>
        
        <com.example.test.PwdSwitchView
            android:layout_width="20dp"
            android:layout_height="20dp"
            
            android:tag="#ff0000,#e1e1e1" 
            android:layout_margin="5dp"/>
        
        <com.example.test.PwdSwitchView
            android:layout_width="10dp"
            android:layout_height="20dp"
            
            android:tag="#ff0000,#e1e1e1" 
            android:layout_margin="5dp"/>
    </LinearLayout>

</ScrollView>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值