2012-10-31 39 views
7

Tôi phải tìm kiếm nơi tôi có thể thay đổi hầu hết mọi thứ theo chương trình. Và bây giờ tôi đang có một vấn đề để thay đổi ngón tay cái và kích thước thanh. Mọi thứ hoạt động tốt nếu thanh lớn hơn hoặc có cùng kích thước với ngón tay cái, nhưng nếu ngón tay cái lớn hơn thanh, tôi không thể hiển thị toàn bộ ngón tay cái với kích thước chính xác của thanh.Thay đổi kích thước của thanh tìm kiếm theo chương trình nhưng không thể nhận được ngón cái lớn hơn thanh

Dưới đây là mã của tôi để tạo ra các ngón tay cái:

public void setSeekBarThumb(int width, int height, int rColor, int gColor, int bColor){ 

     ShapeDrawable thumb = new ShapeDrawable(new OvalShape()); 
     thumb.setIntrinsicHeight(height); 
     thumb.setIntrinsicWidth(width); 
     thumb.setBounds(new Rect(0, 0, width, height)); 


     StateListDrawable states = new StateListDrawable(); 
     states.addState(new int[] {android.R.attr.state_pressed},thumb );//add to the state list with the state pressed 

     thumb = new ShapeDrawable(new OvalShape()); 
     thumb.setIntrinsicHeight(height); 
     thumb.setIntrinsicWidth(width); 
     thumb.setBounds(new Rect(0, 0, width, height)); 

     states.addState(new int[] { },thumb);//add to the state list with no state 

     seekbar.setThumb(states); 
     seekbar.setThumbOffset(0); 
    } 

heres mã của tôi để tạo thanh:

public void setSeekBarProgress(int width, int height, int rColor, int gColor, int bColor){ 
     GradientDrawable shape = new GradientDrawable(Orientation.BOTTOM_TOP, new int[]{Color.WHITE,Color.rgb(rColor, gColor, bColor)}); 
     shape.setCornerRadius(50); 
     shape.setSize(width, height); 
     shape.setBounds(0, 0, width, height); 
     ClipDrawable clip = new ClipDrawable(shape, Gravity.LEFT,ClipDrawable.HORIZONTAL); 

     shape = new GradientDrawable(Orientation.BOTTOM_TOP, new int[]{Color.WHITE, getResources().getColor(R.color.DimGray)}); 
     shape.setCornerRadius(50);//change the corners of the rectangle 
     shape.setSize(width, height); 
     shape.setBounds(0, 0, width, height); 

     LayerDrawable mylayer= new LayerDrawable(new Drawable[]{shape, clip,}); 

     seekbar.setProgressDrawable(mylayer); 
     seekbar.setProgress(50); 

    } 

Dưới đây là mã của tôi để đưa tất cả mọi thứ với nhau, nơi tôi thiết lập chiều cao của thanh tìm kiếm:

public MySeekBar(Activity activity, AbsoluteLayout ParentLayout, SeekBarParameters parameters) 
    {   
     super(activity.getApplicationContext()); 



     seekbar =(SeekBar)activity.getLayoutInflater().inflate(R.layout.horizontal_seek_bar, ParentLayout,false); 
     seekbar.setMax(100);  
     setSeekBarThumb(parameters.widthThumb, parameters.heightThumb,0,100,0); 
     setSeekBarProgress(parameters.width, parameters.height,255,69,0); 

     int drawHeight; 
     if(parameters.height>parameters.heightThumb) drawHeight=parameters.height; 
     else drawHeight=parameters.heightThumb; 

     AbsoluteLayout.LayoutParams params = new AsoluteLayout.LayoutParams(parameters.width,drawHeight,parameters.xPosition, parameters.yPosition); 
     ParentLayout.addView(seekbar, params);      

    } 

Mã XML nơi tôi làm tăng thanh từ:

<SeekBar xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:layout_gravity="center" 
     android:max="100" 
     android:progress="0" 
     android:secondaryProgress="0" 
     android:maxHeight="10000dip" 
     android:id="@+id/slider" 
    /> 

Để thay đổi kích thước thanh tìm kiếm, tôi sử dụng ParentLayout.addView (thanh tìm kiếm, thông số).
Khi thêm chế độ xem, nếu tôi sử dụng chiều cao mà tôi muốn thanh, ngón cái sẽ bị cắt. Nếu tôi sử dụng chiều cao của ngón tay cái, thanh sẽ đạt đến cùng chiều cao của ngón cái. Android thay đổi kích thước thanh theo kích thước tôi sử dụng để thêm chế độ xem.
Tôi đã thử thiết lập kích thước của GradientDrawable đó là thanh với s hape.setSize(width, height), tôi cũng đã thử shape.setBounds(0, 0, width, height) và tôi đã thử tạo một hình ảnh khác có kích thước phù hợp và thêm vào lớp con Lớp có thể vẽ được. Nhưng không có gì hiệu quả.

Khi tạo điều này thông qua XML, bạn có thể sử dụng paddingToppaddingBottom để đưa thanh đó đến đúng kích cỡ. Nhưng tôi không biết làm thế nào để làm điều này với GradientDrawable lập trình.

Trả lời

15

tốt, quá xấu không có câu trả lời nhưng tôi có thể tự trả lời câu hỏi. Câu trả lời là sử dụng InsetDrawable. Bạn phải đặt một trong các drawables của thanh vào một InsetDrawable.

Thats mã:

public void setSeekBarProgress(int width, int height, int rColor, int gColor, int bColor){ 
     GradientDrawable shape2 = new GradientDrawable(Orientation.BOTTOM_TOP, new int[]{Color.WHITE,Color.rgb(rColor, gColor, bColor)}); 
     shape2.setCornerRadius(50);      
     ClipDrawable clip = new ClipDrawable(shape2, Gravity.LEFT,ClipDrawable.HORIZONTAL); 

     GradientDrawable shape1 = new GradientDrawable(Orientation.BOTTOM_TOP, new int[]{Color.WHITE, getResources().getColor(R.color.DimGray)}); 
     shape1.setCornerRadius(50);//change the corners of the rectangle  
     InsetDrawable d1= new InsetDrawable(shape1,5,5,5,5);//the padding u want to use   


     LayerDrawable mylayer = new LayerDrawable(new Drawable[]{d1,clip}); 


     seekbar.setProgressDrawable(mylayer); 

     seekbar.setProgress(50); 

    } 

Tôi hy vọng điều này có thể giúp người khác với cùng một vấn đề.

+0

công việc tốt n Cảm ơn –

Các vấn đề liên quan