2013-07-01 31 views

Trả lời

10

Tôi gặp phải sự cố tương tự. Nhờ Commonsware trỏ đến đúng hướng. Tôi đã viết một lớp học lấy cảm hứng từ code.google.com/p/range-seek-bar) để có được giải pháp.

https://github.com/vashisthg/StartPointSeekBar

+0

Điều này! rất đẹp! Tôi thấy ngón tay cái hơi to nhưng có thể điều chỉnh được, cũng rất dễ thay đổi màu sắc sang trái và phải. Chỉ gặp khó khăn khi thiết lập setNormalizedValue, khi được đặt thành -1, nó bắt đầu tất cả các cách ở bên trái, khi được đặt thành 1 nó bắt đầu tất cả các cách ở bên phải. không thể đặt giá trị đặt ở giữa giải pháp – CularBytes

+1

ở giữa: giá trị từ 0 đến 1, trong đó 0,5 là trung tâm. – CularBytes

+1

Có sẵn để đặt thanh tìm kiếm tùy chỉnh này thành dọc không? – filipst

2

Để bắt đầu ở giữa, bạn có thể gọi setProgress() với một giá trị đó là một nửa của tổng số phạm vi của bạn.

Để bắt đầu từ SeekBar ở giữa và đi đến kết thúc (tức là đường màu xanh của bạn chạy từ giữa sang trái hoặc từ phải sang phải), bạn cần tạo thay thế riêng cho SeekBar hoặc tìm ai đó đã thực hiện việc này, vì điều này không được hỗ trợ bởi chính bản thân số SeekBar.

+0

'thay thế riêng cho SeekBa r', bạn quan tâm 'style'? – lysenkobv

+0

@lysenkobv: Không, ý tôi là một lớp Java. 'SeekBar', giống như' ProgressBar' mà từ đó nó kế thừa, được thiết kế để đặt điểm bắt đầu của thanh ở cạnh trái. Tôi sẽ khá ngạc nhiên nếu điều đó có thể được thay đổi thông qua một phong cách. Tôi hy vọng rằng bạn sẽ cần phải tạo của riêng bạn 'View' xử lý này. Ví dụ, 'RangeSeekBar' (https://code.google.com/p/range-seek-bar/) được thực hiện như một lớp con của' ImageView', để xử lý trường hợp bạn muốn hai ngón tay cái thay vì một. – CommonsWare

4

customSeekBar

Như Trên hết nói, nó có thể đạt được thực hiện tôi seekbar tùy chỉnh riêng của chúng tôi.

Tôi đã thử theo cách sau và nó hiệu quả với tôi.

activity_main.xml

<RelativeLayout 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" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity" 
    android:layout_margin="20dp"> 

<com.example.customseekbar.CustomSeekBar 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"   
    android:max="100" 
    android:progress="50" 
    android:progressDrawable="@android:color/transparent" 
    android:id="@+id/customSeekBar" 
/> 

</RelativeLayout> 

MainActivity.java

package com.example.customseekbar; 

import com.example.suricustomseekbar.R; 
import android.app.Activity; 
import android.os.Bundle; 
import android.widget.SeekBar; 

public class MainActivity extends Activity { 

SeekBar mseekBar; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    mseekBar = (SeekBar) findViewById(R.id.customSeekBar); 
    mseekBar.setProgress(50); 
    } 
    } 

CustomSeekBar.java

package com.example.customseekbar; 

import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.graphics.Rect; 
import android.util.AttributeSet; 
import android.widget.SeekBar; 

    public class CustomSeekBar extends SeekBar { 

private Rect rect; 
private Paint paint ; 
private int seekbar_height; 

public CustomSeekBar(Context context) { 
    super(context); 

} 

public CustomSeekBar(Context context, AttributeSet attrs) { 

    super(context, attrs); 
    rect = new Rect(); 
    paint = new Paint(); 
    seekbar_height = 6; 
} 

public CustomSeekBar(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
} 

@Override 
protected synchronized void onDraw(Canvas canvas) { 

    rect.set(0 + getThumbOffset(), 
      (getHeight()/2) - (seekbar_height/2), 
      getWidth()- getThumbOffset(), 
      (getHeight()/2) + (seekbar_height/2)); 

    paint.setColor(Color.GRAY); 

    canvas.drawRect(rect, paint); 



    if (this.getProgress() > 50) { 


     rect.set(getWidth()/2, 
       (getHeight()/2) - (seekbar_height/2), 
       getWidth()/2 + (getWidth()/100) * (getProgress() - 50), 
       getHeight()/2 + (seekbar_height/2)); 

     paint.setColor(Color.CYAN); 
     canvas.drawRect(rect, paint); 

    } 

    if (this.getProgress() < 50) { 

     rect.set(getWidth()/2 - ((getWidth()/100) * (50 - getProgress())), 
       (getHeight()/2) - (seekbar_height/2), 
       getWidth()/2, 
       getHeight()/2 + (seekbar_height/2)); 

     paint.setColor(Color.CYAN); 
     canvas.drawRect(rect, paint); 

    } 

    super.onDraw(canvas); 
    } 
} 
Các vấn đề liên quan