自定义Seekbar


效果图:


好了直接上代码:

1:先看布局文件:

<com.android.camera.ui.EffectSettingPopupModify xmlns:android="http://schemas.android.com/apk/res/android"
        style="@style/EffectSettingPopupWindow"
        android:layout_width="491px"
        android:layout_height="500px">
    <RelativeLayout android:background="@drawable/floor"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:layout_alignParentBottom="true">
	<TextView
        android:id="@+id/Title_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/effects"
        android:textColor="#ffffff"
        android:paddingLeft="15px"
        android:layout_marginTop="8px"
        android:textSize="@dimen/effect_title_size" />

    <View
        android:id="@+id/view"
        android:layout_width="fill_parent"
        android:layout_height="2px"
        android:layout_below="@+id/Title_id"
        android:alpha="0.8"
        android:layout_marginTop="8px"
        android:background="#7eff00" />
    <!-- 1 -->
    <ImageView
        android:id="@+id/luminance_id"
        android:layout_width="51px"
        android:layout_height="51px"
        android:layout_below="@+id/view"
        android:background="@drawable/luminance"
        android:layout_marginLeft="43px"
        android:layout_marginTop="10px"
       />

    <TextView
        android:id="@+id/luminance_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/luminance_id"
        android:layout_below="@+id/control01"
        android:textColor="#ffffff"
        android:text="@string/luminance_title" />
	<LinearLayout
	    android:id="@+id/control01"
		android:layout_alignTop="@+id/luminance_id"
        android:layout_toRightOf="@+id/luminance_id"
		android:layout_marginLeft="30px"
		android:layout_width="350px" 
		android:layout_height="60px"
		android:orientation="horizontal"
		android:gravity="center"
		>    
    <SeekBar
        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">
    </SeekBar>
</LinearLayout>
    <TextView
        android:id="@+id/luminance_min"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/control01"
        android:layout_below="@+id/control01"
        android:paddingLeft="28px"
        android:textColor="#ffffff"
        android:text="@string/luminance_min" />

    <TextView
        android:id="@+id/luminance_cent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/control01"
        android:layout_marginLeft="280px"
        android:textColor="#ffffff"
        android:text="@string/luminance_cent" />

    <TextView
        android:id="@+id/luminance_max"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/control01"
        android:layout_below="@+id/control01"
        android:paddingRight="15px"
        android:textColor="#ffffff"
        android:text="@string/luminance_max" />
     <!-- 2 -->

    <ImageView
        android:id="@+id/contrast_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/luminance_title"
        android:src="@drawable/contrast"
        android:layout_marginLeft="@dimen/effect_margin_left"
        android:layout_marginTop="10px"/>

    <TextView
        android:id="@+id/contrast_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="25px"
        android:layout_below="@+id/control02"
        android:textColor="#ffffff"
        android:text="@string/contrast_title" />
<LinearLayout
	    android:id="@+id/control02"
		android:layout_alignTop="@+id/contrast_id"
        android:layout_toRightOf="@+id/contrast_id"
		android:layout_marginLeft="30px"
		android:layout_width="350px" 
		android:layout_height="60px"
		android:orientation="horizontal"
		android:gravity="center"
		>
    <SeekBar
        android:id="@+id/SeekBar02"
        android:layout_width="match_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" >
    </SeekBar>
</LinearLayout>    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/control02"
        android:layout_below="@+id/control02"
        android:paddingLeft="28px"
        android:textColor="#ffffff"
        android:text="@string/luminance_min" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/control02"
        android:layout_marginLeft="280px"
        android:textColor="#ffffff"
        android:text="@string/luminance_cent" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/control02"
        android:layout_below="@+id/control02"
        android:paddingRight="15px"
        android:textColor="#ffffff"
        android:text="@string/luminance_max" />
        <!-- 3 -->

    <ImageView
        android:id="@+id/saturation_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/contrast_title"
        android:src="@drawable/saturation"
        android:layout_marginLeft="43px" />

    <TextView
        android:id="@+id/saturation_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="25px"
        android:layout_below="@+id/control03"
        android:textColor="#ffffff"
        android:text="@string/saturation_title" />
<LinearLayout
	    android:id="@+id/control03"
		android:layout_alignTop="@+id/saturation_id"
        android:layout_toRightOf="@+id/saturation_id"
		android:layout_marginLeft="30px"
		android:layout_width="350px" 
		android:layout_height="60px"
		android:orientation="horizontal"
		android:gravity="center"
		>
    <SeekBar
        android:id="@+id/SeekBar03"
        android:layout_width="match_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">
    </SeekBar>
