Android 养成记-1 --1.3 标签拾色器

直接上图:

 

 

创建seekBarDialog ,并将拾色器集成到dialog 中:

public class MySeekBarDialog extends AlertDialog implements TextWatcher {

	private SeekBar seekBar;
	private EditText tView;
	private TextView titleTxv;  //标题   
	private TextView msgTxv;    //提示内容文字
	private TextView positiveTxv;   //确定按钮  
    private TextView negativeTxv;   //取消按钮  
	public static int mColor ;
	public static String mtagName;
	/* �Զ��幹�캯�����ڳ�ʼ�� */

	public MySeekBarDialog(Context context) {
		super(context,R.style.custom_dialog);
		final View view = getLayoutInflater().inflate(R.layout.dialog_seekbar, null);
		
		seekBar = (SeekBar) view.findViewById(R.id.SeekBar01);
		tView = (EditText)view.findViewById(R.id.textview);
		titleTxv = (TextView) view.findViewById(R.id.title);  
		msgTxv = (TextView) view.findViewById(R.id.message); 
        positiveTxv = (TextView) view.findViewById(R.id.positiveButton);  
        negativeTxv = (TextView) view.findViewById(R.id.negativeButton);  
        tView.addTextChangedListener(textWatcher);//设置文本编辑的实时监听
		setView(view);
		initColorBar();
		

		
		seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

			@Override
			public void onStopTrackingTouch(SeekBar arg0) {
			}

			@Override
			public void onStartTrackingTouch(SeekBar arg0) {
			}

			@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				ColorPickGradient  mColorPickGradient = new ColorPickGradient();
				float radio = (float)progress / seekBar.getMax();  //获取当前滑动位置
				mColor = mColorPickGradient.getColor(radio);//采集当前位置的颜色
				seekBar.getThumb().setColorFilter(mColor, Mode.SRC_ATOP); //设置thumb的颜色
				//seekBar.getProgressDrawable().setColorFilter(Color.RED, Mode.SRC_ATOP);
				// mtagName = tView.getText().toString();
//				change.change(progress);
//				tView.setText("͸���ȣ�" + progress + "%");
			}
		});
	}
		
		private void initColorBar() {
		// TODO Auto-generated method stub
			//设置SeekBar的颜色  

	        ShapeDrawable.ShaderFactory shaderFactory = new ShapeDrawable.ShaderFactory() {  
	            @Override  
	            public Shader resize(int width, int height) {  
	                LinearGradient linearGradient = new LinearGradient(0, 0, width, height,  
	                        ColorPickGradient.PICKCOLORBAR_COLORS, ColorPickGradient.PICKCOLORBAR_POSITIONS, Shader.TileMode.REPEAT);  
	                return linearGradient;  
	            }  
	        };  
	        PaintDrawable paint = new PaintDrawable();  
	        paint.setShape(new RectShape());  
	        paint.setCornerRadius(10);  
	        paint.setShaderFactory(shaderFactory);  
	        seekBar.setProgressDrawable(paint);  
	}

		@Override  
	    public void setTitle(CharSequence title) {  
	        titleTxv.setText(title);  
	    }  
	  
	    @Override  
	    public void setTitle(int titleId) {  
	        setTitle(getContext().getString(titleId));  
	    }  
	      
	    /** 
	     * 设置提示内容文字 
	     * @param msg 
	     */  
	    public void setMessage(CharSequence msg){  
	        msgTxv.setText(msg);  
	    }  
	    /** 
	     * 设置确定键文字 
	     * @param text 
	     */  
	    public void setPositiveText(CharSequence text){  
	        positiveTxv.setText(text);  
	    }  
	    /** 
	     * 设置取消键文字 
	     * @param text 
	     */  
	    public void setNegativeText(CharSequence text){  
	        negativeTxv.setText(text);  
	    }  
	      
	    /** 
	     * 取消键监听器 
	     * @param onClickListener 
	     */ 

	    public void setNegativeButton(CharSequence text,View.OnClickListener onClickListener){ 
	    	negativeTxv.setText(text); 
	        negativeTxv.setOnClickListener(onClickListener);  
	    }
	    /** 
	     * 确定键监听器 
	     * @param
	     */ 
		public void setPositiveButton(String text, View.OnClickListener onClickListener) {
			// TODO Auto-generated method stub
			positiveTxv.setText(text);
	        positiveTxv.setOnClickListener(onClickListener);  
		}

		public void setEditContent(String string) {
			// TODO Auto-generated method stub
			if (string ==null)
				return;
	        tView.setText(string);
		}

		public void setEdit(EditText et) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onPointerCaptureChanged(boolean hasCapture) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onProvideKeyboardShortcuts(
				List<KeyboardShortcutGroup> data, Menu menu, int deviceId) {
			// TODO Auto-generated method stub
			
		}
		
		private TextWatcher textWatcher = new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
			int count) {
			//  
			System.out.println("-1-onTextChanged-->"
			+ tView.getText().toString() + "<--");
			}

		@Override
		public void afterTextChanged(Editable arg0) {
			// TODO Auto-generated method stub
			mtagName = tView.getText().toString();
			Log.v("mtagName", mtagName);		}

		@Override
		public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
				int arg3) {
			// TODO Auto-generated method stub
			
		}
		};
		
		
		@Override
		public void onTextChanged(CharSequence arg0, int arg1, int arg2,
				int arg3) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void afterTextChanged(Editable arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
				int arg3) {
			// TODO Auto-generated method stub
			
		}  

