自定义Seekbar


效果图:


好了直接上代码:

1:先看布局文件:


    
	

    
    
    

    
	    
    
    

    

    

    
     

    

    

    
    
    
    

    

    
        

    

    

    
    
    
    

    

    
    

    

    

    
    
    
    

    

    
     


关键点:         android:id="@+id/SeekBar01"
        android:layout_width="fill_parent"
        android:layout_height="40px"
        android:maxHeight="40px"
        android:minHeight="40px"
        android:max="100"
        android:progress="50"
        android:secondaryProgress="0"
        android:progressDrawable="@drawable/effect_seekbar_style"  
        android:thumb="@drawable/slide_one"
        android:thumbOffset="20px">
   

看看这个style:android:progressDrawable="@drawable/effect_seekbar_style" 

  
      
      
      
    

好了布局就已经完成了,只要在Activity里面继承implements SeekBar.OnSeekBarChangeListener 实现几个方法去改变响应的值就ok,是不是很简单呀!

上代码看看吧:


public class EffectSettingPopupModify extends RotateLayout implements SeekBar.OnSeekBarChangeListener {
	private OnPreferenceChangedListener mListener;
    protected CameraPicker mCameraPicker;
    private SeekBar seekBar01,seekBar02,seekBar03,seekBar04;
    private ComboPreferences mPreferences;
    private final int MAX = 2;
    private final int SEC_MAX = 1;
    private final int MIDDLE = 0;
    private final int SEC_MIN = -1;
    private final int MIN = -2;
    private int value_temp;
    
    public void setListener(OnPreferenceChangedListener listener) {
        mListener = listener;
        if (mCameraPicker != null) mCameraPicker.setListener(listener);
    }
	public EffectSettingPopupModify(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}
	public void initEffectSettingPopupModify(ComboPreferences preferences){
		mPreferences = preferences;
		
	}
	private SeekBar[] mSeekBar = new SeekBar[4];
	public void setSeekBar(int which,String progress){
		int temp = Integer.valueOf(progress);
		switch(temp){
		case MAX:
			mSeekBar[which].setProgress(100);
			break;
		case SEC_MAX:
			mSeekBar[which].setProgress(75);
			break;
		case MIDDLE:
			mSeekBar[which].setProgress(50);
			break;
		case SEC_MIN:
			mSeekBar[which].setProgress(25);
			break;
		case MIN:
			mSeekBar[which].setProgress(0);
			break;	
		}
	}
	private final String[] value = new String[]{"-2","-1","0","1","2"};
	private String getValue(int progress){
		int temp = progress/25;
		return value[temp];
	}
	@Override
	protected void onFinishInflate() {
		// TODO Auto-generated method stub
		super.onFinishInflate();
		 seekBar01 = (SeekBar) this.findViewById(R.id.SeekBar01);
	     seekBar02 = (SeekBar) this.findViewById(R.id.SeekBar02);
	     seekBar03 = (SeekBar) this.findViewById(R.id.SeekBar03);
	     seekBar04 = (SeekBar) this.findViewById(R.id.SeekBar04);
	     seekBar01.setOnSeekBarChangeListener(this);
	     seekBar02.setOnSeekBarChangeListener(this);
	     seekBar03.setOnSeekBarChangeListener(this);
	     seekBar04.setOnSeekBarChangeListener(this);
	     mSeekBar[0] = seekBar01;
	     mSeekBar[1] = seekBar02;
	     mSeekBar[2] = seekBar03;
	     mSeekBar[3] = seekBar04;
	}
	@Override
	public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
		// TODO Auto-generated method stub
		switch(seekBar.getId()){
		case R.id.SeekBar01:
			value_temp = progress;
			mPreferences.edit().putString(CameraSettings.KEY_BRIGHTNESS, getValue(value_temp)).commit();
			break;
		case R.id.SeekBar02:
			value_temp = progress;
			mPreferences.edit().putString(CameraSettings.KEY_CONTRAST, getValue(value_temp)).commit();
			break;
		case R.id.SeekBar03:
			value_temp = progress;
			mPreferences.edit().putString(CameraSettings.KEY_SATURATION, getValue(value_temp)).commit();
			break;
		case R.id.SeekBar04:
			value_temp = progress;
			mPreferences.edit().putString(CameraSettings.KEY_SHARPNESS, getValue(value_temp)).commit();
			break;	
		}
		mListener.onSharedPreferenceChanged();
	}

	@Override
	public void onStartTrackingTouch(SeekBar seekBar) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onStopTrackingTouch(SeekBar seekBar) {
		// TODO Auto-generated method stub
		switch(seekBar.getId()){
		case R.id.SeekBar01:
			mPreferences.edit().putString(CameraSettings.KEY_BRIGHTNESS, getValue(value_temp)).commit();
			break;
		case R.id.SeekBar02:
			mPreferences.edit().putString(CameraSettings.KEY_CONTRAST, getValue(value_temp)).commit();
			break;
		case R.id.SeekBar03:
			mPreferences.edit().putString(CameraSettings.KEY_SATURATION, getValue(value_temp)).commit();
			break;
		case R.id.SeekBar04:
			mPreferences.edit().putString(CameraSettings.KEY_SHARPNESS, getValue(value_temp)).commit();
			break;	
		}
		mListener.onSharedPreferenceChanged();
	}
   
}



你可能感兴趣的:(android,android,layout,string,constructor,class,null)