</LinearLayout>    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/control03"
        android:layout_below="@+id/control03"
        android:paddingLeft="28px"
        android:textColor="#ffffff"
        android:text="@string/luminance_min" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/control03"
        android:layout_marginLeft="280px"
        android:textColor="#ffffff"
        android:text="@string/luminance_cent" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/control03"
        android:layout_below="@+id/control03"
        android:paddingRight="15px"
        android:textColor="#ffffff"
        android:text="@string/luminance_max" />
    <!-- 4 -->

    <ImageView
        android:id="@+id/acutance_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/saturation_title"
        android:src="@drawable/acutance"
        android:layout_marginLeft="43px"/>

    <TextView
        android:id="@+id/acutance_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/acutance_id"
        android:layout_below="@+id/control04"
        android:textColor="#ffffff"
        android:text="@string/acutance_title" />
<LinearLayout
	    android:id="@+id/control04"
		android:layout_alignTop="@+id/acutance_id"
        android:layout_toRightOf="@+id/acutance_id"
		android:layout_marginLeft="30px"
		android:layout_width="350px" 
		android:layout_height="60px"
		android:orientation="horizontal"
		android:gravity="center"
		>
    <SeekBar
        android:id="@+id/SeekBar04"
       android:layout_width="match_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">
    </SeekBar>
</LinearLayout>    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/control04"
        android:layout_below="@+id/control04"
        android:paddingLeft="28px"
        android:textColor="#ffffff"
        android:text="@string/luminance_min" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/control04"
        android:layout_marginLeft="280px"
        android:textColor="#ffffff"
        android:text="@string/luminance_cent" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/control04"
        android:layout_below="@+id/control04"
        android:paddingRight="15px"
        android:textColor="#ffffff"
        android:text="@string/luminance_max" />
     </RelativeLayout>
</com.android.camera.ui.EffectSettingPopupModify>

关键点:<SeekBar
        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">
    </SeekBar>

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

<layer-list  
    xmlns:android="http://schemas.android.com/apk/res/android">  
    <item  
        android:id="@android:id/background"  
        android:drawable="@drawable/slip_line_normal"/>  
    <item  
        android:id="@android:id/progress"  
        android:drawable="@drawable/slip_line_green"/>  
    <item  
        android:id="@android:id/secondaryProgress"  
        android:drawable="@drawable/slip_line_green"/>  
</layer-list>    

好了布局就已经完成了,只要在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();
	}
   
}



  • 0
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
Android中,可以通过自定义SeekBar实现数字滑动功能。首先,在布局文件中定义自定义SeekBar的样式,可以使用ProgressBar来实现。如下所示: ``` <ProgressBar android:id="@+id/customSeekBar" style="@style/Widget.AppCompat.ProgressBar.Horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="100" android:progress="50" android:progressDrawable="@drawable/custom_seekbar_progress" android:thumb="@drawable/custom_seekbar_thumb" /> ``` 在drawable文件夹下创建custom_seekbar_progress.xml和custom_seekbar_thumb.xml来定义SeekBar的背景和滑块样式。在custom_seekbar_progress.xml中,可以使用shape和gradient标签来定义进度条的背景样式。在custom_seekbar_thumb.xml中,可以使用shape标签来定义滑块的样式。 接下来,在Activity或Fragment中找到SeekBar的实例,并设置OnSeekBarChangeListener监听器。在监听器中,通过getProgress方法获取SeekBar的进度值,并根据需要进行相应的处理。例如,可以在TextView中显示SeekBar的进度值,如下所示: ``` SeekBar customSeekBar = findViewById(R.id.customSeekBar); final TextView progressTextView = findViewById(R.id.progressTextView); customSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { progressTextView.setText(String.valueOf(progress)); } @Override public void onStartTrackingTouch(SeekBar seekBar) { // 当开始滑动SeekBar时执行的操作 } @Override public void onStopTrackingTouch(SeekBar seekBar) { // 当结束滑动SeekBar时执行的操作 } }); ``` 通过设置OnSeekBarChangeListener监听器,可以在SeekBar滑动时实时更新进度值,并进行相应的处理操作。根据自己的需求,可以在onProgressChanged、onStartTrackingTouch和onStopTrackingTouch方法中添加自定义的逻辑。 以上就是使用自定义SeekBar实现数字滑动的简单方法。可以根据自己的需求进行进一步的定制和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值