拾色器的封装接口:

public class ColorPickGradient {  
    //设置的颜色  
    public static final int[] PICKCOLORBAR_COLORS = new int[]{0xFFFFFFFF,0xFFFF3030, 0xFFF4A460,  
            0xFFFFFF00, 0xFF66CD00,  
            0xFF458B00, 0xFF0000EE,  
            0xFF912CEE,0xFF000000};  
    //每个颜色的位置  
    public static final float[] PICKCOLORBAR_POSITIONS = new float[]{0f, 0.1f, 0.2f, 0.3f, 0.5f, 0.65f,0.8f,0.9f,1f};  
  
    private int[] mColorArr = PICKCOLORBAR_COLORS;  
    private float[] mColorPosition = PICKCOLORBAR_POSITIONS;  
  
    public ColorPickGradient(int[] colorArr) {  
        this.mColorArr = colorArr;  
    }  
  
    public ColorPickGradient() {  
    }  
  
    /** 
     * 获取某个百分比位置的颜色 
     * @param radio 取值[0,1] 
     * @return 
     */  
    public int getColor(float radio) {  
        int startColor;  
        int endColor;  
        if (radio >= 1) {  
            return mColorArr[mColorArr.length - 1];  
        }  
        for (int i = 0; i < mColorPosition.length; i++) {  
            if (radio <= mColorPosition[i]) {  
                if (i == 0) {  
                    return mColorArr[0];  
                }  
                startColor = mColorArr[i - 1];  
                endColor = mColorArr[i];  
                float areaRadio = getAreaRadio(radio,mColorPosition[i-1],mColorPosition[i]);  
                return getColorFrom(startColor,endColor,areaRadio);  
            }  
        }  
        return -1;  
    }  
  
    public float getAreaRadio(float radio, float startPosition, float endPosition) {  
        return (radio - startPosition) / (endPosition - startPosition);  
    }  
  
    /** 
     *  取两个颜色间的渐变区间 中的某一点的颜色 
     * @param startColor 
     * @param endColor 
     * @param radio 
     * @return 
     */  
    public int getColorFrom(int startColor, int endColor, float radio) {  
        int redStart = Color.red(startColor);  
        int blueStart = Color.blue(startColor);  
        int greenStart = Color.green(startColor);  
        int redEnd = Color.red(endColor);  
        int blueEnd = Color.blue(endColor);  
        int greenEnd = Color.green(endColor);  
  
        int red = (int) (redStart + ((redEnd - redStart) * radio + 0.5));  
        int greed = (int) (greenStart + ((greenEnd - greenStart) * radio + 0.5));  
        int blue = (int) (blueStart + ((blueEnd - blueStart) * radio + 0.5));  
        return Color.argb(255, red, greed, blue);  
    }  
  

主要是知道怎么使用就行了。

本demo根据网络资源修改,侵删!!

demo 在如下的链接中,可以直接运行:

https://download.csdn.net/download/linzihahaha/10768993

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值